lundi 19 août 2019

Need to adapt my function index() in Laravel

I need to adapt a function but I am stuck...

My function index() is the following: (It works, but I have to change)

public function index(Request $req)
    {
    if ($req->search == "") {
        $formers = Former::paginate(5);

        $formersIdsDown = Course::where('date_seance', "<=" , Carbon::now())->pluck('fk_former')->toArray();
        return view('admin.formers.index', compact('formers', 'formersIdsDown'));

    } else {

        $validated = $req->validate([
            'search' => 'alpha', 
        ]);

        $formers = Former::where('nom', 'LIKE', '%' . $validated['search'] . '%')->paginate(5);
        $formers->appends($req->only('search'));


         }
    }

My problem is that I don't know how to adapt this line below on my new function;

formersIdsDown = Course::where('date_seance', "<=" , Carbon::now())->pluck('fk_former')->toArray();
        return view('admin.formers.index', compact('formers', 'formersIdsDown'));

Here is, my new code...

public function index(Request $request)
    {   
        $user = $request->user();

        $formers = Former::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->where('nom', 'like', '%'.$request->input('search').'%');
        })
        ->paginate(5);

    return view('admin.formers.index', compact('formers'))
        ->with('display_search', $user->hasRole('admin'));
    }

I adapted, except this line below... Do know you where I have to integrate?

formersIdsDown = Course::where('date_seance', "<=" , Carbon::now())->pluck('fk_former')->toArray();
        return view('admin.formers.index', compact('formers', 'formersIdsDown'));

Thank you



from Newest questions tagged laravel-5 - Stack Overflow https://ift.tt/2ZeFVIV
via IFTTT

Aucun commentaire:

Enregistrer un commentaire