第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

為 foreach() Laravel 5.5 提供的參數(shù)無效

為 foreach() Laravel 5.5 提供的參數(shù)無效

我有兩個(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>&nbsp;<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ù)無效問題可能出在哪里?
查看完整描述

2 回答

?
哆啦的時(shí)光機(jī)

TA貢獻(xiàn)1779條經(jīng)驗(yàn) 獲得超6個(gè)贊

用戶模型將方法定義為post()但在 foreach 中您調(diào)用它->posts(復(fù)數(shù))。嘗試@foreach ($currentuser->post()->get() as $post)


查看完整回答
反對(duì) 回復(fù) 2021-06-25
?
白豬掌柜的

TA貢獻(xiàn)1893條經(jīng)驗(yàn) 獲得超10個(gè)贊

$currentuser->posts in Foreach 

you used $currentuser->posts

but "posts" is method in you model (user.php).

so use it as method.

Or use temp variable for foreach like..

$posts = $currentuser->posts();

foreach($post as $key=> $val)

{

}


查看完整回答
反對(duì) 回復(fù) 2021-06-25
  • 2 回答
  • 0 關(guān)注
  • 136 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)