Add some data in laravel paginate

别等时光非礼了梦想. 提交于 2019-12-18 21:17:35

问题


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?


回答1:


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.




回答2:


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'
}



回答3:


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:

Data model file

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.

Car model file

...
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:

Method in your Controller

$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!



来源:https://stackoverflow.com/questions/41034351/add-some-data-in-laravel-paginate

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!