Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/laravel/11.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/jpa/2.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
Laravel 拉威尔4号雄辩_Laravel - Fatal编程技术网

Laravel 拉威尔4号雄辩

Laravel 拉威尔4号雄辩,laravel,Laravel,最近我和拉雷维尔一起踢球,但我发现有些困难 我有两张桌子: 用户表格和图像表格 用户表结构 图像表结构 在用户模型上,我做了如下操作: public function image() { return $this->hasMany('Image'); } 以下是我的观点: $users = User::all(); ( I declare this to my controller or route file) @foreach($users as $user) {{$user-&g

最近我和拉雷维尔一起踢球,但我发现有些困难

我有两张桌子: 用户表格和图像表格

用户表结构

图像表结构

在用户模型上,我做了如下操作:

public function image() {

return $this->hasMany('Image');
}
以下是我的观点:

$users = User::all(); ( I declare this to my controller or route file)

@foreach($users as $user)
{{$user->image}}

@endforeach
它在我的视图[]中显示了这一点

当我把我的模型换成这个的时候

  public function image(){

  return $this->hasMany('Image','user_id', 'id');
}
它显示图像表中的所有信息

要仅输出图像,我可以做什么?感谢您

从您的观点改变

@foreach($users as $user)
{{$user->image}}

@endforeach


Laravel正在为您的图像模型返回一个集合对象,因此您只需在循环中引用所需的变量。

什么是“它显示图像表中的所有信息”。显示输出。我的意思是,代码输出id、用户id、头像,而我想要头像。谢谢你,我忘记了,当添加多个图像时,这可能不起作用,因为它是一个有很多个。您可能需要
foreach($user->image as$image)
  public function image(){

  return $this->hasMany('Image','user_id', 'id');
}
@foreach($users as $user)
{{$user->image}}

@endforeach
@foreach($users as $user)
{{$user->image->avatar}}

@endforeach