【发布时间】:2016-06-19 19:28:43
【问题描述】:
我有一个表“reviews”,其中有一列名为“from_user”。所以基本上是放置评论的用户。在此列中,用户的 id 存储在 users 表中。是否可以在我的视图中找出用户名?
这就是现在的样子:
@foreach($authUser->reviews as $review)
<p>{{ $review->body }} - {{$review->from_user }}</p>
@endforeach
所以这段代码现在显然显示了用户的 id。
这是我的用户表的样子:
ID |姓名 |用户名 | ...
这是我的评论表的样子:
ID |来自用户 | on_user |身体
所以我的评论表中的 from_user 等于我的用户表的 ID
所以我的问题是是否可以在我的视图中访问我的 foreach 循环中的用户名?
我对 laravel 还很陌生,所以感谢任何帮助!
在此先感谢
编辑:用户模型
class User extends Model implements AuthenticatableContract,
AuthorizableContract,
CanResetPasswordContract
{
use Authenticatable, Authorizable, CanResetPassword, Messagable;
protected $table = 'users';
protected $fillable = ['name', 'email', 'password', 'firstname', 'lastname', 'straat', 'postcode', 'plaats', 'provincie', 'role', 'specialisatie'];
protected $hidden = ['password', 'remember_token'];
public function projects()
{
return $this->hasMany('App\Project');
}
public function reviews()
{
return $this->hasMany('App\Review');
}
public function comments()
{
return $this->hasMany('App\Comment');
}
}
EDIT2 我的评论模型
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Review extends Model
{
protected $guarded = [];
protected $table = 'reviews';
public function User()
{
return $this->belongsTo('App\User');
}
}
【问题讨论】:
-
可以提供用户模型吗?
-
@xdevnull 是的,结帐已编辑 OP