为foreach()提供的无效参数-其余部分正常工作



我正在开发一个像laravel instagram的网站一切都很顺利,直到我实现了foreach命令

这是我的代码:

@extends('layouts.app')
@section('content')
<div class="container">
<div class="row">
<div class="col-3 p-5">
<img src="/images/simbolomax.jpg" class="rounded-circle">
</div>
<div class="col-9 pt-5">
<div class="d-flex justify-content-between align-items-baseline">
<h1 class="p-2">{{ $user->username }}</h1>
<a href="/p/create">Aiciona nova receita</a>
</div>
<div class="d-flex">
<div class="pe-5"><strong>153</strong> receitas</div>
<div class="pe-5"><strong>23k</strong> seguidores</div>
<div class="pe-5"><strong>212</strong> seguindo</div>
</div>
<div class="pt-4">{{ $user->profile->description }}</div>
</div>
</div>
<div class="row pt-5">
@foreach($user->posts as $post)
<div class="col-4">
<img src="/storage/{{$post->image}}" class="w-100" style="height:100%">
</div>
@endforeach
</div>
</div>
@endsection

,错误为:Invalid argument supplied for foreach() (View: C:projetoseasymealresourcesviewsprofilesindex.blade.php)

我想不出来了,快来帮忙!

如果有一个用户没有帖子…出错了。所以…在@foreach之前检查用户的帖子。@ if (count ($ user→岗位)在0)……@foreach

试试这个

@if ($user->posts->isNotEmpty())
@foreach ($user->posts as $item)
<div class="col-4">
<img src="/storage/{{$post->image}}" class="w-100" style="height:100%">
</div>
@endforeach
@endif

最新更新