laravel属于哪里

时间:2017-04-28 01:59:45

标签: php laravel eloquent

我希望table_entrypoint上的数据超过table_a table_b中的所有行,带有过滤的table_c。

表:

table_entrypoint:
    id - int

table_a:
    id - int
    table_entrypoint_id - int
    table_b_id - int
    table_c_id - int|NULL

table_b:
    id - int

table_c:
    id - int
    table_b_int

table_entrypoint有很多table_a。 table_a有一个table_b。 table_b有很多table_c。

Entrypoint::first()->belongsTo('table_a')->with('table_b')->with('table_c')->get()之类的内容会为table_c创建Illuminate\Database\Eloquent\Collection。我想通过table_a.table_c_id过滤Illuminate\Database\Eloquent\Model。如果没有逻辑问题,我首选table_a中的函数。

来自我的代码......

table_a的模型(硬切):

class Order extends Illuminate\Database\Eloquent\Model
{
    protected $fillable = [
        'product_id',
        'variant_id',
        'table_id',
    ];

    /**
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
     */
    public function product()
    {
        return $this->belongsTo(Product::class);
    }

    /**
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
     */
    public function variant()
    {
        return $this->belongsTo(Variant::class);
    }

    /**
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
     */
    public function table()
    {
        return $this->belongsTo(Table::class);
    }
}

table_b的模型(硬切):

class Product extends Illuminate\Database\Eloquent\Model
{
    protected $fillable = [
        'name',
    ];

    protected $with = [
        'variants',
    ];

    /**
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
     */
    public function variants()
    {
        return $this->hasMany(Variant::class);
    }

    /**
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
     */
    public function orders()
    {
        return $this->hasMany(Order::class);
    }
}

table_c的模型(硬切):

class Variant extends Illuminate\Database\Eloquent\Model
{
    protected $fillable = [
        'product_id',
        'name',
    ];

    /**
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
     */
    public function product()
    {
        return $this->belongsTo(Product::class);
    }
}

如果我打电话给修补程序App\Table::first()->orders()->with('product')->get(),我会得到以下结果:

=> Illuminate\Database\Eloquent\Collection {#712
     all: [
       App\Order {#711
         id: "1",
         product: App\Product {#725
           id: "1",
           name: "TEST",
           variants: Illuminate\Database\Eloquent\Collection {#735
             all: [
               App\Variant {#740
                 id: "1",
                 name: "Variant1",
               },
               App\Variant {#741
                 id: "2",
                 name: "Variant2",
               },
               [...]
             ],
           },
         },
       },
     ],
   }

我想要App\Table::first()->orders()->with('productVariant')->get()之类的结果:

=> Illuminate\Database\Eloquent\Collection {#712
     all: [
       App\Order {#711
         id: "1",
         product: App\Product {#725
           id: "1",
           name: "TEST",
           variant: App\Variant {#740
               id: "1",
               name: "Variant1",
             },
           },
         },
       },
     ],
   }

la lavel Illuminate\Database\Eloquent中是否有此功能?

0 个答案:

没有答案