如何在laravel eloquent中从产品表和项目表中检索交货表所有数据
//这是送货表如何在laravel eloquent中从产品表和项目表中检索交货表所有数据,laravel,eloquent,eloquent-relationship,Laravel,Eloquent,Eloquent Relationship,//这是送货表 Schema::create('deliveries', function (Blueprint $table) { $table->bigIncrements('id'); $table->unsignedInteger('user_id'); $table->unsignedInteger('product_id'); $table->unsignedInt
Schema::create('deliveries', function (Blueprint $table) {
$table->bigIncrements('id');
$table->unsignedInteger('user_id');
$table->unsignedInteger('product_id');
$table->unsignedInteger('qty');
$table->string('person_name');
$table->string('designation');
$table->string('Office');
$table->string('mobile');
$table->date('date');
$table->timestamps();
});
//这是产品表
Schema::create('products', function (Blueprint $table) {
$table->bigIncrements('id');
$table->unsignedInteger('user_id');
$table->unsignedInteger('category_id');
$table->unsignedInteger('item_id');
$table->unsignedInteger('brand_id');
$table->string('item_model')->nullable();
$table->unsignedInteger('qty');
$table->unsignedInteger('item_price');
$table->unsignedInteger('total_price');
$table->timestamps();
});
//这是表格上的项目
Schema::create('items', function (Blueprint $table) {
$table->bigIncrements('id');
$table->unsignedInteger('category_id');
$table->string('item_name')->unique();;
});
如何通过雄辩的模型从交货表中检索产品和项目表数据?
我有3个模型文件是Delivery.php、Item.php和Product.php
我想通过索引方法访问DeliveryController.php。但我无法访问项目名称
public function index()
{
return Delivery::with('user')
->with('product')
->orderby('id', 'desc')->get();
}
<tr role="row" class="odd" v-for="(delivery, index) in deliveries" :key="index">
<td>{{ index+1 }}</td>
<td>{{ delivery.product.item_name}}</td>
</tr>
公共功能索引()
{
返回传递::with('user'))
->带(‘产品’)
->orderby('id','desc')->get();
}
{{index+1}}
{{delivery.product.item_name}
首先,您应该在表上添加外键约束。像这样
Schema::create('deliveries', function (Blueprint $table) {
$table->bigIncrements('id');
$table->unsignedInteger('user_id');
$table->unsignedInteger('product_id');
$table->unsignedInteger('qty');
$table->string('person_name');
$table->string('designation');
$table->string('Office');
$table->string('mobile');
$table->date('date');
$table->timestamps();
$table->foreign('user_id')
->references('id')
->on('users');
$table->foreign('product_id')
->references('id')
->on('products')
});
所有这样的桌子。
在此之后,假设您在交付模型上正确定义了关系,您可以这样做:
public function index()
{
return Delivery::with(['user','product'])
->orderby('id', 'desc')->get();
}
首先,应该在表上添加外键约束。像这样
Schema::create('deliveries', function (Blueprint $table) {
$table->bigIncrements('id');
$table->unsignedInteger('user_id');
$table->unsignedInteger('product_id');
$table->unsignedInteger('qty');
$table->string('person_name');
$table->string('designation');
$table->string('Office');
$table->string('mobile');
$table->date('date');
$table->timestamps();
$table->foreign('user_id')
->references('id')
->on('users');
$table->foreign('product_id')
->references('id')
->on('products')
});
所有这样的桌子。
在此之后,假设您在交付模型上正确定义了关系,您可以这样做:
public function index()
{
return Delivery::with(['user','product'])
->orderby('id', 'desc')->get();
}
我认为您忘记在交付模型上定义此关系,请在交付模型上添加此关系
public function user(){
return $this->belongsTo('App\User');
}
public function product(){
return $this->belongsTo('App\Product');
}
添加后,您可以在加载交付资源时访问用户
和产品
Delivery::with(['user','product'])->orderby('id', 'desc')->get();
我认为您忘记在交付模型上定义此关系,请在交付模型上添加此关系
public function user(){
return $this->belongsTo('App\User');
}
public function product(){
return $this->belongsTo('App\Product');
}
添加后,您可以在加载交付资源时访问用户
和产品
Delivery::with(['user','product'])->orderby('id', 'desc')->get();
事实上,兄弟,我以前做过这件事。我无法获取product和user表,但无法从items表中获取item_名称。如何使用Delivery Model从items表中获取item_name是的,如果您在产品和item之间建立了关系,则可以从item表中获取item name,然后只需调用{{{Delivery.product.item.item_name}}实际上,兄弟,我以前做过这件事。我无法获取product和user表,但无法从items表中获取item_名称。如何使用Delivery Model从items表中获取item_name是的,如果您在产品和item之间建立了关系,则可以从item表中获取item name,然后只需调用{{{Delivery.product.item.item_name}}实际上,兄弟,我以前做过这件事。我无法获取product和user表,但无法从items表中获取item_名称。我如何使用交货模型从items表中获取items_name?您能否显示交货模型,并说明项目与交货或产品模型的关系?这是我的文件:@RuhulAmin我认为您的表结构错误,交货表中的项目没有引用。如果您想从交付模型中获取项目,您应该定义这些模型之间的关系,或者通过嵌套的即时加载来获取它们。我建议你读一下这本书,它会100%的帮助你,兄弟,我以前做过这件事。我无法获取product和user表,但无法从items表中获取item_名称。我如何使用交货模型从items表中获取items_name?您能否显示交货模型,并说明项目与交货或产品模型的关系?这是我的文件:@RuhulAmin我认为您的表结构错误,交货表中的项目没有引用。如果您想从交付模型中获取项目,您应该定义这些模型之间的关系,或者通过嵌套的即时加载来获取它们。我建议你读一读,这将100%帮助你