php - Laravel: Fetch with where filter on "with" -
php - Laravel: Fetch with where filter on "with" -
in laravel can fetch related database entry in 1 query with method this:
$customer = customer::with('user')->where([ 'id' =>'1' ])->first();
which homecoming client , it's related user object. possible apply filter item? thought possible:
$customer = customer::with('user')->where([ 'id' =>'1', 'users.status' => 'activated' ])->first();
but not seem work, tries find column "users.status" instead of column status in users table.
you can utilize eager loading constraints. allows perform queries on related models.
like this:
$customer = customer::with(['user' => function($query){ $query->where('status', 'activated'); //in user table }])->where('id', '1')->first(); //in client table
you can read eager load constaints part more informations:
http://laravel.com/docs/4.2/eloquent#eager-loading
php laravel eloquent
Comments
Post a Comment