我在Laravel 9.x中使用多态关系时遇到问题。

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

I have problems using Polymorphic Relationships in Laravel 9.x

问题

I have 5 tables named Users, Chats, Buletins, Hartas, and Owners. All of these tables have a many-to-many relationship with another table named Medias, via a pivot table named Mediables.

I need to use the polymorphic relationships because all of these 5 tables will be using the same Mediables table to store their relations with the Medias table.

For this thread, let me share my Buletins, Medias, and Mediables polymorphic relationships defined on their respective model files. I'll also share the migration file for the Mediables table and my controller.

Buletin Model:

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\MorphToMany;

class Buletin extends Model
{
  public function user() 
  {
    return $this->belongsTo(User::class);
  }
  public function medias(): MorphToMany
  {
    return $this->morphToMany(Media::class, 'mediable');
  }
}

Media Model:

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\MorphToMany;

class Media extends Model
{
  public function buletins(): MorphToMany
  {
    return $this->morphedByMany(Buletin::class, 'mediable');
  }
}

Mediable Model:

class Medialink extends Model
{
  // no relation defined
}

Mediable Migration:

public function up()
{
  Schema::create('mediables', function (Blueprint $table) {
    $table->id();
    $table->unsignedBigInteger('media_id');
    $table->foreign('media_id')->references('id')->on('medias')->onDelete('cascade');
    $table->unsignedBigInteger('mediable_id');
    $table->string('mediable_type');
    $table->timestamps();
  });
}

In the Controller file:

public function showbuletin($id)
{
  $buletins = Buletin::where('id', $id)->orWhere('comment_id', $id)->paginate(5);
  return view('layouts.buletin.show-bbs')->with(compact('buletins'));
}

In show-bbs.blade.php, I managed to echo all of these inside a foreach loop ($buletins as $buletin):

{{ $buletin->user->name }} // Buletin belongs to User & User has many Buletin
{{ $buletin->messages }}
{{ $buletin->created_at }}

The problem is, I didn't manage to echo this:

{{ $buletin->medias }} // gives me "[]" on the display

Please help me.

英文:

I have 5 tables named Users, Chats, Buletins, Hartas, and Owners. All of these tables has many-to-many relationship with another table named Medias, via a pivot table named Mediables.

I need to use the polymorphic relationships because all of these 5 tables will be using the same Mediables table to store their relations with the Medias table.

For this thread, let me share my Buletins, Medias, and Mediables polymorphic relationships defined on their respective model files. I'll also shared the migration file for Mediables tablemy controller

Buletin Model:

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\MorphToMany;

class Buletin extends Model
{
  public function user() 
  {
    return $this->belongsTo(User::class);
  }
  public function medias(): MorphToMany
  {
    return $this->morphToMany(Media::class, 'mediable');
  }
}

Media Model:

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\MorphToMany;

class Media extends Model
{
  public function buletins(): MorphToMany
  {
    return $this->morphedByMany(Buletin::class, 'mediable');
  }
}

Mediable Model:

class Medialink extends Model
{
  // no relation defined
}

Mediable Migration:

public function up()
{
  Schema::create('mediables', function (Blueprint $table) {
    $table->id();
    $table->unsignedBigInteger('media_id');
    $table->foreign('media_id')->references('id')->on('medias')->onDelete('cascade');
    $table->unsignedBigInteger('mediable_id');
    $table->string('mediable_type');
    $table->timestamps();
  });
}

In Controller file:

public function showbuletin ($id)
{
  $buletins = Buletin::where('id', $id)->orWhere('comment_id', $id)->paginate(5);
  return view('layouts.buletin.show-bbs')->with(compact('buletins'));
}

In show-bbs.blade.php, I managed to echo all of these inside a foreach loop ($buletins as $buletin):

{{ $buletin->user->name }} // Buletin belongsTo User & User hasMany Buletin
{{ $buletin->messages }}
{{ $buletin->created_at }}

Problem is, I didn't manage to echo this:

{{ $buletin->medias }} // gives me "[]" on the display

Please help me.

答案1

得分: 0

已经得到了这个问题的答案。问题出在如何保存mediables表中的mediable_type。以下是我保存它以使其正常工作的方式:

控制器文件:

// 在mediables表中存储新的buletin_media
if ($request->media_id) {
foreach ($request->media_id as $key => $value) {
Mediable::create([
'media_id' => $value,
'mediable_id' => $buletin->id,
'mediable_type' => Buletin::class,
]);
}
}

我必须保存精确的Buletin模型以使其正常工作。

英文:

Already got the answer for this question. Problems lies in how I save the mediable_type inside my mediables table. Below is how I save it to get it to works:

Controller File:

// store new buletin_media in mediables table
if ($request->media_id) {
  foreach ($request->media_id as $key => $value) {
    Mediable::create([
      'media_id' => $value,
      'mediable_id' => $buletin->id,
      'mediable_type' => Buletin::class,
    ]);
  }
}

I have to save the exact Buletin model to get it to works.

huangapple
  • 本文由 发表于 2023年3月15日 19:52:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/75744330.html
匿名

发表评论

匿名网友

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

确定