eloquent
Unset/Remove relation object from Laravel Eloquent collection
In Laravel 5.6.25, you can use unsetRelation(): $product->unsetRelation(‘merchant’)->unsetRelation(‘picture’); Before that: $relations = $product->getRelations(); unset($relations[‘merchant’], $relations[‘picture’]); $product->setRelations($relations);
Group By Eloquent ORM
Eloquent uses the query builder internally, so you can do: $users = User::orderBy(‘name’, ‘desc’) ->groupBy(‘count’) ->having(‘count’, ‘>’, 100) ->get();
Laravel migration with SQLite ‘Cannot add a NOT NULL column with default value NULL’
It looks like this is a SQLite oddity. According to a Laracast forum thread about the same issue: When adding a table from scratch, you can specify NOT NULL. However, you can’t do this when adding a column. SQLite’s specification says you have to have a default for this, which is a poor choice. Looking … Read more
Laravel eloquent search on fields of related model
That’s where whereHas comes in handy: $user = User::with(‘Profile’)->where(‘status’, 1)->whereHas(‘Profile’, function($q){ $q->where(‘gender’, ‘Male’); })->get(); Basically it adds the condition that the user needs to have a profile with gender = Male