Skip to content

Instantly share code, notes, and snippets.

@alxmtr
Last active March 30, 2023 13:41
Show Gist options
  • Save alxmtr/20f87036566685ae10f8074b585974ad to your computer and use it in GitHub Desktop.
Save alxmtr/20f87036566685ae10f8074b585974ad to your computer and use it in GitHub Desktop.
Laravel Snippets

Manually register a user in Laravel (Tinker)

User::create([
    'email' => 'name@example.com', 
    'name' => 'Alex', 
    'password' => bcrypt('password')
])

Update a product if the amount is upper to 0

$product->update(['amount' => max(0, request('amount'))]);

Age macro with Carbon

\Illuminate\Support\Carbon::macro('age', function ($date) {
    return $this->diff($this->parse($date))->format('%y');
});
// usage:
now()->age('1988-08-25');           // 29
now()->addDays()->ge('1988-08-25'); // 30

Easy way to disable all logs from your app. In "logging.php":

'void' => [
    'driver' => 'monolog',
    'handler' => \Monolog\Handler\NullHandler::class
],

Pass data up the blade layout you are extending

@extends('layouts.app', ['nologo' => true])
<?php
// Manually register a user in Laravel (Tinker)
User::create([
'email' => 'name@example.com',
'name' => 'Alex',
'password' => bcrypt('password')
])
// Update a product if the amount is upper to 0
$product->update(['amount' => max(0, request('amount'))]);
// Age macro with Carbon
\Illuminate\Support\Carbon::macro('age', function ($date) {
return $this->diff($this->parse($date))->format('%y');
});
// usage:
now()->age('1988-08-25'); // 29
now()->addDays()->ge('1988-08-25'); // 30
// Easy way to disable all logs from your app. In "logging.php":
'void' => [
'driver' => 'monolog',
'handler' => \Monolog\Handler\NullHandler::class
],
// Pass data up the blade layout you are extending
@extends('layouts.app', ['nologo' => true])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment