Remove relationship in return json from Laravel's Datatables package

0

I have the following code snippet using laravel and the Datatables package

$products = Product::with('enterprise')
                   ->select(['id', 'enterprise_id', 'name'])
                   ->whereIn('enterprise_id', [1, 2]);

return Datatables::of($products)
       ->editColumn('enterprise_id', function($product){
           return $product->enterprise->name;
       })
       ->make();

And I have the following json as a return:

[
    "12",
    "Nome empresa",
    "Nome do produto",
    {
        "id": 1,
        "client_id": 1,
        "name": "Nome empresa",
        "created_at": "2016-08-25 16:38:24",
        "updated_at": "2016-08-25 16:38:24"
    }
]

How to remove that json snippet where the relationship appears? What I would like to take away is this part of json:

{
    "id": 1,
    "client_id": 1,
    "name": "Nome empresa",
    "created_at": "2016-08-25 16:38:24",
    "updated_at": "2016-08-25 16:38:24"
}

I have already tried using the removeColumn feature without success. And I've also tried to add a new column nome_empresa and removing the column enterprise_id but also without success.

The result I expected to receive in json would be:

[
    "12",
    "Nome empresa",
    "Nome do produto",
]
    
asked by anonymous 26.08.2016 / 17:29

1 answer

0

To solve this problem, just give removeColumn in the relationship name.

In my case in model Product I had the following method to do the relationship:

public function enterprise(){
    return $this->belongsTo('App\Models\Enterprise');
}

Then I removed the column as follows:

->removeColumn('enterprise')

Okay, that solved the problem and now json came as he needed it.

    
26.08.2016 / 18:16