eloquent
How to search case insensitive in Eloquent model [duplicate]
If you are using Postgres, you don’t need to use UPPER, just use ilike as the comparator and it will do a case-insensitive comparison. Model::where(‘column’, ‘ilike’, ‘%’ . $value . ‘%’) You do need the % signs to signify the substring you’re searching for.
Change default primary key in Eloquent
Yes class User extends Eloquent { protected $primaryKey = ‘admin_id’; }
How to use Eloquent ORM without Laravel?
Yes you can. A while ago Dan Horrigan released a package called Capsule for Laravel 4 which allowed Eloquent to be used independently and with minimal setup. The package itself has been merged with the L4 core so you no longer need to use the package. If you refer to the illuminate/database repository there is … Read more
Laravel Eloquent Select CASE?
Move your raw() call inside the SELECT statement: ->select(‘shares.id AS share_id’, ‘users.id AS user_id’, ‘shares.connected_user_id’, ‘shares.original_language_id’, ‘shares.image’, ‘users.first_name’, ‘users.last_name’, ‘users.email’, ‘locations.city’, ‘provinces.name’, ‘countries.code’, ‘locations.lat’, ‘locations.lng’, ‘shares.created_at’, DB::raw(‘(CASE WHEN users.id = ‘ . $user . ‘ THEN 1 ELSE 0 END) AS is_user’) ) ->orderBy(‘shares.created_at’, ‘desc’) From: https://laravel.com/docs/5.4/queries#raw-expressions
Laravel belongsToMany exclude pivot table
Add pivot to your $hidden property’s array in your model(s). class Badge extends Eloquent { protected $hidden = [‘pivot’]; public function users() { return $this->belongsToMany(‘User’, ‘users_badges’); } } And same with your User model class User extends Eloquent { protected $hidden = [‘pivot’]; public function badges() { return $this->belongsToMany(‘Badge’, ‘users_badges’); } }
Dynamically hide certain columns when returning an Eloquent object as JSON?
$model->getHidden(); $model->setHidden(array $columns); $model->setVisible(array $columns);
Laravel Eloquent ORM – Many to Many Delete Pivot Table Values left over
The detach method is used to release a relationship from the pivot table, whilst delete will delete the model record itself i.e. the record in the reviews table. My understanding is that delete won’t trigger the detach implicitly. You can use model events to trigger a cleanup of the pivot table, though, using something like: … Read more
How to alias the name of a column in Eloquent
Simplest way to do this would be to add the fields you need to the get() method and alias the ones you want to rename there. Products::where(“actice”, “=”, true) ->joinWithTags ->get([‘tags.name AS tag_name’, ‘products.*’]) ->toArray();