如何使用具有 morphTo() 的模型从用户模型查询和显示用户名字
How to query and display user firstname from User model using a Model that has morphTo()
我知道这个问题有点难理解。但是,我保证当你读到这篇文章时不会这样:我正在创建一个简单的应用程序来在一个名为 reviews 的 table 中创建产品、商店和用户评论。所以,在这种情况下,我使用 Polymorphic Relation
下面是我的数据库table迁移:
这是我的用户 table 迁移:
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();
});
}
这是我的评论table迁移:
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..
});
}
这是产品 table 迁移:
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 变量显示用户 first_name?我在下面尝试过但它不起作用:
@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模型已经定义为Polymorphic关系,如何定义关系呢?
那是一个愚蠢的错误。而不是
public function users()
{
return $this->hasMany('App\Review');
}
我改成了
public function users()
{
return $this->hasMany('App\User');
}
问题解决了。
我知道这个问题有点难理解。但是,我保证当你读到这篇文章时不会这样:我正在创建一个简单的应用程序来在一个名为 reviews 的 table 中创建产品、商店和用户评论。所以,在这种情况下,我使用 Polymorphic Relation
下面是我的数据库table迁移:
这是我的用户 table 迁移:
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();
});
}
这是我的评论table迁移:
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..
});
}
这是产品 table 迁移:
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 变量显示用户 first_name?我在下面尝试过但它不起作用:
@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模型已经定义为Polymorphic关系,如何定义关系呢?
那是一个愚蠢的错误。而不是
public function users()
{
return $this->hasMany('App\Review');
}
我改成了
public function users()
{
return $this->hasMany('App\User');
}
问题解决了。