使用eloquent分页时,用模型得到的items是对象而不是数组,怎么解

ghjkg546

问题描述

这里写问题描述

这种写法打印的items是对象

 $WebUsers = MovieBasic::when(is_numeric($request->input('status')) , function ($query) use ($request) {
                return $query->where('status', $request->input('status'));
            })->when($request->input('type'), function ($query) use ($request) {
                return $query->where('type', $request->input('type'));
            })
            ->paginate($request->get('pageSize'));
        $items = $WebUsers->items();
        print_r($items);

这种写法打印的items正常,是数组

 $WebUsers =Db::table('movie_basic)->when(is_numeric($request->input('status')) , function ($query) use ($request) {
                return $query->where('status', $request->input('status'));
            })->when($request->input('type'), function ($query) use ($request) {
                return $query->where('type', $request->input('type'));
            })
            ->paginate($request->get('pageSize'));
        $items = $WebUsers->items();
        print_r($items);

model代码

class MovieBasic extends Model
{

    /**
     * The table associated with the model.
     *
     * @var string
     */
    protected $table = 'movie_basic';

    /**
     * The primary key associated with the table.
     *
     * @var string
     */
    protected $primaryKey = 'id';

    /**
     * Indicates if the model should be timestamped.
     *
     * @var bool
     */
    public $timestamps = false;
}
299 1 0
1个回答

不败少龙

toArray()

  • ghjkg546 2024-05-16

    谢谢,用model时,我是把整个数组拿去toArray,所以一直不行,其实是要把 数组的每个子项去toArray, 用model和Db::table,返回的东西不同,卡住我好久了

×
🔝