samedi 28 septembre 2019

Syntax for a request in laravel (2 tables)

I have a table named Payments and another table named Students with the field name.

In my Controller Payment, I want to retrieve the student names.

I have this:

public function index(Request $request)
    {   
        $user = $request->user();
        $payments = Payment::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('name', 'like', '%'.$request->input('search').'%');
        })
        ->paginate(5);



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

How do I join the students table?

payment::whereHas('students'

I don't know where I have to put this line

Thank you for your help.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire