@markdown# Reuse or clone query()Typically, we need to query multiple time from a filtered query. So, most of the time we use `query()` method,let's write a query for getting today created active and inactive products```php$query = Product::query();$today = request()->q_date ?? today();if($today){ $query->where('created_at', $today);}// lets get active and inactive products$active_products = $query->where('status', 1)->get(); // this line modified the $query object variable$inactive_products = $query->where('status', 0)->get(); // so here we will not find any inactive products```But, after getting `$active products` the` $query `will be modified. So, `$inactive_products` will not find any inactive products from `$query` and that will return blank collection every time. Cause, that will try to find inactive products from `$active_products` (`$query` will return active products only).For solve this issue, we can query multiple time by reusing this `$query` object.So, We need to clone this `$query` before doing any `$query` modification action.```php$active_products = $query->clone()->where('status', 1)->get(); // it will not modify the $query$inactive_products = $query->clone()->where('status', 0)->get(); // so we will get inactive products from $query```### Eloquent where date methodsIn Eloquent, check the date with functions `whereDay()`, `whereMonth()`, `whereYear()`, `whereDate()` and `whereTime()`.```php$products = Product::whereDate('created_at', '2018-01-31')->get();$products = Product::whereMonth('created_at', '12')->get();$products = Product::whereDay('created_at', '31')->get();$products = Product::whereYear('created_at', date('Y'))->get();$products = Product::whereTime('created_at', '=', '14:13:58')->get();```### Increments and decrementsIf you want to increment some DB column in some table, just use `increment()` function. Oh, and you can increment not only by 1, but also by some number, like 50.```phpPost::find($post_id)->increment('view_count');User::find($user_id)->increment('points', 50);```### No timestamp columnsIf your DB table doesn't contain timestamp fields `created_at` and `updated_at`, you can specify that Eloquent model wouldn't use them, with `$timestamps = false` property.```phpclass Company extends Model{ public $timestamps = false;}```### Soft-deletes: multiple restoreWhen using soft-deletes, you can restore multiple rows in one sentence.```phpPost::onlyTrashed()->where('author_id', 1)->restore();```### Model all: columnsWhen calling Eloquent's `Model::all()`, you can specify which columns to return.```php$users = User::all(['id', 'name', 'email']);```### To Fail or not to FailIn addition to `findOrFail()`, there's also Eloquent method `firstOrFail()` which will return 404 page if no records for query are found.```php$user = User::where('email', '[email protected]')->firstOrFail();```### Column name changeIn Eloquent Query Builder, you can specify "as" to return any column with a different name, just like in plain SQL query.```php$users = DB::table('users')->select('name', 'email as user_email')->get();```### Map query resultsAfter Eloquent query you can modify rows by using `map()` function in Collections.```php$users = User::where('role_id', 1)->get()->map(function (User $user) { $user->some_column = some_function($user); return $user;});```### Change Default Timestamp FieldsWhat if you’re working with non-Laravel database and your timestamp columns are named differently? Maybe, you have create_time and update_time. Luckily, you can specify them in the model, too:```phpclass Role extends Model{ const CREATED_AT = 'create_time'; const UPDATED_AT = 'update_time';}```### Quick Order by created_atInstead of:```phpUser::orderBy('created_at', 'desc')->get();```You can do it quicker:```phpUser::latest()->get();```By default, `latest()` will order by `created_at`.There is an opposite method `oldest()` which would order by `created_at` ascending:```phpUser::oldest()->get();```Also, you can specify another column to order by. For example, if you want to use `updated_at`, you can do this:```php$lastUpdatedUser = User::latest('updated_at')->first();```### Automatic Column Value When Creating RecordsIf you want to generate some DB column value when creating record, add it to model's `boot()` method.For example, if you have a field "position" and want to assign the next available position to the new record (like `Country::max('position') + 1)`, do this:```phpclass Country extends Model { protected static function boot() { parent::boot(); Country::creating(function($model) { $model->position = Country::max('position') + 1; }); }}```### DB Raw Query Calculations Run FasterUse SQL raw queries like `whereRaw()` method, to make some DB-specific calculations directly in query, and not in Laravel, usually the result will be faster. Like, if you want to get users that were active 30+ days after their registration, here's the code:```phpUser::where('active', 1) ->whereRaw('TIMESTAMPDIFF(DAY, created_at, updated_at) > ?', 30) ->get();```### More than One ScopeYou can combine and chain Query Scopes in Eloquent, using more than one scope in a query.Model:```phppublic function scopeActive($query) { return $query->where('active', 1);}public function scopeRegisteredWithinDays($query, $days) { return $query->where('created_at', '>=', now()->subDays($days));}```Some Controller:```php$users = User::registeredWithinDays(30)->active()->get();```### No Need to Convert CarbonIf you're performing `whereDate()` and check today's records, you can use Carbon's `now()` and it will automatically be transformed to date. No need to do `->toDateString()`.```php// Instead of$todayUsers = User::whereDate('created_at', now()->toDateString())->get();// No need to convert, just use now()$todayUsers = User::whereDate('created_at', now())->get();```### Grouping by First LetterYou can group Eloquent results by any custom condition, here's how to group by first letter of user's name:```php$users = User::all()->groupBy(function($item) { return $item->name[0];});```### Never Update the ColumnIf you have DB column which you want to be set only once and never updated again, you can set that restriction on Eloquent Model, with a mutator:```phpclass User extends Model{ public function setEmailAttribute($value) { if ($this->email) { return; } $this->attributes['email'] = $value; }}```### Find ManyEloquent method `find()` may accept multiple parameters, and then it returns a Collection of all records found, not just one Model:```php// Will return Eloquent Model$user = User::find(1);// Will return Eloquent Collection$users = User::find([1,2,3]);``````phpreturn Product::whereIn('id', $this->productIDs)->get();// You can do thisreturn Product::find($this->productIDs)```Tip given by [@tahiriqbalnajam](https://twitter.com/tahiriqbalnajam/status/1436120403655671817)Incase of integer, use `whereIn` with limited data range only instead use `whereIntegerInRaw` which is faster then `whereIn`.```phpProduct::whereIn('id', range(1, 50))->get();// You can do thisProduct::whereIntegerInRaw('id', range(1, 50))->get();```Tip given by [@sachinkiranti](https://raisachin.com.np)### Find Many and return specific columnsEloquent method `find()` may accept multiple parameters, and then it returns a Collection of all records found with specificied columns, not all columns of model:```php// Will return Eloquent Model with first_name and email only$user = User::find(1, ['first_name', 'email']);// Will return Eloquent Collection with first_name and email only$users = User::find([1,2,3], ['first_name', 'email']);```Tip given by [@tahiriqbalnajam](https://github.com/tahiriqbalnajam)### Find by KeyYou can also find multiple records with `whereKey()` method which takes care of which field is exactly your primary key (`id` is the default but you may override it in Eloquent model):```php$users = User::whereKey([1,2,3])->get();```### Use UUID instead of auto-incrementYou don't want to use auto incrementing ID in your model?Migration:```phpSchema::create('users', function (Blueprint $table) { // $table->increments('id'); $table->uuid('id')->unique();});```Model:```phpclass User extends Model{ public $incrementing = false; protected $keyType = 'string'; protected static function boot() { parent::boot(); User::creating(function ($model) { $model->setId(); }); } public function setId() { $this->attributes['id'] = Str::uuid(); }}```### Sub-selects in Laravel WayFrom Laravel 6, you can use addSelect() in Eloquent statement, and do some calculation to that added column.```phpreturn Destination::addSelect(['last_flight' => Flight::select('name') ->whereColumn('destination_id', 'destinations.id') ->orderBy('arrived_at', 'desc') ->limit(1)])->get();```### Hide Some ColumnsWhen doing Eloquent query, if you want to hide specific field from being returned, one of the quickest ways is to add `->makeHidden()` on Collection result.```php$users = User::all()->makeHidden(['email_verified_at', 'deleted_at']);```### Exact DB ErrorIf you want to catch Eloquent Query exceptions, use specific `QueryException` instead default Exception class, and you will be able to get the exact SQL code of the error.```phptry { // Some Eloquent/SQL statement} catch (\Illuminate\Database\QueryException $e) { if ($e->getCode() === '23000') { // integrity constraint violation return back()->withError('Invalid data'); }}```### Soft-Deletes with Query BuilderDon't forget that soft-deletes will exclude entries when you use Eloquent, but won't work if you use Query Builder.```php// Will exclude soft-deleted entries$users = User::all();// Will NOT exclude soft-deleted entries$users = User::withTrashed()->get();// Will NOT exclude soft-deleted entries$users = DB::table('users')->get();```### Good Old SQL QueryIf you need to execute a simple SQL query, without getting any results - like changing something in DB schema, you can just do `DB::statement()`.```phpDB::statement('DROP TABLE users');DB::statement('ALTER TABLE projects AUTO_INCREMENT=123');```### Use DB TransactionsIf you have two DB operations performed, and second may get an error, then you should rollback the first one, right?For that, I suggest to use DB Transactions, it's really easy in Laravel:```phpDB::transaction(function () { DB::table('users')->update(['votes' => 1]); DB::table('posts')->delete();});```### Update or CreateIf you need to check if the record exists, and then update it, or create a new record otherwise, you can do it in one sentence - use Eloquent method `updateOrCreate()`:```php// Instead of this$flight = Flight::where('departure', 'Oakland') ->where('destination', 'San Diego') ->first();if ($flight) { $flight->update(['price' => 99, 'discounted' => 1]);} else {$flight = Flight::create([ 'departure' => 'Oakland', 'destination' => 'San Diego', 'price' => 99, 'discounted' => 1]);}// Do it in ONE sentence$flight = Flight::updateOrCreate( ['departure' => 'Oakland', 'destination' => 'San Diego'], ['price' => 99, 'discounted' => 1]);```### Forget Cache on SaveTip given by [@pratiksh404](https://github.com/pratiksh404)If you have cache key like `posts` that gives collection, and you want to forget that cache key on new store or update, you can call static `saved` function on your model:```phpclass Post extends Model{ // Forget cache key on storing or updating public static function boot() { parent::boot(); static::saved(function () { Cache::forget('posts'); }); }}```### Change Format Of Created_at and Updated_atTip given by [@syofyanzuhad](https://github.com/syofyanzuhad)To change the format of `created_at` you can add a method in your model like this:```phppublic function getCreatedAtFormattedAttribute(){ return $this->created_at->format('H:i d, M Y');}```So you can use it `$entry->created_at_formatted` when it's needed.It will return the `created_at` attribute like this: `04:19 23, Aug 2020`.And also for changing format of `updated_at` attribute, you can add this method :```phppublic function getUpdatedAtFormattedAttribute(){ return $this->updated_at->format('H:i d, M Y');}```So you can use it `$entry->updated_at_formatted` when it's needed.It will return the `updated_at` attribute like this: `04:19 23, Aug 2020`.### Storing Array Type into JSONTip given by [@pratiksh404](https://github.com/pratiksh404)If you have input field which takes an array and you have to store it as a JSON, you can use `$casts` property in your model. Here `images` is a JSON attribute.```phpprotected $casts = [ 'images' => 'array',];```So you can store it as a JSON, but when retrieved from DB, it can be used as an array.### Make a Copy of the ModelIf you have two very similar Models (like shipping address and billing address) and you need to make a copy of one to another, you can use `replicate()` method and change some properties after that.Example from the [official docs](https://laravel.com/docs/8.x/eloquent#replicating-models):```php$shipping = Address::create([ 'type' => 'shipping', 'line_1' => '123 Example Street', 'city' => 'Victorville', 'state' => 'CA', 'postcode' => '90001',]);$billing = $shipping->replicate()->fill([ 'type' => 'billing']);$billing->save();```### Reduce MemorySometimes we need to load a huge amount of data into memory. For example:```php$orders = Order::all();```But this can be slow if we have really huge data, because Laravel prepares objects of the Model class.In such cases, Laravel has a handy function `toBase()````php$orders = Order::toBase()->get();//$orders will contain `Illuminate\Support\Collection` with objects `StdClass`.```By calling this method, it will fetch the data from the database, but it will not prepare the Model class.Keep in mind it is often a good idea to pass an array of fields to the get method, preventing all fields to be fetched from the database.### Force query without $fillable/$guardedIf you create a Laravel boilerplate as a "starter" for other devs, and you're not in control of what THEY would later fill in Model's $fillable/$guarded, you may use forceFill()```php$team->update(['name' => $request->name])```What if "name" is not in Team model's `$fillable`? Or what if there's no `$fillable/$guarded` at all?```php$team->forceFill(['name' => $request->name])```This will "ignore" the `$fillable` for that one query and will execute no matter what.### 3-level structure of parent-childrenIf you have a 3-level structure of parent-children, like categories in an e-shop, and you want to show the number of products on the third level, you can use `with('yyy.yyy')` and then add `withCount()` as a condition```phpclass HomeController extend Controller{ public function index() { $categories = Category::query() ->whereNull('category_id') ->with(['subcategories.subcategories' => function($query) { $query->withCount('products'); }])->get(); }}``````phpclass Category extends Model{ public function subcategories() { return $this->hasMany(Category::class); } public function products() { return $this->hasMany(Product::class); }}``````php
One returns a strict true/false boolean, the other returns an integer which you can use as a falsy in conditionals.```phppublic function index(){ if (\App\Models\User::exists()) { // returns boolean true or false if the table has any saved rows } if (\App\Models\User::count()) { // returns the count of rows in the table }}```Tip given by [@aschmelyun](https://twitter.com/aschmelyun/status/1440641525998764041)### How to prevent “property of non-object” error```php// BelongsTo Default Models// Let's say you have Post belonging to Author and then Blade code:$post->author->name;// Of course, you can prevent it like this:$post->author->name ?? ''// or@$post->author->name// But you can do it on Eloquent relationship level:// this relation will return an empty App\Author model if no author is attached to the postpublic function author() { return $this->belongsTo('App\Author')->withDefault();}// orpublic function author() { return $this->belongsTo('App\Author')->withDefault([ 'name' => 'Guest Author' ]);}```Tip given by [@coderahuljat](https://twitter.com/coderahuljat/status/1440556610837876741)### Get original attributes after mutating an Eloquent recordGet original attributes after mutating an Eloquent record you can get the original attributes by calling getOriginal()```php$user = App\User::first();$user->name; // John$user->name = "Peter"; // Peter$user->getOriginal('name'); // John$user->getOriginal(); // Original $user record```Tip given by [@devThaer](https://twitter.com/devThaer/status/1442133797223403521)### A simple way to seed a databaseA simple way to seed a database in Laravel with a .sql dump file```phpDB::unprepared( file_get_contents(__DIR__ . './dump.sql'));```Tip given by [@w3Nicolas](https://twitter.com/w3Nicolas/status/1447902369388249091)### The crossJoinSub method of the query constructorUsing the CROSS JOIN subquery```phpuse Illuminate\Support\Facades\DB;$totalQuery = DB::table('orders')->selectRaw('SUM(price) as total');DB::table('orders') ->select('*') ->crossJoinSub($totalQuery, 'overall') ->selectRaw('(price / overall.total) * 100 AS percent_of_total') ->get();```Tip given by [@PascalBaljet](https://twitter.com/pascalbaljet)### Belongs to Many Pivot table namingTo determine the table name of the relationship's intermediate table, Eloquent will join the two related model names in alphabetical order. This would mean a join between `Post` and `Tag` could be added like this:```phpclass Post extends Model{ public $table = 'posts'; public function tags() { return $this->belongsToMany(Tag::class); }}```However, you are free to override this convention, and you would need to specify the join table in the second argument.```phpclass Post extends Model{ public $table = 'posts'; public function tags() { return $this->belongsToMany(Tag::class, 'posts_tags'); }}```If you wish to be explicit about the primary keys you can also supply these as third and fourth arguments.```phpclass Post extends Model{ public $table = 'posts'; public function tags() { return $this->belongsToMany(Tag::class, 'post_tag', 'post_id', 'tag_id'); }}```Tip given by [@iammikek](https://twitter.com/iammikek)### Order by Pivot Fields`BelongsToMany::orderByPivot()` allows you to directly sort the results of a BelongsToMany relationship query.```phpclass Tag extends Model{ public $table = 'tags';}class Post extends Model{ public $table = 'posts'; public function tags() { return $this->belongsToMany(Tag::class, 'post_tag', 'post_id', 'tag_id') ->using(PostTagPivot::class) ->withTimestamps() ->withPivot('flag'); }}class PostTagPivot extends Pivot{ protected $table = 'post_tag';}// Somewhere in the Controllerpublic function getPostTags($id){ return Post::findOrFail($id)->tags()->orderByPivot('flag', 'desc')->get();}```Tip given by [@PascalBaljet](https://twitter.com/pascalbaljet)### Find a single record from a databaseThe `sole()` method will return only one record that matches the criteria. If no such entry is found, then a `NoRecordsFoundException` will be thrown. If multiple records are found, then a `MultipleRecordsFoundException` will be thrown.```phpDB::table('products')->where('ref', '#123')->sole();```Tip given by [@PascalBaljet](https://twitter.com/pascalbaljet)### Automatic records chunkingSimilar to `each()` method, but easier to use. Automatically splits the result into parts (chunks).```phpreturn User::orderBy('name')->chunkMap(fn ($user) => [ 'id' => $user->id, 'name' => $user->name,]), 25);```Tip given by [@PascalBaljet](https://twitter.com/pascalbaljet)### Updating the model without dispatching eventsSometimes you need to update the model without sending any events. We can now do this with the `updateQuietly()` method, which under the hood uses the `saveQuietly()` method.```php$flight->updateQuietly(['departed' => false]);```Tip given by [@PascalBaljet](https://twitter.com/pascalbaljet)### Periodic cleaning of models from obsolete recordsTo periodically clean models of obsolete records. With this trait, Laravel will do this automatically, only you need to adjust the frequency of the `model:prune` command in the Kernel class.```phpuse Illuminate\Database\Eloquent\Model;use Illuminate\Database\Eloquent\Prunable;class Flight extends Model{ use Prunable; /** * Get the prunable model query. * * @return \Illuminate\Database\Eloquent\Builder */ public function prunable() { return static::where('created_at', '<=', now()->subMonth()); }}```Also, in the pruning method, you can set the actions that must be performed before deleting the model:```phpprotected function pruning(){ // Removing additional resources, // associated with the model. For example, files. Storage::disk('s3')->delete($this->filename);}```Tip given by [@PascalBaljet](https://twitter.com/pascalbaljet)### Immutable dates and casting to themLaravel 8.53 introduces the `immutable_date` and `immutable_datetime` castes that convert dates to `Immutable`.Cast to CarbonImmutable instead of a regular Carbon instance.```phpclass User extends Model{ public $casts = [ 'date_field' => 'immutable_date', 'datetime_field' => 'immutable_datetime', ];}```Tip given by [@PascalBaljet](https://twitter.com/pascalbaljet)### The findOrFail method also accepts a list of idsThe findOrFail method also accepts a list of ids. If any of these ids are not found, then it "fails".
Nice if you need to retrieve a specific set of models and don't want to have to check that the count you got was the count you expected```phpUser::create(['id' => 1]);User::create(['id' => 2);User::create(['id' => 3]);// Retrives the user...$user = User::findOrFail(1);// Throws a 404 because the user doesn't exist...User::findOrFail(99);// Retrives all 3 users...$users = User::findOrFail([1, 2, 3]);// Throws because it is unable to find *all* of the usersUser::findOrFail([1, 2, 3, 99]);```Tip given by [@timacdonald87](https://twitter.com/timacdonald87/status/1457499557684604930)### Prunable trait to automatically remove models from your databaseNew in Laravel 8.50: You can use the Prunable trait to automatically remove models from your database. For example, you can permanently remove soft deleted models after a few days.```phpclass File extends Model{ use SoftDeletes; // Add Prunable trait use Prunable; public function prunable() { // Files matching this query will be pruned return static::query()->where('deleted_at', '<=', now()->subDays(14)); } protected function pruning() { // Remove the file from s3 before deleting the model Storage::disk('s3')->delete($this->filename); }}// Add PruneCommand to your schedule (app/Console/Kernel.php)$schedule->command(PruneCommand::class)->daily();```Tip by [@Philo01](https://twitter.com/Philo01/status/1457626443782008834)### withAggregate methodUnder the hood, the withAvg/withCount/withSum and other methods in Eloquent use the 'withAggregate' method. You can use this method to add a subselect based on a relationship```php// Eloquent Modelclass Post extends Model{ public function user() { return $this->belongsTo(User::class); }}// Instead of eager loading all users...$posts = Post::with('user')->get();// You can add a subselect to only retrieve the user's name...$posts = Post::withAggregate('user', 'name')->get();// This will add a 'user_name' attribute to the Post instance:$posts->first()->user_name;```Tip given by [@pascalbaljet](https://twitter.com/pascalbaljet/status/1457702666352594947)### Date conventionUsing the `something_at` convention instead of just a boolean in Laravel models gives you visibility into when a flag was changed – like when a product went live.```php// MigrationSchema::table('products', function (Blueprint $table) { $table->datetime('live_at')->nullable();});// In your modelpublic function live(){ return !is_null($this->live_at);}// Also in your modelprotected $dates = [ 'live_at'];```Tip given by [@alexjgarrett](https://twitter.com/alexjgarrett/status/1459174062132019212)### Eloquent multiple upsertsThe upsert() method will insert or update multiple records.- First array: the values to insert or update- Second: unique identifier columns used in the select statement- Third: columns that you want to update if the record exists```phpFlight::upsert([ ['departure' => 'Oakland', 'destination' => 'San Diego', 'price' => 99], ['departure' => 'Chicago', 'destination' => 'New York', 'price' => 150],], ['departure', 'destination'], ['price']);```Tip given by [@mmartin_joo](https://twitter.com/mmartin_joo/status/1461591319516647426)### Retrieve the Query Builder after filtering the resultsTo retrieve the Query Builder after filtering the results: you can use `->toQuery()`.
The method internally use the first model of the collection and a `whereKey` comparison on the Collection models.```php// Retrieve all logged_in users$loggedInUsers = User::where('logged_in', true)->get();// Filter them using a Collection method or php filtering$nthUsers = $loggedInUsers->nth(3);// You can't do this on the collection$nthUsers->update(/* ... */);// But you can retrieve the Builder using ->toQuery()if ($nthUsers->isNotEmpty()) { $nthUsers->toQuery()->update(/* ... */);}```Tip given by [@RBilloir](https://twitter.com/RBilloir/status/1462529494917566465)### Custom castsYou can create custom casts to have Laravel automatically format your Eloquent model data. Here's an example that capitalises a user's name when it is retrieved or changed.```phpclass CapitalizeWordsCast implements CastsAttributes{ public function get($model, string $key, $value, array $attributes) { return ucwords($value); } public function set($model, string $key, $value, array $attributes) { return ucwords($value); }}class User extends Model{ protected $casts = [ 'name' => CapitalizeWordsCast::class, 'email' => 'string', ]; }```Tip given by [@mattkingshott](https://twitter.com/mattkingshott/status/1462828232206659586)### Order based on a related model's average or countDid you ever need to order based on a related model's average or count?
It's easy with Eloquent!```phppublic function bestBooks(){ Book::query() ->withAvg('ratings as average_rating', 'rating') ->orderByDesc('average_rating');}```Tip given by [@mmartin_joo](https://twitter.com/mmartin_joo/status/1466769691385335815)### Return transactions resultIf you have a DB transaction and want to return its result, there are at least two ways, see the example```php// 1. You can pass the parameter by reference$invoice = NULL;DB::transaction(function () use (&$invoice) { $invoice = Invoice::create(...); $invoice->items()->attach(...);})// 2. Or shorter: just return trasaction result$invoice = DB::transaction(function () { $invoice = Invoice::create(...); $invoice->items()->attach(...); return $invoice;});```### Remove several global scopes from queryWhen using Eloquent Global Scopes, you not only can use MULTIPLE scopes, but also remove certain scopes when you don't need them, by providing the array to `withoutGlobalScopes()`
[Link to docs](https://laravel.com/docs/8.x/eloquent#removing-global-scopes)```php// Remove all of the global scopes...User::withoutGlobalScopes()->get();// Remove some of the global scopes...User::withoutGlobalScopes([ FirstScope::class, SecondScope::class])->get();```### Order JSON column attributeWith Eloquent you can order results by a JSON column attribute```php// JSON column example:// bikes.settings = {"is_retired": false}$bikes = Bike::where('athlete_id', $this->athleteId) ->orderBy('name') ->orderByDesc('settings->is_retired') ->get();```Tip given by [@brbcoding](https://twitter.com/brbcoding/status/1473353537983856643)### Get single column's value from the first resultYou can use `value()` method to get single column's value from the first result of a query```php// Instead ofIntegration::where('name', 'foo')->first()->active;// You can useIntegration::where('name', 'foo')->value('active');// or this to throw an exception if no records foundIntegration::where('name', 'foo')->valueOrFail('active')';```Tip given by [@justsanjit](https://twitter.com/justsanjit/status/1475572530215796744)### Check if altered value changed keyEver wanted to know if the changes you've made to a model have altered the value for a key? No problem, simply reach for originalIsEquivalent.```php$user = User::first(); // ['name' => "John']$user->name = 'John';$user->originalIsEquivalent('name'); // true$user->name = 'David'; // Set directly$user->fill(['name' => 'David']); // Or set via fill$user->originalIsEquivalent('name'); // false```Tip given by [@mattkingshott](https://twitter.com/mattkingshott/status/1475843987181379599)### New way to define accessor and mutatorNew way to define attribute accessors and mutators in Laravel 8.77:```php// Before, two-method approachpublic function setTitleAttribute($value){ $this->attributes['title'] = strtolower($value);}public function getTitleAttribute($value){ return strtoupper($value);} // New approachprotected function title(): Attribute{ return new Attribute( get: fn ($value) => strtoupper($value), set: fn ($value) => strtolower($value),}```Tip given by [@Teacoders](https://twitter.com/Teacoders/status/1473697808456851466)### Another way to do accessors and mutatorsIn case you are going to use the same accessors and mutators in many models , You can use custom casts instead.Just create a `class` that implements `CastsAttributes` interface. The class should have two methods, the first is `get` to specify how models should be retrieved from the database and the second is `set` to specify how the the value will be stored in the database.```phpdiffForHumans(); } public function set($model, string $key, $value, array $attributes) { return Carbon::parse($value)->format('Y-m-d h:i:s'); }}```Then you can implement the cast in the model class.```php TimestampsCast::class, 'created_at' => TimestampsCast::class, ];}```Tip given by [@AhmedRezk](https://github.com/AhmedRezk59)### When searching for the first record, you can perform some actionsWhen searching for the first record, you want to perform some actions, when you don't find it. `firstOrFail()` throws a 404 Exception.
You can use `firstOr(function() {})` instead. Laravel got your covered```php$book = Book::whereCount('authors') ->orderBy('authors_count', 'DESC') ->having('modules_count', '>', 10) ->firstOr(function() { // THe Sky is the Limit ... // You can perform any action here });```Tip given by [@bhaidar](https://twitter.com/bhaidar/status/1487757487566639113/)### Directly convert created_at date to human readable formatDid you know you can directly convert created_at date to human readble format like 1 miniute ago, 1 month ago using diffForHumans() function. Laravel eloquent by default enables Carbon instance on created_at field.```php$post = Post::whereId($id)->first();$result = $post->created_at->diffForHumans();/* OUTPUT */// 1 Minutes ago, 2 Week ago etc..as per created time```Tip given by [@vishal__2931](https://twitter.com/vishal__2931/status/1488369014980038662)### Ordering by an Eloquent AccessorOrdering by an Eloquent Accessor! Yes, that's doable. Instead of ordering by the accessor on the DB level, we order by the accessor on the returned Collection.```phpclass User extends Model{ // ... protected $appends = ['full_name']; public function getFullNameAttribute() { return $this->attribute['first_name'] . ' ' . $this->attributes['last_name']; } // ..}``````phpclass UserController extends Controller{ // .. public function index() { $users = User::all(); // order by full_name desc $users->sortByDesc('full_name'); // or // order by full_name asc $users->sortBy('full_name'); // .. } // ..}````sortByDesc` and `sortBy` are methods on the CollectionTip given by [@bhaidar](https://twitter.com/bhaidar/status/1490671693618053123)### Check for specific model was created or foundIf you want to check for specific model was created or found, use `wasRecentlyCreated` model attribute.```php$user = User::create([ 'name' => 'Oussama',]);// return booleanreturn $user->wasRecentlyCreated;// true for recently created// false for found (already on you db)```Tip given by [@sky_0xs](https://twitter.com/sky_0xs/status/1491141790015320064)### Laravel Scout with database driverWith laravel v9 you can use Laravel Scout (Search) with database driver. No more where likes!```php$companies = Company::search(request()->get('search'))->paginate(15);```Tip given by [@magarrent](https://twitter.com/magarrent/status/1493221422675767302)### Make use of the value method on the query builderMake use of the `value` method on the query builder to execute a more efficient query when you only need to retrieve a single column.```php// Before (fetches all columns on the row)Statistic::where('user_id', 4)->first()->post_count;// After (fetches only `post_count`)Statistic::where('user_id', 4)->value('post_count');```Tip given by [@mattkingshott](https://twitter.com/mattkingshott/status/1493583444244410375)### Pass array to where methodLaravel you can pass an array to the where method.```php// Instead of thisJobPost::where('company', 'laravel') ->where('job_type', 'full time') ->get();// You can pass an arrayJobPost::where(['company' => 'laravel', 'job_type' => 'full time']) ->get();```Tip given by [@cosmeescobedo](https://twitter.com/cosmeescobedo/status/1495626752282234881)### Return the primary keys from models collectionDid you know `modelsKeys()` eloquent collection method? It returns the primary keys from models collection.```php$users = User::active()->limit(3)->get();$users->modelsKeys(); // [1, 2, 3]```Tip given by [@iamharis010](https://twitter.com/iamharis010/status/1495816807910891520)### Force Laravel to use eager loadingIf you want to prevent a lazy loading in your app, you only need to add following line to the `boot()` method in your `AppServiceProvider````phpModel::preventLazyLoading();```But, if you want to enable this feature only on your local development you can change above code on that:```phpModel::preventLazyLoading(!app()->isProduction());```Tip given by [@CatS0up](https://github.com/CatS0up)### Make all your models mass assignableIt is not a recommended approach for security reasons, but it is possible.When you want do this, you don't need to set an empty `$guarded` array for every model, like this:```phpprotected $guarded = [];```You can do it from a single place, just add following line to the `boot()` method in your `AppServiceProvider`:```phpModel::unguard();```Now, all your models are mass assignable.Tip given by [@CatS0up](https://github.com/CatS0up)### Hiding columns in select all statementsIf you use Laravel v8.78 and MySQL 8.0.23 and onwards, you can define choosen columns as "invisible". Columns which are define as `invisible` will be hidden from the `select *` statements.However, to do so, we must use a `invisible()` method in the migration, something like that:```phpuse Illuminate\Database\Schema\Blueprint;use Illuminate\Support\Facades\Schema;Schema::table('table', function (Blueprint $table) { $table->string('secret')->nullable()->invisible();});```That's it! This will make chosen column hidden from `select *` statement.Tip given by [@CatS0up](https://github.com/CatS0up)### JSON Where ClausesLaravel offers helpers to query JSON columns for databases that support them.
Currently, MySQL 5.7+, PostgreSQL, SQL Server 2016, and SQLite 3.9.0 (using the JSON1 extension)```php// To query a json column you can use the -> operator$users = User::query() ->where('preferences->dining->meal', 'salad') ->get();// You can check if a JSON array contains a set of values$users = User::query() ->whereJsonContains('options->languages', [ 'en', 'de' ]) ->get();// You can also query by the length a JSON array$users = User::query() ->whereJsonLength('options->languages', '>', 1) ->get();```Tip given by [@cosmeescobedo](https://twitter.com/cosmeescobedo/status/1509663119311663124)### Get all the column names for a table```phpDB::getSchemaBuilder()->getColumnListing('users');/*returns [ 'id', 'name', 'email', 'email_verified_at', 'password', 'remember_token', 'created_at', 'updated_at',]; */```Tip given by [@aaronlumsden](https://twitter.com/aaronlumsden/status/1511014229737881605)### Compare the values of two columnsYou can use `whereColumn` method to compare the values of two columns.```phpreturn Task::whereColumn('created_at', 'updated_at')->get();// pass a comparison operatorreturn Task::whereColumn('created_at', '>', 'updated_at')->get();```Tip given by [@iamgurmandeep](https://twitter.com/iamgurmandeep/status/1511673260353548294)### Accessor CachingAs of Laravel 9.6, if you have a computationally intensive accessor, you can use the shouldCache method.```phppublic function hash(): Attribute{ return Attribute::make( get: fn($value) => bcrypt(gzuncompress($value)), )->shouldCache();}```Tip given by [@cosmeescobedo](https://twitter.com/cosmeescobedo/status/1514304409563402244)### New scalar() methodIn Laravel 9.8.0, the `scalar()` method was added that allows you to retrieve the first column of the first row from the query result.```php// BeforeDB::selectOne("SELECT COUNT(CASE WHEN food = 'burger' THEN 1 END) AS burgers FROM menu_items;")->burgers// NowDB::scalar("SELECT COUNT(CASE WHEN food = 'burger' THEN 1 END) FROM menu_items;")```Tip given by [@justsanjit](https://twitter.com/justsanjit/status/1514550185837408265)### Select specific columnsTo select specific columns on a model you can use the select method -- or you can pass an array directly to the get method!```php// Select specified columns from all employees$employees = Employee::select(['name', 'title', 'email'])->get();// Select specified columns from all employees$employees = Employee::get(['name', 'title', 'email']);```Tip given by [@ecrmnn](https://twitter.com/ecrmnn/status/1516087672351203332)### Chain conditional clauses to the query without writing if-else statementsThe "when" helper in the query builder is🔥You can chain conditional clauses to the query without writing if-else statements.Makes your query very clear:```phpclass RatingSorter extends Sorter{ function execute(Builder $query) { $query ->selectRaw('AVG(product_ratings.rating) AS avg_rating') ->join('product_ratings', 'products.id', '=', 'product_ratings.product_id') ->groupBy('products.id'); ->when( $this->direction === SortDirections::Desc, fn () => $query->orderByDesc('avg_rating') fn () => $query->orderBy('avg_rating'), ); return $query; }}```Tip given by [@mmartin_joo](https://twitter.com/mmartin_joo/status/1521461317940350976)@endmarkdown
- @foreach($categories as $category)
- {{ $category->name }} @if ($category->subcategories)
- @foreach($category->subcategories as $subcategory)
- {{ $subcategory->name }} @if ($subcategory->subcategories)
- @foreach ($subcategory->subcategories as $subcategory)
- {{ $subcategory->name }} ({{ $subcategory->product_count }}) @endforeach
@endforeach
@endforeach - {{ $subcategory->name }} @if ($subcategory->subcategories)
One returns a strict true/false boolean, the other returns an integer which you can use as a falsy in conditionals.```phppublic function index(){ if (\App\Models\User::exists()) { // returns boolean true or false if the table has any saved rows } if (\App\Models\User::count()) { // returns the count of rows in the table }}```Tip given by [@aschmelyun](https://twitter.com/aschmelyun/status/1440641525998764041)### How to prevent “property of non-object” error```php// BelongsTo Default Models// Let's say you have Post belonging to Author and then Blade code:$post->author->name;// Of course, you can prevent it like this:$post->author->name ?? ''// or@$post->author->name// But you can do it on Eloquent relationship level:// this relation will return an empty App\Author model if no author is attached to the postpublic function author() { return $this->belongsTo('App\Author')->withDefault();}// orpublic function author() { return $this->belongsTo('App\Author')->withDefault([ 'name' => 'Guest Author' ]);}```Tip given by [@coderahuljat](https://twitter.com/coderahuljat/status/1440556610837876741)### Get original attributes after mutating an Eloquent recordGet original attributes after mutating an Eloquent record you can get the original attributes by calling getOriginal()```php$user = App\User::first();$user->name; // John$user->name = "Peter"; // Peter$user->getOriginal('name'); // John$user->getOriginal(); // Original $user record```Tip given by [@devThaer](https://twitter.com/devThaer/status/1442133797223403521)### A simple way to seed a databaseA simple way to seed a database in Laravel with a .sql dump file```phpDB::unprepared( file_get_contents(__DIR__ . './dump.sql'));```Tip given by [@w3Nicolas](https://twitter.com/w3Nicolas/status/1447902369388249091)### The crossJoinSub method of the query constructorUsing the CROSS JOIN subquery```phpuse Illuminate\Support\Facades\DB;$totalQuery = DB::table('orders')->selectRaw('SUM(price) as total');DB::table('orders') ->select('*') ->crossJoinSub($totalQuery, 'overall') ->selectRaw('(price / overall.total) * 100 AS percent_of_total') ->get();```Tip given by [@PascalBaljet](https://twitter.com/pascalbaljet)### Belongs to Many Pivot table namingTo determine the table name of the relationship's intermediate table, Eloquent will join the two related model names in alphabetical order. This would mean a join between `Post` and `Tag` could be added like this:```phpclass Post extends Model{ public $table = 'posts'; public function tags() { return $this->belongsToMany(Tag::class); }}```However, you are free to override this convention, and you would need to specify the join table in the second argument.```phpclass Post extends Model{ public $table = 'posts'; public function tags() { return $this->belongsToMany(Tag::class, 'posts_tags'); }}```If you wish to be explicit about the primary keys you can also supply these as third and fourth arguments.```phpclass Post extends Model{ public $table = 'posts'; public function tags() { return $this->belongsToMany(Tag::class, 'post_tag', 'post_id', 'tag_id'); }}```Tip given by [@iammikek](https://twitter.com/iammikek)### Order by Pivot Fields`BelongsToMany::orderByPivot()` allows you to directly sort the results of a BelongsToMany relationship query.```phpclass Tag extends Model{ public $table = 'tags';}class Post extends Model{ public $table = 'posts'; public function tags() { return $this->belongsToMany(Tag::class, 'post_tag', 'post_id', 'tag_id') ->using(PostTagPivot::class) ->withTimestamps() ->withPivot('flag'); }}class PostTagPivot extends Pivot{ protected $table = 'post_tag';}// Somewhere in the Controllerpublic function getPostTags($id){ return Post::findOrFail($id)->tags()->orderByPivot('flag', 'desc')->get();}```Tip given by [@PascalBaljet](https://twitter.com/pascalbaljet)### Find a single record from a databaseThe `sole()` method will return only one record that matches the criteria. If no such entry is found, then a `NoRecordsFoundException` will be thrown. If multiple records are found, then a `MultipleRecordsFoundException` will be thrown.```phpDB::table('products')->where('ref', '#123')->sole();```Tip given by [@PascalBaljet](https://twitter.com/pascalbaljet)### Automatic records chunkingSimilar to `each()` method, but easier to use. Automatically splits the result into parts (chunks).```phpreturn User::orderBy('name')->chunkMap(fn ($user) => [ 'id' => $user->id, 'name' => $user->name,]), 25);```Tip given by [@PascalBaljet](https://twitter.com/pascalbaljet)### Updating the model without dispatching eventsSometimes you need to update the model without sending any events. We can now do this with the `updateQuietly()` method, which under the hood uses the `saveQuietly()` method.```php$flight->updateQuietly(['departed' => false]);```Tip given by [@PascalBaljet](https://twitter.com/pascalbaljet)### Periodic cleaning of models from obsolete recordsTo periodically clean models of obsolete records. With this trait, Laravel will do this automatically, only you need to adjust the frequency of the `model:prune` command in the Kernel class.```phpuse Illuminate\Database\Eloquent\Model;use Illuminate\Database\Eloquent\Prunable;class Flight extends Model{ use Prunable; /** * Get the prunable model query. * * @return \Illuminate\Database\Eloquent\Builder */ public function prunable() { return static::where('created_at', '<=', now()->subMonth()); }}```Also, in the pruning method, you can set the actions that must be performed before deleting the model:```phpprotected function pruning(){ // Removing additional resources, // associated with the model. For example, files. Storage::disk('s3')->delete($this->filename);}```Tip given by [@PascalBaljet](https://twitter.com/pascalbaljet)### Immutable dates and casting to themLaravel 8.53 introduces the `immutable_date` and `immutable_datetime` castes that convert dates to `Immutable`.Cast to CarbonImmutable instead of a regular Carbon instance.```phpclass User extends Model{ public $casts = [ 'date_field' => 'immutable_date', 'datetime_field' => 'immutable_datetime', ];}```Tip given by [@PascalBaljet](https://twitter.com/pascalbaljet)### The findOrFail method also accepts a list of idsThe findOrFail method also accepts a list of ids. If any of these ids are not found, then it "fails".
Nice if you need to retrieve a specific set of models and don't want to have to check that the count you got was the count you expected```phpUser::create(['id' => 1]);User::create(['id' => 2);User::create(['id' => 3]);// Retrives the user...$user = User::findOrFail(1);// Throws a 404 because the user doesn't exist...User::findOrFail(99);// Retrives all 3 users...$users = User::findOrFail([1, 2, 3]);// Throws because it is unable to find *all* of the usersUser::findOrFail([1, 2, 3, 99]);```Tip given by [@timacdonald87](https://twitter.com/timacdonald87/status/1457499557684604930)### Prunable trait to automatically remove models from your databaseNew in Laravel 8.50: You can use the Prunable trait to automatically remove models from your database. For example, you can permanently remove soft deleted models after a few days.```phpclass File extends Model{ use SoftDeletes; // Add Prunable trait use Prunable; public function prunable() { // Files matching this query will be pruned return static::query()->where('deleted_at', '<=', now()->subDays(14)); } protected function pruning() { // Remove the file from s3 before deleting the model Storage::disk('s3')->delete($this->filename); }}// Add PruneCommand to your schedule (app/Console/Kernel.php)$schedule->command(PruneCommand::class)->daily();```Tip by [@Philo01](https://twitter.com/Philo01/status/1457626443782008834)### withAggregate methodUnder the hood, the withAvg/withCount/withSum and other methods in Eloquent use the 'withAggregate' method. You can use this method to add a subselect based on a relationship```php// Eloquent Modelclass Post extends Model{ public function user() { return $this->belongsTo(User::class); }}// Instead of eager loading all users...$posts = Post::with('user')->get();// You can add a subselect to only retrieve the user's name...$posts = Post::withAggregate('user', 'name')->get();// This will add a 'user_name' attribute to the Post instance:$posts->first()->user_name;```Tip given by [@pascalbaljet](https://twitter.com/pascalbaljet/status/1457702666352594947)### Date conventionUsing the `something_at` convention instead of just a boolean in Laravel models gives you visibility into when a flag was changed – like when a product went live.```php// MigrationSchema::table('products', function (Blueprint $table) { $table->datetime('live_at')->nullable();});// In your modelpublic function live(){ return !is_null($this->live_at);}// Also in your modelprotected $dates = [ 'live_at'];```Tip given by [@alexjgarrett](https://twitter.com/alexjgarrett/status/1459174062132019212)### Eloquent multiple upsertsThe upsert() method will insert or update multiple records.- First array: the values to insert or update- Second: unique identifier columns used in the select statement- Third: columns that you want to update if the record exists```phpFlight::upsert([ ['departure' => 'Oakland', 'destination' => 'San Diego', 'price' => 99], ['departure' => 'Chicago', 'destination' => 'New York', 'price' => 150],], ['departure', 'destination'], ['price']);```Tip given by [@mmartin_joo](https://twitter.com/mmartin_joo/status/1461591319516647426)### Retrieve the Query Builder after filtering the resultsTo retrieve the Query Builder after filtering the results: you can use `->toQuery()`.
The method internally use the first model of the collection and a `whereKey` comparison on the Collection models.```php// Retrieve all logged_in users$loggedInUsers = User::where('logged_in', true)->get();// Filter them using a Collection method or php filtering$nthUsers = $loggedInUsers->nth(3);// You can't do this on the collection$nthUsers->update(/* ... */);// But you can retrieve the Builder using ->toQuery()if ($nthUsers->isNotEmpty()) { $nthUsers->toQuery()->update(/* ... */);}```Tip given by [@RBilloir](https://twitter.com/RBilloir/status/1462529494917566465)### Custom castsYou can create custom casts to have Laravel automatically format your Eloquent model data. Here's an example that capitalises a user's name when it is retrieved or changed.```phpclass CapitalizeWordsCast implements CastsAttributes{ public function get($model, string $key, $value, array $attributes) { return ucwords($value); } public function set($model, string $key, $value, array $attributes) { return ucwords($value); }}class User extends Model{ protected $casts = [ 'name' => CapitalizeWordsCast::class, 'email' => 'string', ]; }```Tip given by [@mattkingshott](https://twitter.com/mattkingshott/status/1462828232206659586)### Order based on a related model's average or countDid you ever need to order based on a related model's average or count?
It's easy with Eloquent!```phppublic function bestBooks(){ Book::query() ->withAvg('ratings as average_rating', 'rating') ->orderByDesc('average_rating');}```Tip given by [@mmartin_joo](https://twitter.com/mmartin_joo/status/1466769691385335815)### Return transactions resultIf you have a DB transaction and want to return its result, there are at least two ways, see the example```php// 1. You can pass the parameter by reference$invoice = NULL;DB::transaction(function () use (&$invoice) { $invoice = Invoice::create(...); $invoice->items()->attach(...);})// 2. Or shorter: just return trasaction result$invoice = DB::transaction(function () { $invoice = Invoice::create(...); $invoice->items()->attach(...); return $invoice;});```### Remove several global scopes from queryWhen using Eloquent Global Scopes, you not only can use MULTIPLE scopes, but also remove certain scopes when you don't need them, by providing the array to `withoutGlobalScopes()`
[Link to docs](https://laravel.com/docs/8.x/eloquent#removing-global-scopes)```php// Remove all of the global scopes...User::withoutGlobalScopes()->get();// Remove some of the global scopes...User::withoutGlobalScopes([ FirstScope::class, SecondScope::class])->get();```### Order JSON column attributeWith Eloquent you can order results by a JSON column attribute```php// JSON column example:// bikes.settings = {"is_retired": false}$bikes = Bike::where('athlete_id', $this->athleteId) ->orderBy('name') ->orderByDesc('settings->is_retired') ->get();```Tip given by [@brbcoding](https://twitter.com/brbcoding/status/1473353537983856643)### Get single column's value from the first resultYou can use `value()` method to get single column's value from the first result of a query```php// Instead ofIntegration::where('name', 'foo')->first()->active;// You can useIntegration::where('name', 'foo')->value('active');// or this to throw an exception if no records foundIntegration::where('name', 'foo')->valueOrFail('active')';```Tip given by [@justsanjit](https://twitter.com/justsanjit/status/1475572530215796744)### Check if altered value changed keyEver wanted to know if the changes you've made to a model have altered the value for a key? No problem, simply reach for originalIsEquivalent.```php$user = User::first(); // ['name' => "John']$user->name = 'John';$user->originalIsEquivalent('name'); // true$user->name = 'David'; // Set directly$user->fill(['name' => 'David']); // Or set via fill$user->originalIsEquivalent('name'); // false```Tip given by [@mattkingshott](https://twitter.com/mattkingshott/status/1475843987181379599)### New way to define accessor and mutatorNew way to define attribute accessors and mutators in Laravel 8.77:```php// Before, two-method approachpublic function setTitleAttribute($value){ $this->attributes['title'] = strtolower($value);}public function getTitleAttribute($value){ return strtoupper($value);} // New approachprotected function title(): Attribute{ return new Attribute( get: fn ($value) => strtoupper($value), set: fn ($value) => strtolower($value),}```Tip given by [@Teacoders](https://twitter.com/Teacoders/status/1473697808456851466)### Another way to do accessors and mutatorsIn case you are going to use the same accessors and mutators in many models , You can use custom casts instead.Just create a `class` that implements `CastsAttributes` interface. The class should have two methods, the first is `get` to specify how models should be retrieved from the database and the second is `set` to specify how the the value will be stored in the database.```phpdiffForHumans(); } public function set($model, string $key, $value, array $attributes) { return Carbon::parse($value)->format('Y-m-d h:i:s'); }}```Then you can implement the cast in the model class.```php TimestampsCast::class, 'created_at' => TimestampsCast::class, ];}```Tip given by [@AhmedRezk](https://github.com/AhmedRezk59)### When searching for the first record, you can perform some actionsWhen searching for the first record, you want to perform some actions, when you don't find it. `firstOrFail()` throws a 404 Exception.
You can use `firstOr(function() {})` instead. Laravel got your covered```php$book = Book::whereCount('authors') ->orderBy('authors_count', 'DESC') ->having('modules_count', '>', 10) ->firstOr(function() { // THe Sky is the Limit ... // You can perform any action here });```Tip given by [@bhaidar](https://twitter.com/bhaidar/status/1487757487566639113/)### Directly convert created_at date to human readable formatDid you know you can directly convert created_at date to human readble format like 1 miniute ago, 1 month ago using diffForHumans() function. Laravel eloquent by default enables Carbon instance on created_at field.```php$post = Post::whereId($id)->first();$result = $post->created_at->diffForHumans();/* OUTPUT */// 1 Minutes ago, 2 Week ago etc..as per created time```Tip given by [@vishal__2931](https://twitter.com/vishal__2931/status/1488369014980038662)### Ordering by an Eloquent AccessorOrdering by an Eloquent Accessor! Yes, that's doable. Instead of ordering by the accessor on the DB level, we order by the accessor on the returned Collection.```phpclass User extends Model{ // ... protected $appends = ['full_name']; public function getFullNameAttribute() { return $this->attribute['first_name'] . ' ' . $this->attributes['last_name']; } // ..}``````phpclass UserController extends Controller{ // .. public function index() { $users = User::all(); // order by full_name desc $users->sortByDesc('full_name'); // or // order by full_name asc $users->sortBy('full_name'); // .. } // ..}````sortByDesc` and `sortBy` are methods on the CollectionTip given by [@bhaidar](https://twitter.com/bhaidar/status/1490671693618053123)### Check for specific model was created or foundIf you want to check for specific model was created or found, use `wasRecentlyCreated` model attribute.```php$user = User::create([ 'name' => 'Oussama',]);// return booleanreturn $user->wasRecentlyCreated;// true for recently created// false for found (already on you db)```Tip given by [@sky_0xs](https://twitter.com/sky_0xs/status/1491141790015320064)### Laravel Scout with database driverWith laravel v9 you can use Laravel Scout (Search) with database driver. No more where likes!```php$companies = Company::search(request()->get('search'))->paginate(15);```Tip given by [@magarrent](https://twitter.com/magarrent/status/1493221422675767302)### Make use of the value method on the query builderMake use of the `value` method on the query builder to execute a more efficient query when you only need to retrieve a single column.```php// Before (fetches all columns on the row)Statistic::where('user_id', 4)->first()->post_count;// After (fetches only `post_count`)Statistic::where('user_id', 4)->value('post_count');```Tip given by [@mattkingshott](https://twitter.com/mattkingshott/status/1493583444244410375)### Pass array to where methodLaravel you can pass an array to the where method.```php// Instead of thisJobPost::where('company', 'laravel') ->where('job_type', 'full time') ->get();// You can pass an arrayJobPost::where(['company' => 'laravel', 'job_type' => 'full time']) ->get();```Tip given by [@cosmeescobedo](https://twitter.com/cosmeescobedo/status/1495626752282234881)### Return the primary keys from models collectionDid you know `modelsKeys()` eloquent collection method? It returns the primary keys from models collection.```php$users = User::active()->limit(3)->get();$users->modelsKeys(); // [1, 2, 3]```Tip given by [@iamharis010](https://twitter.com/iamharis010/status/1495816807910891520)### Force Laravel to use eager loadingIf you want to prevent a lazy loading in your app, you only need to add following line to the `boot()` method in your `AppServiceProvider````phpModel::preventLazyLoading();```But, if you want to enable this feature only on your local development you can change above code on that:```phpModel::preventLazyLoading(!app()->isProduction());```Tip given by [@CatS0up](https://github.com/CatS0up)### Make all your models mass assignableIt is not a recommended approach for security reasons, but it is possible.When you want do this, you don't need to set an empty `$guarded` array for every model, like this:```phpprotected $guarded = [];```You can do it from a single place, just add following line to the `boot()` method in your `AppServiceProvider`:```phpModel::unguard();```Now, all your models are mass assignable.Tip given by [@CatS0up](https://github.com/CatS0up)### Hiding columns in select all statementsIf you use Laravel v8.78 and MySQL 8.0.23 and onwards, you can define choosen columns as "invisible". Columns which are define as `invisible` will be hidden from the `select *` statements.However, to do so, we must use a `invisible()` method in the migration, something like that:```phpuse Illuminate\Database\Schema\Blueprint;use Illuminate\Support\Facades\Schema;Schema::table('table', function (Blueprint $table) { $table->string('secret')->nullable()->invisible();});```That's it! This will make chosen column hidden from `select *` statement.Tip given by [@CatS0up](https://github.com/CatS0up)### JSON Where ClausesLaravel offers helpers to query JSON columns for databases that support them.
Currently, MySQL 5.7+, PostgreSQL, SQL Server 2016, and SQLite 3.9.0 (using the JSON1 extension)```php// To query a json column you can use the -> operator$users = User::query() ->where('preferences->dining->meal', 'salad') ->get();// You can check if a JSON array contains a set of values$users = User::query() ->whereJsonContains('options->languages', [ 'en', 'de' ]) ->get();// You can also query by the length a JSON array$users = User::query() ->whereJsonLength('options->languages', '>', 1) ->get();```Tip given by [@cosmeescobedo](https://twitter.com/cosmeescobedo/status/1509663119311663124)### Get all the column names for a table```phpDB::getSchemaBuilder()->getColumnListing('users');/*returns [ 'id', 'name', 'email', 'email_verified_at', 'password', 'remember_token', 'created_at', 'updated_at',]; */```Tip given by [@aaronlumsden](https://twitter.com/aaronlumsden/status/1511014229737881605)### Compare the values of two columnsYou can use `whereColumn` method to compare the values of two columns.```phpreturn Task::whereColumn('created_at', 'updated_at')->get();// pass a comparison operatorreturn Task::whereColumn('created_at', '>', 'updated_at')->get();```Tip given by [@iamgurmandeep](https://twitter.com/iamgurmandeep/status/1511673260353548294)### Accessor CachingAs of Laravel 9.6, if you have a computationally intensive accessor, you can use the shouldCache method.```phppublic function hash(): Attribute{ return Attribute::make( get: fn($value) => bcrypt(gzuncompress($value)), )->shouldCache();}```Tip given by [@cosmeescobedo](https://twitter.com/cosmeescobedo/status/1514304409563402244)### New scalar() methodIn Laravel 9.8.0, the `scalar()` method was added that allows you to retrieve the first column of the first row from the query result.```php// BeforeDB::selectOne("SELECT COUNT(CASE WHEN food = 'burger' THEN 1 END) AS burgers FROM menu_items;")->burgers// NowDB::scalar("SELECT COUNT(CASE WHEN food = 'burger' THEN 1 END) FROM menu_items;")```Tip given by [@justsanjit](https://twitter.com/justsanjit/status/1514550185837408265)### Select specific columnsTo select specific columns on a model you can use the select method -- or you can pass an array directly to the get method!```php// Select specified columns from all employees$employees = Employee::select(['name', 'title', 'email'])->get();// Select specified columns from all employees$employees = Employee::get(['name', 'title', 'email']);```Tip given by [@ecrmnn](https://twitter.com/ecrmnn/status/1516087672351203332)### Chain conditional clauses to the query without writing if-else statementsThe "when" helper in the query builder is🔥You can chain conditional clauses to the query without writing if-else statements.Makes your query very clear:```phpclass RatingSorter extends Sorter{ function execute(Builder $query) { $query ->selectRaw('AVG(product_ratings.rating) AS avg_rating') ->join('product_ratings', 'products.id', '=', 'product_ratings.product_id') ->groupBy('products.id'); ->when( $this->direction === SortDirections::Desc, fn () => $query->orderByDesc('avg_rating') fn () => $query->orderBy('avg_rating'), ); return $query; }}```Tip given by [@mmartin_joo](https://twitter.com/mmartin_joo/status/1521461317940350976)@endmarkdown