如何解决与with
我有一个N:N关系,这给我使用with()
带来的收益带来了问题,它遵循我的两个模型的例子以及问题和我进行查询调用的方式。
模型product_type
public function makes()
{
return $this->belongsToMany(\App\Models\Make::class);
}
模型制作
public function productTypes()
{
return $this->belongsToMany(\App\Models\ProductType::class);
}
我的关系表具有以下字段
make_product_type -> nome da tabela
make_id
product_type_id
在我的控制器中,我试图以此方式退回所有带有其品牌的产品
Make::with('productTypes')->get()
但此时返回以下错误:
试图获取非对象的属性“ make_id”
我尝试告知模型中的关系字段,但它也不起作用。它起作用的唯一方法是重写模型的toArray()方法并强制返回关系,这对我来说不可行,因为我只需要在执行的查询中返回此关系即可。
public function toArray()
{
return [
'id' => $this->id,'name' => $this->name,'product_type' => $this->productTypes()->get()->toArray()
];
}
如果有人可以向我解释我做错了什么,尤其是为什么做错了,我将不胜感激,因为我已多次阅读Laravel文档,但我未能成功理解问题所在。观察如果有更改表结构的建议,那也没有问题,因为我正在启动一个项目,此刻我仍然可以更改内容
修改
跟随我的迁移
public function up()
{
Schema::create('make_product_type',function (Blueprint $table) {
$table->foreignId('make_id')->constrained()->onDelete('restrict');
$table->foreignId('product_type_id')->constrained()
->onDelete('restrict');
});
}
public function down()
{
Schema::dropIfExists('make_product_type');
}
我的表中只有我插入进行测试的这些记录
|make_id |product_type_id |
|--------------------|--------------------|
|1 |1 |
|1 |2 |
|1 |18 |
进行迁移
Schema::create('makes',function (Blueprint $table) {
$table->id();
$table->string('name',150);
$table->integer('importance')->nullable();
$table->integer('created_by')->nullable();
$table->integer('updated_by')->nullable();
$table->integer('deleted_by')->nullable();
$table->softDeletes();
$table->timestamps();
});
产品迁移
Schema::create('product_types',150);
$table->integer('created_by')->nullable();
$table->integer('updated_by')->nullable();
$table->integer('deleted_by')->nullable();
$table->softDeletes();
$table->timestamps();
});
已解决
I managed to find the problem,I had a function to set the attributes
public function setAttribute ($key,$value)
{
if (array_key_exists ($key,$this->columns)) {
$key = $this->columns [$key];
}
return parent::setAttribute($key,$value);
}
I dont understand why but when removing this function everything worked normally.
Thank you very much for the help you have given me
解决方法
它可以根据您的需要
make_product_type迁移
public function up()
{
Schema::create('make_product_type',function (Blueprint $table) {
$table->id();
$table->unsignedBigInteger('make_id')->nullable();
$table->foreign('make_id')->references('id')->on('makes')->onDelete('cascade');
$table->unsignedBigInteger('product_type_id')->nullable();
$table->foreign('product_type_id')->references('id')->on('product_types')->onDelete('cascade');
});
}
public function down()
{
Schema::dropIfExists('make_product_type');
}
产品型号类型
public function makes()
{
return $this->belongsToMany('App\Make');
}
模型制作
public function productTypes()
{
return $this->belongsToMany('App\ProductType');
}
然后签入您的控制器
$products = Make::with('productTypes')->get();
dd($products);
,
首先您需要添加
public function up(){
Schema::create('make_product_type',function (Blueprint $table) {
$table->foreignId('make_id')->constrained()->onDelete('restrict');
$table->foreignId('product_type_id')->constrained()
->onDelete('restrict');
$table->primary(['make_id','product_type_id']);
});
}
现在添加的薄型号名称为makeProductType
namespace App;
use Illuminate\Database\Eloquent\Relations\Pivot;
class MakeProductType extends Pivot
{
protected $table = 'make_product_type';
protected $fillable = ['make_id','product_type_id'];
protected $primaryKey = ['make_id','product_type_id'];
public $incrementing = false;
public function makes() {
return $this->belongsTo(Make::class,'make_id','id');
}
public function ProductTypes() {
return $this->belongsTo(ProductType::class,'product_type_id','id');
}
}
在模型ProductType中添加新关系
public function makeProductType(){
return $this->hasManyThrough(MakeProductType::class,Make::class);
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。