My http://localhost:8888/VGL/public/category/18?sty=3
When dd($request->sty); equal 3
however I put $request->sty in the whereHas is
Undefined variable: request
public function show(Request $request, $id)
{
$products = Product::with('features')
->whereHas('features', function ($q) {
return $q->where('id', $request->sty);
})
->where('category_id',17)
->get();
}
Try this
If you want to use any variable inside the where closure then you have to pass that variable inside the use($variable)
public function show(Request $request, $id)
{
$products = Product::with('features')
->whereHas('features', function ($q) use($request) {
return $q->where('id', $request->sty);
})
->where('category_id',17)
->get();
}
Related
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()
Here is my Macro for relation search in Laravel Eloquent.
Bellow is my Macro in AppServiceProvider.php
Builder::macro('whereLike', function ($attributes, string $searchTerm) {
$this->where(function (Builder $query) use ($attributes, $searchTerm) {
foreach (Arr::wrap($attributes) as $attribute) {
$query->when(
str_contains($attribute, '.'),
function (Builder $query) use ($attribute, $searchTerm) {
[$relationName, $relationAttribute] = explode('.', $attribute);
$query->orWhereHas($relationName, function (Builder $query) use
($relationAttribute, $searchTerm) {
$query->where($relationAttribute, 'LIKE', "%{$searchTerm}%");
});
},
function (Builder $query) use ($attribute, $searchTerm) {
$query->orWhere($attribute, 'LIKE', "%{$searchTerm}%");
}
);
}
});
return $this;
});
Bellow is my search Query in MemberController.php
public function getMembers(request $request){
$members = Member::with('products')->where('groupId', 1)->whereLike(['firstname',
'lastname', 'products.name'], $request->searchValue)->paginate(10);
dd($members);
}
The "whereLike" is invoked from the appServiceProvider.php as a macro in there thus where the error occur if i remove the members.name the macro executes perfectly.
I get an Error with orWhereHas
"Call to undefined method Illuminate\Database\Query\Builder::orWhereHas()"
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();
});
});
}
I have a problem with my function index() concerning a request. In fact, I have to adapt my old function with a recent.
For information, my function index() (old) below was written like this and it works !!
public function index(Request $req)
{
if ($req->search == "") {
$retours = Retour::join('eleves', 'retours.fk_eleve', '=', 'eleves.id')->orderBy('eleves.nom', 'asc')->select('retours.*')->paginate(5);
return view('admin.retours.index', compact('retours'));
} else {
$validated = $req->validate([
'search' => 'alpha',
]);
$retours = Retour::join('eleves', 'retours.fk_eleve', '=', 'eleves.id')->where('eleves.nom','like', '%' . $req->search . '%')->orderBy('eleves.nom', 'asc')->select('retours.*')->paginate(5);
return view('admin.retours.index', compact('retours'));
}
}
Now, I have to change a party of code and I must to adapt my request:
$retours = Retour::join('eleves', 'retours.fk_eleve', '=', 'eleves.id')->where('eleves.nom','like', '%' . $req->search . '%')->orderBy('eleves.nom', 'asc')->select('retours.*')->paginate(5);
Here is an idea of my new code, my problem is that the search bar doesn't filter ? ^^
public function index(Request $request)
{
$user = $request->user();
$retours = Retour::query()
->when($user->hasRole('admin') !== true, function (Builder $query) use ($user) {
$query->where('email', $user->email);
})
->when($request->has('search'), function (Builder $query) use ($request) {
$query->join('eleves', 'retours.fk_eleve', '=', 'eleves.id')->orderBy('eleves.nom', 'asc')->select('retours.*');
})
->paginate(5);
return view('admin.retours.index', compact('retours'))
->with('display_search', $user->hasRole('admin'));
}
My problem is perhaps here ?
->when($request->has('search'), function (Builder $query) use ($request) {
$query->join('eleves', 'retours.fk_eleve', '=', 'eleves.id')->orderBy('eleves.nom', 'asc')->select('retours.*');
})
->paginate(5);
Do have you an idea please?
Thank you
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);
});
}