我有兩個(gè)表,用戶和帖子表使用 Laravel 5.5 中的一對(duì)多關(guān)系鏈接在一起。我只想顯示與登錄用戶關(guān)聯(lián)的帖子。我有 PostController,我可以在其中獲取當(dāng)前用戶登錄信息并將其傳遞給視圖 index.php。在 index.php 視圖中,我嘗試使用 @foreach 獲取和循環(huán)與登錄用戶關(guān)聯(lián)的帖子。請(qǐng)參閱下面我的示例代碼。后控制器public function index(){ $currentuser = Auth::user(); return view('posts.index')->withCurrentuser($currentuser);}index.php 視圖@foreach ($currentuser->posts as $post) <tr> <td>{{ $post->id }}</td> <td>{{ $post->title }}</td> <td>{{ substr(strip_tags($post->body), 0, 40) }}{{ strlen(strip_tags($post->body))>40 ? "..." : "" }}</td> <td>{{ date('M j, Y', strtotime($post->created_at)) }}</td> <td><a href="{{ route('posts.show', $post->id) }}" class="btn btn-outline-blue btn-sm">View</a> <a href="{{ route('posts.edit', $post->id)}}" class="btn btn-sm btn-outline-blue">Edit</a></td> </tr> @endforeachUser.php 模型 protected $table = 'users'; public function post(){ return $this->hasMany('App\Post'); }Post.php 模型public function users(){ return $this->belongsTo('App\User');}我收到一個(gè)錯(cuò)誤為 foreach() 提供的參數(shù)無效問題可能出在哪里?
為 foreach() Laravel 5.5 提供的參數(shù)無效
繁星點(diǎn)點(diǎn)滴滴
2021-06-21 17:08:55