使用 Laravel 9 进行预加载关系数据时出现问题

huangapple go评论116阅读模式
英文:

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');
}

huangapple
  • 本文由 发表于 2023年3月9日 22:36:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/75686030.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定