spa-laravel-vuejs icon indicating copy to clipboard operation
spa-laravel-vuejs copied to clipboard

explanation about filterPaginateOrder trait

Open t0n1zz opened this issue 6 years ago • 4 comments

hi, i love your project and it works very well and match what i need for an spa project with laravel. But i don't quite get it how everything works... and now i having a hard time to using it with json data that returned like this

{
  "model": {
    "current_page": 1,
    "data": [
      {
        "id": 8,
        "id_cu": 0,
        "id_pus": 1,
        "name": "test1",
        "username": "test17",
        "gambar": "",
        "status": 1,
        "created_at": "2018-02-27 06:37:10",
        "c_u": null,
        "pus": {
          "id": 1,
          "name": "Puskopdit BKCU Kalimantan"
        },
        "roles": [
          {
            "name": "CU Akses Penuh",
            "pivot": {
              "model_id": 8,
              "role_id": 2
            }
          }
        ]
      },
      {
        "id": 7,
        "id_cu": 0,
        "id_pus": 1,
        "name": "test1",
        "username": "test16",
        "gambar": "",
        "status": 1,
        "created_at": "2018-02-27 06:36:06",
        "c_u": null,
        "pus": {
          "id": 1,
          "name": "Puskopdit BKCU Kalimantan"
        },
        "roles": [
          
        ]
      }
    ],
    "from": 1,
    "last_page": 4,
    "next_page_url": "https://bkcuvue.dev/api/v1/user?page=2",
    "path": "https://bkcuvue.dev/api/v1/user",
    "per_page": "2",
    "prev_page_url": null,
    "to": 2,
    "total": 8
  }
}

so according to the json data returned above i want to user roles for search_column, i tried to use roles.name but it is not working so any idea to use those?

my controller is like this

public function index()
	{
    	$table_data = User::with(array('CU','pus','roles' => function($query){
				$query->select('name');
			}))->select('id','id_cu','id_pus','name','username','gambar','status','created_at')->filterPaginateOrder();

    	return response()
			->json([
				'model' => $table_data
			]);
	}

t0n1zz avatar Feb 28 '18 12:02 t0n1zz

I think u have to use WhereHas method

anishdcruz avatar Feb 28 '18 12:02 anishdcruz

whereHas? care to explain more? i'm still not really understand about whole eloquent and collection thing works in laravel...

and for more information roles table is from spatie permission package and i just happen to find out i can get those related roles to the user model by using with like that

t0n1zz avatar Feb 28 '18 12:02 t0n1zz

In order to filter the results by relationship you have to use whereHas method.

For example:

User::with(['a', 'b', 'etc'])
  ->whereHas('roles', function($query) {
    $query->where('name', '=', 'admin'); // any search function
})->get();

The above code is not tested.

anishdcruz avatar Feb 28 '18 13:02 anishdcruz

hmm but i'm in situation where i don't know which roles name will be, in fact i trying to get those roles name. so i am calling user model and roles that have relation with those user model.... i trying to make a list of table with user model (name, username, etc) with roles that those user model have.

t0n1zz avatar Feb 28 '18 13:02 t0n1zz