WhereHas not working, in Has Many Relationsip - laravel

Im try to using where to show data is_default=1 in relationship using whereHas like this code
$data = $this->user->with('profile', 'account')
->whereHas('account', function ($query) use ($id) {
return $query->where('is_default', 1);
})
->where('id', $id)
->first();
but my return still showing all data of account, even i set where is_default=1
how i can solved this problem, please help me

This should work.
$data = $this->user->with('profile')->where('id', $id)
->whereHas('account', function($q) use ($id){
return $q->where('is_default', 1);
})->first();

I've been solved this problem using this code
$data = $this->user->with(['profile', 'account_default' => function ($q) use ($id) {
$q->where('is_default', 1);
}])
->where('id', $id)
->first();

Related

retrieve simple products and product_variation with corcel

In larvel I'm using corcel and with the help of this comment I'm trying to retrieve only simple products and variations of variable products.
$type = array('product_variation','product');
$status = 'publish';
$posts =
\Corcel\Model\Post::
type($type)->
whereHas('taxonomies', function($q) {
$q->where('taxonomy', 'product_type')
->whereHas('term', function($q) {
$q->where('slug', 'simple');
})
// since variations have no product_type taxonomy, then
->orwhere('taxonomy','<>', 'product_type');
})->
status($status)->
latest()->
limit(500)->
get();
return $posts;
but it only returns product_variation(s), and no simple product. can some one please explain my wrong doing?
i had the same problem, i realized that i can use doesntHave
$posts = \Corcel\Model\Post::status('publish')
->whereIn('post_type', ['product_variation','product'] )
->doesntHave('taxonomies')
->orWhereHas('taxonomies', function ($query) {
$query->whereIn('taxonomy',['product_type'])
->whereHas('term', function($q) {
$q->where('slug', 'simple');
});
})
->latest()
->limit(50)
->get();
return $posts;

Laravel 8 - How to use where conditions for relation's column

hello friends can you help me to get the contents of $filter? I want to run where which is where the target column is in the relation array
$filter = $request->get('Biogear');
$data = DetailBarang::with(['barang' => function ($q) use ($filter) {
$q->where('brand', '=', $filter);
}])->get();
return response()->json($data);
you can try this
$filter = $request->get('Biogear');
$data = DetailBarang::with('barang')->whereHas('barang',function (Illuminate\Database\Eloquent\Builder $q) use ($filter) {
$q->where('brand', '=', $filter);
})->get();
return response()->json($data);

Query relationship inside the relationship in Laravel Eloquent

I am trying to get the news of a writer according to date passed. I have written function so far:
public static function getNewsByAuthorByDate($id, $year, $limit = 1){
return User::where('id', $id)->has('news')
->with(['news' => function($q) use(&$limit, &$year) {
$q->where('created_at', 'like', '%' . $year . '%')->latest()->limit($limit);
}])
->first();
}
But the news[] is null. Is it possible to get writer news according to date or do I have to try another way?
you can use whereYear
public static function getNewsByAuthorByDate($id, $year, $limit = 1){
return User::where('id', $id)->has('news')
->with(['news' => function($q) use(&$limit, &$year) {
$q->whereYear('created_at',$year)->latest()->limit($limit);
}])
->first();
}
and to use limit with eager loading you should use the package eloquent-eager-limit
According to the documentation:
The limit and take query builder methods may not be used when constraining eager loads.
Also, You don't need to use has method, the news would be empty if the user has no news:
return User::with(['news' => function($q) use($year) {
$q->whereYear('created_at', $year)->latest();
}])
->find($id);

Laravel Eloquent - Having trouble querying relationship with ownership

I want to get all categories and within those categories, only items that belong to the logged in user. If there are none, the category should still be there.
This is what I tried. Still getting items from other users.
$categories = Category::parents()
->with(['lineItems' => function ($query) use($id) {
$query->where('user_id', $id);
}])->get();
Haven't been able to find anything that works. (using laravel 5.7)
Relationships
Category
public function lineItems()
{
return $this->hasMany(LineItem::class);
}
LineItems
public function category()
{
return $this->belongsTo(Category::class);
}
public function user()
{
return $this->belongsTo(User::class);
}
I'm not sure where the ::parents() method is coming from (I can't find documentation for it anywhere; is it something you wrote?), but it seems like
$categories = Category::all()
->with(['lineItems' => function ($query) use ($id) {
$query->where('user_id', $id);
}])->get();
might work?
So, Travis's question made me think about my loop. My with was affecting my top level categories which left its children's line items without the where user. There's probably a more elegant method of doing this but this is what ended up working.
This queries the child's line items and the grandchild's line items.
$categories = Category::parents()->ordered()
->with(['children.lineItems' => function($q) use($id) {
$q->where('user_id', '=', $id);
}, 'children.children.lineItems' => function($q) use($id) {
$q->where('user_id', '=', $id);
}])->get();
Try this:
$categories = Category::parents()
->whereHas('lineItems', function ($query) use($id) {
$query->where('user_id', $id);
})->get();

Laravel 4 Eager Loading constraints

I want to get all Items (topics) WITH their comments, if comments user_id = $id. I try something like this, but it isn't working. So if Item hasn't got any comment with user_id = $id, then I don't need this Item.
In DiscussionsItem model I have methode:
public function discussionsComments() {
return $this->hasMany('DiscussionsComment', 'discussionsitem_id');
}
My query in controller is like this:
$items = DiscussionsItem::whereBrandId($brand_id)
->whereBrandCountryId($country_id)
->with(['discussionsComments' => function($query) use ($id) {
$query->where('user_id', '=', $id);
}])
->whereHas('discussionsComments', function($query) use ($id) {
$query->where('user_id', '=', $id);
})
->with(['views' => function($query) use ($id) {
$query->where('user_id', $id)->count();
}])
->orderBy('created_at', 'DESC')->get();
My problem is that I get items with comments, where comments user_id != $id.
P.S. I need to take 5 comments, but I cant imagine how to do that, because ->take(5) in my eager load is not working.
You can do a custom scope, or just limit the amount returned by your relation:
public function discussionsComments() {
return $this->hasMany('DiscussionsComment', 'discussionsitem_id')
->latest()
->take(5);
}

Resources