Northify
Northify

Reputation: 391

Laravel laratables how to use id in enclosure

I'm using the Laratables package. I have a function to being in the data for the datatable but I need to only query for a user by ID.

public function data($id) 
{

    return  Laratables::recordsOf(User::class, function($query)
    {
        return $query->where('manager_id', $id)->where('status', 'Enabled');
    });

}

I get an error:

Undefined variable: id

I'm not sure how I can use the id variable inside this function.

Upvotes: 0

Views: 275

Answers (1)

Salman Zafar
Salman Zafar

Reputation: 4035

If you are using php < 7.4 then you can do something like below:

public function data($id) 
    {

        return  Laratables::recordsOf(User::class, function($query) use ($id)
        {

            return $query->where('manager_id', $id)->where('status', 'Enabled');
        });

    }

and if your are using php 7.4 then you can use short closure/arrow function for this.

   public function data($id) 
        {

            return  Laratables::recordsOf(User::class, fn($query) =>

             $query->where('manager_id', $id)->where('status', 'Enabled')
           );

        }

to read more anonymous function visit this or this

Hope this helps.

Thanks

Upvotes: 1

Related Questions