Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/69.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 当您知道id时,Laravel将从视图中的其他表中获取数据_Php_Mysql_Laravel - Fatal编程技术网

Php 当您知道id时,Laravel将从视图中的其他表中获取数据

Php 当您知道id时,Laravel将从视图中的其他表中获取数据,php,mysql,laravel,Php,Mysql,Laravel,我有一个“reviews”表,其中有一列名为“from_user”。因此,基本上是放置评论的用户。在此列中,用户id存储在users表中。可以在我的视图中找到用户名吗 这就是它现在的样子: @foreach($authUser->reviews as $review) <p>{{ $review->body }} - {{$review->from_user }}</p> @endforeach EDIT2我的评论模型 已更新 在您的查看模型

我有一个“reviews”表,其中有一列名为“from_user”。因此,基本上是放置评论的用户。在此列中,用户id存储在users表中。可以在我的视图中找到用户名吗

这就是它现在的样子:

 @foreach($authUser->reviews as $review)
   <p>{{ $review->body }} - {{$review->from_user }}</p>
 @endforeach
EDIT2我的评论模型
已更新

在您的
查看模型中
需要告诉关系要使用什么外键。默认情况下,它添加
relationName\u id

Eloquent通过检查 关系方法的名称,并在方法名称后面加上 _id。但是,如果手机型号上的外键不是用户id,则可以将自定义密钥名称作为第二个参数传递给belongsTo 方法


你能提供用户模型吗?@xdevnull是的,签出编辑的OP我想我已经编辑了关系签出OP我添加了我的用户模型我已经将关系应用于
查看
模型。阅读类名。你能签出我编辑的OP吗?我添加了我的用户和评论模型。我的关系正确吗?非常感谢!很简单:)
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');
    }
}
<?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');
    }
}
<?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', 'from_user');
    }
}
@foreach($authUser->reviews as $review)
   <p>{{ $review->body }} - {{ $review->user->name }}</p>
 @endforeach