Your mixing query builder and eloquent which is confusing as many functions have the same name.
With eloquent you pass an array into your get(), first() or list() to retrieve the columns you need.
firstOrFail(['id']); continue adding as you need.
I have many to many relation like this:
On Campaign model:
public function categories() {
return $this->belongsToMany('App\Categories_reseller', 'campaigns_categories', 'campaign_id', 'category_id')->withTimestamps();
}
On Categories_reseller model:
public function campaigns() {
return $this->belongsToMany('App\Campaigns', 'campaigns_categories', 'category_id', 'campaign_id')->withTimestamps();
}
When I do this I receive categories collection:
$campaign = \App\Campaign::getByToken($token)
->with('categories')
->firstOrFail();
When I do this, I receive empty collection in categories items relation:
$campaign = \App\Campaign::getByToken($token)
->select('id') // campaign ID
->with('categories')
->firstOrFail();
The getByToken function is a scope one:
public function scopeGetByToken($query, $token) {
return $query->where('token', $token)->where('status', 1);
}
Any Idea what I'm doing wrong? Is it not possible to select specific values from campaigns table when using "with()"?
Thanks
Your mixing query builder and eloquent which is confusing as many functions have the same name.
With eloquent you pass an array into your get(), first() or list() to retrieve the columns you need.
firstOrFail(['id']); continue adding as you need.
Please or to participate in this conversation.