mercredi 28 février 2018

Add data into laravel object

so i have this kind of class from my user controller

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

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

it is using trait called filterPaginateOrder and the end result that i receive are:

{
  "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"
        }
      },
      {
        "id": 1,
        "id_cu": 0,
        "id_pus": 1,
        "name": "tony",
        "username": "t0n1zz",
        "gambar": null,
        "status": 1,
        "created_at": "2017-01-01 11:11:11",
        "c_u": null,
        "pus": {
          "id": 1,
          "name": "Puskopdit BKCU Kalimantan"
        }
      }
    ],
    "from": 1,
    "last_page": 1,
    "next_page_url": null,
    "path": "https://bkcuvue.dev/api/v1/user",
    "per_page": "2",
    "prev_page_url": null,
    "to": 2,
    "total": 2
  }
}

so what if i want to add some data into it? inside data array for each array i want to add "role":"master" how do i do that?

i tried $table_data->push('role','master') and $table_data->put('role','master') from reading about collection (those query in laravel return collection right?) but none of those code working...



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire