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’); } }

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