How can I make this conditions with eloquent? I want all records with product and category table whose url_name equals to $variable_name.
Here is the code:
\App\Product::with(['category' => function ($query) { $query->whereHas('url_name','Summer-Collection'); }])->get();
$variable_namenot being defined? If so you will just need to adduse ($variable_name)after the function parameters to put it within scope. If not, please add more detail to your question. Or even if that is the problem, include it in your question.Product::with(['category' => fn($q) => $q->whereHas('url_name', $variable_name)])->get();No need to import variables from parent scope with arrow functions.