In login model I have implement relation with picture table
function picture () {
return $this->hasOne('App\Picture');
}
Now I want data where Picture.picture_status = 1 and User.user_status = 1
Login::with('picture')->where('picture_status', 1)->where('user_status',1);
but where condition is not working with picture table, how can i implement and condition on both table
The only difference between hasOne and belongsTo is where the foreign key column is located. Let's say you have two entities: User and an Account. In short hasOne and belongsTo are inverses of one another - if one record belongTo the other, the other hasOne of the first.
Forum Querying a model using conditions in relationship $banners = \App\Banner::where('visible', 1) ->where('up_at','<=', $today) ->where('down_at','>=', $today) ->with(['categories' => function($query) use($category->id) { $query->where('category_id', '=', $category->id); } ]) ->get();
$categoryOne = DB::table('posts') ->where('category_id', 1); $categoryTwo = DB::table('posts') ->where('category_id', 2) ->union($categoryOne) ->get();
BelongsTo is a inverse of HasOne. We can define the inverse of a hasOne relationship using the belongsTo method. Take simple example with User and Phone models. I'm giving hasOne relation from User to Phone. class User extends Model { /** * Get the phone record associated with the user.
This should do it:
Login::with(['picture' => function ($query) {
$query->where('picture_status', 1)->where('user_status',1);
}])->get();
Sometimes you may wish to eager load a relationship, but also specify additional query constraints for the eager loading query https://laravel.com/docs/5.2/eloquent-relationships#constraining-eager-loads
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With