2

I'm trying to call using with() method in Laravel Eloquent ORM, but getting the following error.

Argument 1 passed to App\Http\Controllers\DashboardController::App\Http\Controllers\{closure}() must be an instance of Illuminate\Database\Eloquent\Builder, instance of Illuminate\Database\Eloquent\Relations\HasMany given

I'm using the latest version of Laravel 6. Any ideas what might have caused this?

Controller

class DashboardController extends Controller
{
    public function __construct()
    {
        $this->middleware('auth:api');
    }

    public function formIndex(Request $request)
    {
        $id = auth()->user()->id;
        $item = Groupe::find($id)->with(
            [
                'etudiants' => function (Builder $query) {
                    $query->select('id');
                }
            ]
        )->first();

        return $item;
    }
}

Model

class Groupe extends Authenticatable implements JWTSubject
{
    public function etudiants()
    {
        return $this->hasMany('App\Etudiant');
    }
}

3 Answers 3

5

The error comes from the type hint you put on the $query variable, as the error message said the object that gets passed in there is a relationship not a raw Query Builder. Just remove the type hint. Also ::find() executes a query, so you're be executing 2 queries, use the query below instead

Groupe::where('id', $id)->with(['etudiants' => function ($query) {
        $query->select('id');
    }])->first();

Additionally, you don't need to use the callback syntax to only eager load certain columns, the callback syntax is for putting contraints on which records get returned. Try this instead.

Groupe::where('id', $id)->with('etudiants:id,name,email')->first();

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

4 Comments

make sure to also select the foreign key for the relationship
Thanks a lot that worked, with adding the foreign key to the query
And Hell Broke loose because of that one type hint xD .. thanks a lot it really helped, never would've guessed it in million years
The Laravel Documentation is giving exact this example. The documentation is wrong. Indeed without typehint it works.
0

But what do you want return? groupe->students[]?? you can use

    $item = Groupe::where('id',$id)->with('etudiants')->first();

2 Comments

Yes I want to return that students data, but with specified columns, like name, email not all fields
ok so you can use protected $hidden = ['id','created_at','others'] in the model
0

le probleme viens tous simplement de limport du builder utilise ceci "use Illuminate\Contracts\Database\Eloquent\Builder;"

1 Comment

Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.

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.