【问题标题】:Retriving All Polymorphic Relations in Laravel在 Laravel 中检索所有多态关系
【发布时间】:2020-11-14 23:31:54
【问题描述】:

我在我的项目中创建了多态关系。当我想像下面这样检索它们时

return view('category.index')->with('categories', Category::all()) ;

它在我 dd() 时返回关系。但是,当我想访问视图中的关系时

当我在视图中调用它时,它会给我以下结果

    {"id":1,"image":"abc\/abc.jpg","imageable_id":6,"imageable_type":"App\\Category","created_at":"2020-07-24T13:37:29.000000Z","updated_at":"2020-07-24T13:37:29.000000Z"}

但是当我想访问它说的 id 或图像索引时

试图获取非对象的属性“id”(查看:
D:\WaheedSindhani\Projects\Menu\menu_proj\resources\
视图\类别\index.blade.php)

我不知道这里发生了什么

@if(isset($categories))
            @foreach($categories as $category)
                <tr>
                    <th scope="row">{{$loop->index+1}}</th>
                    <td>{{$category->name}}</td>
                    <td>{{$category->images->image}}</td>
                </tr>
            @endforeach
        @endif

它说没有找到名为 image 的属性。
迁移如下
图像表

public function up()
    {
        Schema::create('images', function (Blueprint $table) {
            $table->id();
            $table->string('image');
            $table->unsignedInteger('imageable_id');
            $table->string('imageable_type');
            $table->timestamps();
    });

    }

菜单表

 public function up()
{
    Schema::create('menus', function (Blueprint $table) {
        $table->id();
        $table->string('name');
        $table->boolean('visible');
        $table->integer('priority');
        $table->timestamps();
    });
}

类别表

public function up()
{
    Schema::create('categories', function (Blueprint $table) {
        $table->id();
        $table->string('name');
        $table->boolean('visibility');
        $table->integer('priority');
        $table->timestamps();
    });
}

型号如下

    class Menu extends Model
{
    public function images()
    {
        return $this->morphMany( 'App\Image', 'imageable');
    }
}



class Image extends Model
{
    protected $guarded = [];
    public function imageable(){
        return $this->morphTo();
    }
}

    class Category extends Model
{
    

    protected $guarded = [];
    public function images()
    {
        return $this->morphMany(Image::class, 'imageable');
    }
}

【问题讨论】:

  • but when i want to access the id or image index it says是怎么做到的?
  • {{$category->images->id}}

标签: laravel eloquent


【解决方案1】:

出现您的问题是因为category-&gt;images 是图像的Collection,而不是单个图像。因为您的关系是一对多的,而不是一对一的关系,所以建议是使用 each 循环遍历图像,然后在那里完成您的工作。

这就是为什么您会收到尝试访问非对象上的道具的错误。

@if(isset($categories))
            @foreach($categories as $category)
                <tr>
                    <th scope="row">{{$loop->index+1}}</th>
                    <td>{{$category->name}}</td>

                     @foreach($category->images as $image)
                       <td>{$image->name}</td>
                     @endforeach

                </tr>
            @endforeach
@endif

这个概念适用于每个包含Many、hasMany、belongsToMany 等的关系......

【讨论】:

  • Huhhhhhhh 感谢您的回答。我今天将完成我的项目。今天是最后一天了。
  • 很高兴我能提供帮助,并且编码愉快:)]
猜你喜欢
  • 2019-11-14
  • 1970-01-01
  • 1970-01-01
  • 2019-03-03
  • 1970-01-01
  • 2022-01-10
  • 2020-10-10
  • 1970-01-01
  • 2020-09-10
相关资源
最近更新 更多