【问题标题】:Laravel Model relationship "Page have many Attachments"Laravel 模型关系“页面有很多附件”
【发布时间】:2021-11-25 09:36:39
【问题描述】:

我有模型:

页面

  • 身份证
  • 蛞蝓

图片

  • 身份证
  • 文件

视频

  • 身份证
  • 文件

我需要页面模型通过一种关系与多个图像和视频模型建立关系,例如

foreach($page->attachments as $attachment)
{
    // $attachment can be Image or Video
}

然后像这样插入

$attachments = [$image, $video];
$page->attachments()->saveMany($attachments);

我试图建立一个变形关系,但没有任何结果,请帮忙。

【问题讨论】:

    标签: php laravel eloquent-relationship


    【解决方案1】:

    使用以下列/属性创建附件模型和附件表:

    • 身份证
    • 文件
    • page_id
    • 类型(视频/图像)

    那么你可以在你的页面模型中添加hasmany关系

    public function attachments()
    {
        return $this->hasMany(Attachment::class);
    }
    

    然后你可以像你尝试的那样获取附件

    【讨论】:

      【解决方案2】:

      为了实现这一点,您必须为关系制作表格。这个表应该这样定义:

      page_image_video

      • 身份证
      • page_id
      • image_id
      • video_id

      并且字段page_idimage_idvideo_id 应该是外键。这是一个表格,您可以在其中保存页面的附件。之后,您可以使用hasMany() 在您的页面模型中定义方法attachments()

      【讨论】:

        【解决方案3】:

        创建迁移:

        页表:

        Schema::create('posts', function (Blueprint $table) {
        
            $table->increments('id');
        
            $table->string("slug");
        
            $table->timestamps();
        
        });
        

        图像表:

        Schema::create('tags', function (Blueprint $table) {
        
            $table->increments('id');
        
            $table->string("file");
        
            $table->timestamps();
        
        });
        

        视频表:

        Schema::create('video', function (Blueprint $table) {
        
            $table->increments('id');
        
            $table->string("file");
        
            $table->timestamps();
        
        });
        

        分页表:

        Schema::create('pageables', function (Blueprint $table) {
        
            $table->integer("pages_id");
        
            $table->integer("pageable_id");
        
            $table->string("pageable_type");
        
        });
        

        创建模型:

        现在,我们将创建页面、图像和视频模型。我们还将使用 morphToMany() 和 morphedByMany() 来处理两个模型的关系。

        视频模型:

        <?php
        
        namespace App\Models;
        
        use Illuminate\Database\Eloquent\Factories\HasFactory;
        use Illuminate\Database\Eloquent\Model;
        
        class Video extends Model
        {
            use HasFactory;
            protected $table='video';
            protected $primaryKey='id';
        
            protected $guarded = [];
        
            public function pages()
            {
                return $this->morphToMany(Pages::class, 'pageable');
            }
        }
        

        图像模型:

        <?php
        
        namespace App\Models;
        
        use Illuminate\Database\Eloquent\Factories\HasFactory;
        use Illuminate\Database\Eloquent\Model;
        
        
        class Images extends Model
        {
            use HasFactory;
            protected $table='image';
            protected $primaryKey='id';
        
            protected $guarded = [];
        
            public $timestamps = false;
        
            public function pages()
            {
                return $this->morphToMany(Pages::class, 'pageable');
            }
        }
        

        页面模型:

        <?php
        
        namespace App\Models;
        
        use Illuminate\Database\Eloquent\Factories\HasFactory;
        use Illuminate\Database\Eloquent\Model;
        use Illuminate\Database\Eloquent\Relations\Relation;
        
        
        class Pages extends Model
        {
            use HasFactory;
            protected $table='page';
            protected $primaryKey='id';
        
            protected $guarded = [];
            public $timestamps = false;
        
        
            public function posts()
            {
                return $this->morphedByMany(Images::class, 'pageable');
            }
        
            /**
             * Get all of the videos that are assigned this tag.
             */
            public function videos()
            {
                return $this->morphedByMany(Video::class, 'pageable');
            }
        
        
        }
        

        检索记录:

        $pages = Pages::find(1);
                foreach ($pages->posts as $post) {
                    var_dump($post);
                }
                foreach ($pages->videos as $video) {
                    print_r('<br>');
                    //var_dump($video);
                }
        

        创建记录:

        $page = Pages::find(1);

            $img = new Images();
            $img->file = "test insert";
        
            $page->posts()->save($img);
        

        全部完成。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2014-07-10
          • 1970-01-01
          • 2015-08-05
          • 2016-10-12
          • 2020-10-24
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多