how to write function in relationship using eloquent / etc with laravel?

0

Issue

$data = Program::with('peserta', function ($query) {
                return $query->peserta()->whereBetween('tanggal', array($request->from, $request->until));
            })->get();

this code return error :

mb_strpos() expects parameter 1 to be string, object given",
exception: "ErrorException".

Help. thanks

Solution

Closure function inside with() must be array and remove that return. Example in laravel doc

$data = Program::with(['peserta' => function ($query) use($request) {
                $query->whereBetween('tanggal', array($request->from, $request->until));
            }])->get();

Answered By – BABAK ASHRAFI

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave A Reply

Your email address will not be published.

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More