Reputation: 1180
As the tittle said, Here is my Controller
$book = Data::where('userId','1')->paginate(3);
return response()->json($book);
And get the json data like this:
data:[{id: 1, userId: 1, vendorId: 1, name: "Alfreda Berge PhD", phone: "1-850-813-5950 x96169",…},…]
from:1
last_page:2
next_page_url: "http:/localhost/XX/public/user/book/list/1?page=2"
perpage:4
prev_page_url:null
to:4
total:5
// if I want to add a new column and value here ,what should I do?
I tried to do this :
$book = Data::where('userId','1')->paginate(3);
$book->printWord = 'Hellow!';
return response()->json($book);
But, It seem will remove the column printWord
.
Any idea?
Upvotes: 20
Views: 33054
Reputation: 910
$book = Data::where('userId','1')->paginate(3);
$book = $book->toArray();
$book['printWord'] = 'Hellow!';
return response()->json($book);
This will work. you can convert paginate object to array, then you need to add element into the array
Upvotes: 0
Reputation: 595
I believe the proper way is to create a resource (see https://laravel.com/docs/8.x/eloquent-resources) and use the additional() method for this. A common use case is displaying a paged result set while showing the grand total for the entire collection.
// collect the grand total
$grandTotal = $customer->invoices()->sum('amount_excl');
// prepare the result set
$data = InvoiceResource::collection($customer->invoices()->paginate(10));
// inject additional data
$data->additional(['extra'=>['total_amount' => $grandTotal]]);
return $data;
You'll get the proper response with metadata, and your added data in the separate 'extra' property.
{
data: [{id: 15922, identifier: "12345", amount_excl: 143.27,…},…]
extra: {total_amount: 6554.35}
links: {first: "https://your.url.com/customers/80/invoices?page=1",…}
meta: {current_page: 1, from: 1, last_page: 5, path:
}
Upvotes: 5
Reputation: 323
Modify exiting pagination data send update response.
$itemsPaginated = $this->items()
->paginate(15);
$itemsTransformed = $itemsPaginated
->getCollection()
->map(function($item) {
return [
'id' => $item->id,
];
})->toArray();
$itemsTransformedAndPaginated = new \Illuminate\Pagination\LengthAwarePaginator(
$itemsTransformed,
$itemsPaginated->total(),
$itemsPaginated->perPage(),
$itemsPaginated->currentPage(), [
'path' => \Request::url(),
'query' => [
'page' => $itemsPaginated->currentPage()
]
]
);
Upvotes: 1
Reputation: 91
Finally figured this out. If you are trying to return a json or array result, just change the paginated data to an array first. Then add the attributes you need to the array.
$results = $data->paginate()->toArray();
$results['new_attribute'] = 'Attribute Value';
return response()->json($result);
Merging or appending to the paginated data doesn't work because it gets lost when the json() method converts it to an array. So by converting it to an array first, you can modify the end results.
Upvotes: 0
Reputation: 909
If you use the Illuminate\Pagination\LengthAwarePaginator class "manually", there's the option of extending it and overriding the toArray
method:
return new class(
$collection,
$count,
$limit,
$page,
// https://github.com/laravel/framework/blob/6.x/src/Illuminate/Pagination/LengthAwarePaginator.php#L40
// values here will become properties of the object
[
'seed' => $seed
]
) extends LengthAwarePaginator {
public function toArray()
{
$data = parent::toArray();
// place whatever you want to send here
$data['seed'] = $this->seed;
return $data;
}
};
Result
current_page 1
data []
first_page_url "/finder?max_miles=100&zip_code=10001&seed=0.2&page=1"
from null
last_page 1
last_page_url "/finder?max_miles=100&zip_code=10001&seed=0.2&page=1"
next_page_url null
path "/finder"
per_page 20
prev_page_url null
to null
total 0
seed 0.2 // <-- our custom prop
There's some extra work involved with instantiating LengthAwarePaginator
yourself, but it gets the job done.
Upvotes: 7
Reputation: 435
You can also obtain data from your model using eloquent, for example, let say that each Data has a list of cars, books, and pets. Your Data model file should have a method that declares the "has many" relations with those classes, like below:
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class Data extends Model{
protected $table = 'data';
protected $fillable = [
'id', 'user_id', 'vendorId', 'name', 'phone'
];
public function cars(){
return $this->hasMany(Car::class);
}
public function books(){
return $this->hasMany(Book::class);
}
public function pets(){
return $this->hasMany(Pet::class);
}
Also, you must have the "belongsTo" method in every Car, Book, and Pet model.
...
public function data(){
return $this->belongsTo(Data::class);
}
...
Finally, to obtain your results paginated and with all the data related to cars, books, and pets, the implementation should be:
$data_elements = Data::where('userId','1')->paginate(3);
foreach($data_elements as $d){
$d->cars;
$d->books;
$d->pets;
}
return response()->json($data_elements);
I hope this alternative be useful for you or for some other developer!
Upvotes: 0
Reputation: 163978
You can create collection with custom data manually and use merge()
helper:
$book = Data::where('userId','1')->paginate(3);
$custom = collect(['my_data' => 'My custom data here']);
$data = $custom->merge($book);
return response()->json($data);
Just checked, it works just fine.
Upvotes: 56
Reputation: 33216
The paginate function will return an object of the type Illuminate\Pagination\LengthAwarePaginator
, it is not possible to just add another field to this.
I think the best solution for you would be to add the pagination object and the other values in an array and convert this array to json. This way your books data is seperated from the other data you want to add.
Like so:
return response()->json([
'books' => $books,
'foo' => 'bar'
]);
In this case your json object will look like this:
{
books: {
// Pagination object with books
}
foo: 'bar'
}
Upvotes: 2