codekerala / spa-laravel-vuejs

Single Page Application with Laravel 5.3 and Vue.js 2.1.x
https://codekerala.com
153 stars 82 forks source link

explanation about filterPaginateOrder trait #5

Open t0n1zz opened 6 years ago

t0n1zz commented 6 years ago

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
            ]);
    }
anishdcruz commented 6 years ago

I think u have to use WhereHas method

t0n1zz commented 6 years ago

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

anishdcruz commented 6 years ago

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.

t0n1zz commented 6 years ago

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.