Laravel Eloquent Eager用分页加载多个关系

时间:2015-08-18 04:52:08

标签: php laravel-4 pagination eloquent eager-loading

我有一个运行Laravel 4.2的网站,我现在正在处理,问题是我有一个订单模型和另一个属于订单的项目的子模型。这些项目还有一个用于尺寸信息和图像信息的子模型。

我有我的疑问:

$orders = Auth::user()->orders()->with('items','items.size','items.image')->get()->toArray();

这会返回我期待的内容,但我希望使用Laravel的分页对结果进行分页。我曾预料到这会起作用:

$orders = Auth::user()->orders()->with('items','items.size','items.image')->paginate( 10 );

这会返回一个空白的白页和500 error,对于我的生命,我无法弄清楚如何使其正常工作。

以下是我的各种模型:User.php

use Illuminate\Auth\UserTrait;
use Illuminate\Auth\UserInterface;
use Illuminate\Auth\Reminders\RemindableTrait;
use Illuminate\Auth\Reminders\RemindableInterface;

class User extends Eloquent implements UserInterface, RemindableInterface {

    use UserTrait, RemindableTrait;

    /**
     * The database table used by the model.
     *
     * @var string
     */
    protected $table = 'users';

    /**
     * The attributes excluded from the model's JSON form.
     *
     * @var array
     */
    protected $hidden = array('password', 'remember_token');

    protected $fillable = array( 'first_name', 'last_name', 'address', 'city', 'state', 'zip', 'lat', 'lng', 'email', 'phone', 'password', 'hash', 'type', 'blocked', 'created_at', 'updated_at', 'deleted_at' );

    public function setPasswordAttribute( $value ) {
        return $this -> attributes['password'] = Hash::make( $value );
    }

    public function projects() {
        return $this -> belongsToMany('Project');
    }

    public function orders() {
        return $this->hasMany('PrintOrder');
    }

    public function bookings() {
        return $this -> belongsToMany('Booking');
    }
}

PrintOrder.php

use Illuminate\Database\Eloquent\SoftDeletingTrait;

class PrintOrder extends Eloquent {

    use SoftDeletingTrait;

    protected $dates = ['deleted_at'];

    /**
     * The database table used by the model.
     *
     * @var string
     */
    protected $table = 'print_orders';

    protected $fillable = array( 'project_id', 'user_id', 'status', 'created_at', 'updated_at', 'deleted_at' );

    public function items() {
        return $this->hasMany('PrintOrderItem');
    }
}

PrintOrderItem.php

use Illuminate\Database\Eloquent\SoftDeletingTrait;

class PrintOrderItem extends Eloquent {

    use SoftDeletingTrait;

    protected $dates = ['deleted_at'];

    /**
     * The database table used by the model.
     *
     * @var string
     */
    protected $table = 'print_order_items';

    protected $fillable = array( 'print_order_id', 'upload_id', 'size_id', 'quantity', 'created_at', 'updated_at', 'deleted_at' );

    public function image() {
        return $this->hasOne('Upload','id');
    }

    public function size() {
        return $this->hasOne('PrintSize', 'id');
    }
}

Upload.php

use Illuminate\Database\Eloquent\SoftDeletingTrait;

class Upload extends Eloquent {

    use SoftDeletingTrait;

    /**
     * The database table used by the model.
     *
     * @var string
     */
    protected $table = 'uploads';

    protected $fillable = array( 'user_id', 'project_id', 'thumbnail', 'small','medium','large','original','title', 'description','keywords', 'file_type','file_size','file_extension','created_at', 'updated_at', 'deleted_at' );

    protected $dates = ['deleted_at'];

    public function categories() {
        return $this->belongsToMany('UploadCategory', 'upload_image_categorys');
    }
}

PrintSize.php

use Illuminate\Database\Eloquent\SoftDeletingTrait;

class PrintSize extends Eloquent {

    use SoftDeletingTrait;

    protected $dates = ['deleted_at'];

    /**
     * The database table used by the model.
     *
     * @var string
     */
    protected $table = 'print_sizes';

    protected $fillable = array( 'title', 'description', 'cost_actual', 'cost_client', 'status', 'created_at', 'updated_at', 'deleted_at' );

}

0 个答案:

没有答案