亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何在刀片模板的 foreach 循環中訪問 laravel 集合?

如何在刀片模板的 foreach 循環中訪問 laravel 集合?

PHP
蠱毒傳說 2021-07-06 16:49:27
我想從表中的用戶名信息Users與筆者id從表S = Pages。為此,我做了一個foreach循環并將數據添加到users數組中。這會獲取數據,但其格式不正確,無法在刀片模板中使用。下面是代碼:$pages = \App\page::all();    $users = array();    foreach ($pages as $page) {        $user = \App\user::where('id', $page->author)->get('name');        $users[] = $user;    }           dd($users);    return view('admin.pages', compact('pages', 'users'));我已經用dd($users)方法轉儲了結果并得到了這個:我在刀片模板中使用此代碼來檢索但在該$users[$loop->index]->get('name')位置獲得空白。@foreach ($pages as $page)            <tr>              <th>{{ $page->id }}</th>              <th>{{ $page->title }}</th>              <th>{{ $users[$loop->index]->get('name') }}</th>              @if($page->status = 'ACTIVE')              <th><span class="label label-success">{{ $page->status }} </span></th>              @elseif($page->status = 'DRAFT')              <th><span class="label label-warning">{{ $page->status }} </span></th>              @endif              <th>{{ $page->Actions }}</th>            </tr>            @endforeach
查看完整描述

3 回答

?
繁華開滿天機

TA貢獻1816條經驗 獲得超4個贊

你沒有正確定義你的關系。App\User應該在模型中定義:


public function pages()

{

    // have to specify the key name is author, not user_id

    return $this->hasMany("App\Page", "author");

}

然后在App\Page模型中你有這個:


public function pageauthor()

{

    // have to specify the key name is author, not user_id

    return $this->belongsTo("App\User", "author");

}

然后在您的 Blade 模板中,您只需引用$page->author->name:


@foreach ($pages as $page)

            <tr>

              <th>{{ $page->id }}</th>

              <th>{{ $page->title }}</th>

              <th>{{ $page->pageauthor->name }}</th>

...


查看完整回答
反對 回復 2021-07-09
?
心有法竹

TA貢獻1866條經驗 獲得超5個贊

像這樣的東西應該可以幫助你


您的控制器


$pages = \App\page::all();

foreach ($pages as $page) {

    $user = \App\user::find($page->author)->pluck('name');

    if(isset($user)){

      $page['user_name'] = $user->name;

    } else {

       $page['user_name'] = 'N/A';

     }

}

return view('admin.pages', compact('pages'));

你的看法


    @foreach ($pages as $page)

            <tr>

              <th>{{ $page->id }}</th>

              <th>{{ $page->title }}</th>

              <th>{{ $page->user_name) }}</th>

              @if($page->status = 'ACTIVE')

              <th><span class="label label-success">{{ $page->status }} 

    </span></th>

              @elseif($page->status = 'DRAFT')

              <th><span class="label label-warning">{{ $page->status }} 

</span></th>

               @endif

              <th>{{ $page->Actions }}</th>

            </tr>

            @endforeach

代碼與您當前的代碼有何不同


1) 它在主集合本身 ($pages) 中寫入用戶名,從而讓您不必擔心在視圖中只循環遍歷一個集合


2)它使用laravel,find()因為頁面只能有作者(我假設)(注意:find()基于您的主鍵工作,在這種情況下,它將搜索最常見的主鍵列id列)


3)它使用 pluck()


查看完整回答
反對 回復 2021-07-09
  • 3 回答
  • 0 關注
  • 188 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號