【问题标题】:How to query and display user firstname from User model using a Model that has morphTo()如何使用具有 morphTo() 的模型从用户模型中查询和显示用户名
【发布时间】:2015-11-27 21:38:47
【问题描述】:

我知道这个问题有点难以理解。但是,我保证你读到这里就不会了:我正在创建一个简单的应用程序来在一个名为评论的表中创建产品、商店和用户评论。所以,在这种情况下,我使用的是Polymorphic Relation

下面是我的数据库表迁移:

这是我的用户表迁移:

public function up()
{
    Schema::create('users', function (Blueprint $table) {
        $table->increments('id');
        $table->string('first_name');
        $table->string('last_name');
        $table->string('email')->unique();
        $table->string('username')->nullable();
        $table->string('password', 60);
        $table->rememberToken();
        $table->timestamps();
    });
}

这是我的评论表迁移:

public function up()
{
    Schema::create('reviews', function (Blueprint $table) {
        $table->increments('id');
        $table->integer('user_id')->unsigned()->index();
        $table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
        $table->string('body')->nullable();
        $table->integer('reviewable_id')->nullable();
        $table->string('reviewable_type')->nullable();
        $table->timestamps(); //contains the review date etc..
    });
}

这是产品表迁移:

public function up()
{
    Schema::create('products', function (Blueprint $table) {
        $table->increments('id');
        $table->string('user_id');
        $table->string('name');
        $table->string('description')->nullable();
        $table->string('category')->nullable();
        $table->decimal('price')->nullable(); // Product price
        $table->string('product_photo_path')->default('image/default_product_photo.jpg')->nullable(); //add one first
        $table->timestamps();
    });
}

对于我的模型: 在我的 Review.php 模型中,我有这样的关系:

  public function reviewable()
{
    return $this->morphTo();

}

在我的 User.php 模型中,我有这种关系:

  public function reviews()
{
    return $this->morphMany('App\Review', 'reviewable');
}

在我的 Product.php 模型中,我有这种关系(与用户模型相同):

  public function reviews()
{
    return $this->morphMany('App\Review', 'reviewable');
}

我可以在一页中列出用户发表的所有产品评论,但是如何使用 $reviews 变量显示发表评论的用户的姓名。

例如,这是我的 ReviewController.php,我有这个函数来查询所有评论:

public function index()
{
    $reviews = Review::all();
    return view('reviews.index')->with('reviews', $reviews);
}

然后在reviews.index.blade.php中

我可以直接调用

   @foreach($reviews as $review)
    {{$review->body }}
   @endforeach

这将列出用户所做的所有评论。所以,问题是,如何使用 $review 变量显示用户名?我在下面尝试过,但它不起作用:

   @foreach($reviews as $review)
    {{$review->users->first_name}} //Not working
   @endforeach

我认为我需要在 Review 模型上添加一个额外的关系,如下所示,这样我才能像这个 review->users->first_name;

    public function users()
   {
    return $this->hasMany('App\Review');
   }

但这不起作用。那么,在Review模型已经定义为多态关系的情况下,这里如何定义关系呢?

【问题讨论】:

    标签: database eloquent relationship laravel-5.1


    【解决方案1】:

    那是一个愚蠢的错误。而不是

       public function users()
       {
        return $this->hasMany('App\Review');
       }
    

    我改成

        public function users()
      {
        return $this->hasMany('App\User');
      }
    

    解决了这个问题。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-09-17
      • 2020-11-02
      • 1970-01-01
      • 2017-02-02
      • 2023-03-29
      • 2017-06-04
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多