Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

where condition with HasOne laravel

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

like image 938
Harman Avatar asked Jul 14 '16 09:07

Harman


People also ask

What is difference between belongsTo and hasOne laravel?

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.

How do I apply relationship conditions in laravel?

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();

Where condition fetch data from database in laravel?

$categoryOne = DB::table('posts') ->where('category_id', 1); $categoryTwo = DB::table('posts') ->where('category_id', 2) ->union($categoryOne) ->get();

What is belongsTo in laravel?

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.


1 Answers

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

like image 153
ClearBoth Avatar answered Oct 12 '22 10:10

ClearBoth