Nested "with()" laravel - laravel

I'm having issues with some relationships. I have the following situation
Product:
public function catalogues()
{
return $this->belongsToMany('App\Catalogue');
}
public function category()
{
return $this->belongsTo('App\Category');
}
Category:
public function products()
{
return $this->hasMany('App\Product');
}
Catalogue:
public function products()
{
return $this->belongsToMany('App\Product');
}
With a single query, I need to get all the categories that have products belonging to a certain catalogue. How can I achieve it?

Use whereHas():
Category::whereHas('products.catalogues', function($q) use($catalogueId) {
$q->where('catalogues.id', $catalogueId);
})
->get();
Or:
Category::whereHas('products', function($q) use($catalogueId) {
$q->whereHas('catalogues', function($q) use($catalogueId) {
$q->where('id', $catalogueId);
})
})
->get();

You need whereHas and
Category::whereHas('products')
->with(['products' => function($query) use ($id){
$query->with('catalogues' => function($query) use ($id){
$query->where('catalogues.id',$id);
});
}])
->get();
Hope this helps.

Related

Laravel where of Relationship from Relationship

I have easy relationships like this
Project Model:
public function milestones()
{
return $this->hasMany('App\Models\Milestone');
}
Milestone Model:
public function milestones()
{
return $this->hasMany('App\Models\Milestone');
}
Now I try to get Todo with id = 3. And it should only show Milestone where my Todo is inside. This is my solution at the moment (shows all Milestones inside project)
$query = Project::whereHas('milestones',function($query) use ($UserId){
$query->whereHas('todo', function ($query) use ($UserId){
$query->where('id',3);
});
})->with('milestones.todo',function($query){
$query->where('id',3);
})
->get();
How do I limit milestones to that one, where todo with id 3 is inside?
You'll need to be explicit with your eager load:
$query = Project
::with('milestones', function ($query) use ($UserId) {
$query
->with([
'todos' => function ($query) use ($UserId) {
$query->where('id', $UserId);
},
])
->whereHas('todos', function ($query) use ($UserId) {
$query->where('id', $UserId);
});
})
->whereHas('milestones', function ($query) use ($UserId) {
$query->whereHas('todo', function ($query) use ($UserId) {
$query->where('id', $UserId);
});
})
->get()

Laravel pass value from query to with() function

Here is my query:
$fix = Fixture::select('fixtures.*')
->whereDate('kickoff', '<', $date)
->with('competition')
->with('teamA')
->with(['teamA.standing' => function ($q) {
$q->whereColumn('standings.competition_id', '=', {{here i want the competition_id from competitions or fixtures table}} );
}])
->with('teamB')
->with(['teamB.standing' => function ($q) {
$q->whereColumn('standings.competition_id', '=', {{here i want the competition_id from competitions table or fixtures table}});
}])->get();
Code from Fixture model:
public function teamA()
{
return $this->belongsTo(Team::class,'team_a','id');;
}
public function teamB()
{
return $this->belongsTo(Team::class,'team_b','id');;
}
Code from Team model:
public function standing()
{
return $this->hasOne(Standing::class);;
}
Code from Standing model:
public function team(){
return $this->belongsTo(Team::class);
}
public function competition()
{
return $this->belongsTo(Competition::class);
}
Can someone help me to get this done or if I am doing it wrong what's the correct way?
->with(['teamA.standing' => function ($q) {
$q->whereColumn('standings.competition_id', '=', {{here i want the competition_id from competition table or fixtures table}} );
}])
Example:
Category::select('id', 'name')->whereHas('statuses', function($query) { $query(); })->with('courses')->get();

Property [columns] does not exist on the Eloquent builder instance

i'm getting this error while trying to add a subquery select to a query builder. this error occurs when i try to get the product count for a given category.
addSubSelect Macro
Builder::macro('addSubSelect', function ($column, $query) {
if (is_null($this->columns)) {
$this->select($this->from.'.*');
}
return $this->selectSub($query, $column);
});
Category.php
public function scopeWithProductCount($query){
$query->addSubSelect('products_count', function($query) {
$query->whereHas('product', function ($query) {
$query->select('id')
->from('products')
->whereColumn('category_id', 'category.id')
->count();
});
});
}
Category Controller
public function index()
{
$categories = $this->user()->store->categories()
->withProductsSum()
->get();
return response()->json($categories);
}
i changed the scopedWithProductCount method to this to try and get more debugging options but i end up with the same error
public function scopeWithProductCount($query){
$query->addSubSelect('', function($query) {
});
}
The columns property is in the Illuminate\Database\Query\Builder Class and not Illuminate\Database\Eloquent\Builder. Your $this will refer to the Eloquent\Builder reference and not the Query\Builder.
So try changing your macro like this:
addSubSelect Macro
Builder::macro('addSubSelect', function ($column, $query) {
if (is_null($this->getQuery()->columns)) {
$this->select($this->from.'.*');
}
return $this->selectSub($query, $column);
});
Write join and the name of the table before the field:
public function scopeWithProductCount($query){
$query->addSubSelect('products_count', function($query) {
$query->whereHas('product', function ($query) {
$query->select('products.id')
->from('products')
->join('categories', 'categories.id', '=', 'products.category_id')
->count();
});
});
}

How to show Laravel model with belongsToMany connection if has specified connection?

I have this product model:
public function categories() {
return $this->belongsToMany('App\Category', 'product_categories');
}
And in my controller:
public function search(Request $request) {
return Product::with([
'categories' => function($categories) use ($request) {
// ...
}
]);
}
If I try to use $request in the categories function it's search only in categories, but shows all products.
How do I show only that products whitch has defined categories in $request->category_id?
you may use the whereHas keyword in laravel:
public function search(Request $request) {
return Product::with('categories')
->whereHas('categories', function ($query) use ($request){
$query->where('category_id', $request->category_id);
})->get();
}
Here is the docs
You can search it in following way:
public function search(Request $request) {
return Product::with('categories')
->whereHas('categories', function ($q) use (request) {
$q->where('id', $request->category_id);
});
}

Scope always get the same relation

I want to make a relation ship bt condition...
I'm try like this:
Model:
public function page_link()
{
return $this->belongsTo(Link::class, 'page_id', 'user_id')->inRandomOrder()->Select('links.id', 'links.title', 'links.photo', 'links.country', 'links.friendly_url', 'links.clicks', 'links.description', 'links.suggestions', 'links.count_comments', 'links.url', 'links.shares', 'links.page_id', 'links.tag_id', 'links.created_at')->where('sponsored', 0)->where('scheduled', 0)>where('status', 1)->take(3)->orderBy('id','desc');
}
public function user_share()
{
return $this->belongsTo(Share::class, 'user_id', 'user_id')->inRandomOrder()->Select('id', 'link_id', 'user_id', 'shared_in', 'content', 'created_at')->take(3)->orderBy('id', 'desc')->where('type', '=', 0);
}
public function user(){
return $this->belongsTo(User::class, 'user_id', 'id');
}
public function scopeProfile($query)
{
return $query
->when($this->type == 0, function($q)
{
return $q->with('user_share.link.page')->with('user')->with('user_share.link.tag');
})
->when($this->type == 1, function($q)
{
return $q->with('page_link');
})
->when($this->type == 2, function($q)
{
return $q->with('group_link');
});
}
Controller:
$feed = Feed::Profile()->where('follower_id', auth()->user()->id)
->take(10)
->get();
But ALL the results, even the type 1 or 2 returns "user_links" relation. I don't know whats happening.
Someone can help me?

Resources