vendredi 27 septembre 2019

Hasrole with 2 tables

I have a model named Student with the field name , here is the function, which is correct.

public function index(Request $request)
    {   
        $user = $request->user();
        $students = Student::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.students.index', compact('students'))
        ->with('display_search', $user->hasRole('admin'));
    }

Now, in my model Payment I have a request with 2 tables (Payment & Student).

Here is my function index()

public function index(Request $req) 
    {
    if ($req->search == "") {
        $payments = Payment::paginate(5);
        return view('admin.payments.index', compact('payments'));

    } else {

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

        $payments = payment::whereHas('students', function($query) use($req) {
            $query->where('name', 'like', '%' . $req->search . '%');
        })->paginate(5);
        return view('admin.payments.index', compact('payments'));

        }

    }

My problem is I want to adapt my function for connect me with an user.

I have this for now ???

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) {

how to include this:

$payments = payment :: whereHas ('students', function ($ query) use ($ req) {
$query-> where ('name', 'like', '%'. $ req-> search. '%');


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

Aucun commentaire:

Enregistrer un commentaire