0

Morning,

il would like to use Eloquent to make this request but i have an error.

$ModelVars=Model1::with(['Model2' => function($query,$var){
                            return $query->where('field1', 'like', '%'.$var.'%');}])->get();

Could somebody help me. Thks in advance.

2
  • 1
    What's the error? Commented Oct 28, 2020 at 16:58
  • Tks @Jackowski, See below error : Too few arguments to function App\Http\Controllers\Achat\DemandeachatsController::App\Http\Controllers\Achat\{closure}(), 1 passed in ..\vendor\laravel\framework\src\Illuminate\Database\Query\Builder.php on line 431 and exactly 2 expected Commented Oct 28, 2020 at 17:10

1 Answer 1

1

Change your code to :

$ModelVars = Model1::with(['Model2' => function($query) use ($var){
    return $query->where('field1', 'like', '%'.$var.'%'); }])
->get();

use is not a function, it's part of the Closure syntax It simply makes the specified variables of the outer scope available inside the closure.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.