英文:
Problems When fetching relation data with eagerload laravel 9
问题
I have a problem when Eager Loading a data which is already has one to many relationship and the error like code bellow
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'media_categories.foto_id' in 'where clause'
select * from media_categories
where media_categories
.foto_id
in (2)
this error appear when i try to eagerload the media_category data from FotoController, And why its demanding foto_id?
this is my controller
$foto = Foto::with('mediaCategory','user')->where('id', $request->id)->first();
return Inertia::render('Foto/DetailFoto', [
'foto' => $foto
]);
and this is my table scheme
this is my fotos table
Schema::create('fotos', function (Blueprint $table) {
$table->id();
$table->unsignedInteger('media_id');
$table->string('name');
$table->decimal('price');
$table->string('description');
$table->date('date');
$table->string('folder');
$table->smallInteger('pcs');
$table->unsignedInteger('user_id');
$table->timestamps();
});
this is my media_categories table
Schema::create('media_categories', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->text('description');
$table->unsignedInteger('user_id');
$table->timestamps();
});
and this is my relationship at foto model
public function user(){
return $this->BelongsTo(User::class);
}
public function mediaCategory(){
return $this->HasMany(MediaCategory::class);
}
this my media_category model
public function user(){
return $this->BelongsTo(User::class);
}
public function foto(){
return $this->BelongsTo(Foto::class);
}
英文:
I have a problem when Eager Loading a data which is already has one to many relationship and the error like code bellow
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'media_categories.foto_id' in 'where clause'
select * from `media_categories` where `media_categories`.`foto_id` in (2)
this error appear when i try to eagerload the media_category data from FotoController, And why its demanding foto_id?
this is my controller
$foto = Foto::with('mediaCategory','user')->where('id', $request->id)->first();
return Inertia::render('Foto/DetailFoto', [
'foto' => $foto
]);
and this is my table scheme
this is my fotos table
Schema::create('fotos', function (Blueprint $table) {
$table->id();
$table->unsignedInteger('media_id');
$table->string('name');
$table->decimal('price');
$table->string('description');
$table->date('date');
$table->string('folder');
$table->smallInteger('pcs');
$table->unsignedInteger('user_id');
$table->timestamps();
});
this is my media_categories table
Schema::create('media_categories', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->text('description');
$table->unsignedInteger('user_id');
$table->timestamps();
});
and this is my relationship at foto model
public function user(){
return $this->BelongsTo(User::class);
}
public function mediaCategory(){
return $this->HasMany(MediaCategory::class);
}
this my media_category model
public function user(){
return $this->BelongsTo(User::class);
}
public function foto(){
return $this->BelongsTo(Foto::class);
}
答案1
得分: 1
你在Foto模型中将关系定义为HasMany,但它应该是BelongsTo。
public function mediaCategory(){
return $this->belongsTo(MediaCategory::class, 'media_id');
}
英文:
You have defined the relationship as HasMany in the Foto model, but it should be BelongsTo instead
public function mediaCategory(){
return $this->belongsTo(MediaCategory::class, 'media_id');
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论