如何在刀片模板的 foreach 循环中访问 laravel 集合?

我想从表中的用户名信息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)方法转储了结果并得到了这个:

http://img2.mukewang.com/60e806be00016ff904760651.jpg

我在刀片模板中使用此代码来检索但在该$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


蛊毒传说
浏览 173回答 3
3回答

繁华开满天机

你没有正确定义你的关系。App\User应该在模型中定义:public function pages(){&nbsp; &nbsp; // have to specify the key name is author, not user_id&nbsp; &nbsp; return $this->hasMany("App\Page", "author");}然后在App\Page模型中你有这个:public function pageauthor(){&nbsp; &nbsp; // have to specify the key name is author, not user_id&nbsp; &nbsp; return $this->belongsTo("App\User", "author");}然后在您的 Blade 模板中,您只需引用$page->author->name:@foreach ($pages as $page)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <tr>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <th>{{ $page->id }}</th>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <th>{{ $page->title }}</th>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <th>{{ $page->pageauthor->name }}</th>...

心有法竹

像这样的东西应该可以帮助你您的控制器$pages = \App\page::all();foreach ($pages as $page) {&nbsp; &nbsp; $user = \App\user::find($page->author)->pluck('name');&nbsp; &nbsp; if(isset($user)){&nbsp; &nbsp; &nbsp; $page['user_name'] = $user->name;&nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp;$page['user_name'] = 'N/A';&nbsp; &nbsp; &nbsp;}}return view('admin.pages', compact('pages'));你的看法&nbsp; &nbsp; @foreach ($pages as $page)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <tr>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <th>{{ $page->id }}</th>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <th>{{ $page->title }}</th>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <th>{{ $page->user_name) }}</th>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @if($page->status = 'ACTIVE')&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <th><span class="label label-success">{{ $page->status }}&nbsp;&nbsp; &nbsp; </span></th>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @elseif($page->status = 'DRAFT')&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <th><span class="label label-warning">{{ $page->status }}&nbsp;</span></th>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;@endif&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <th>{{ $page->Actions }}</th>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </tr>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @endforeach代码与您当前的代码有何不同1) 它在主集合本身 ($pages) 中写入用户名,从而让您不必担心在视图中只循环遍历一个集合2)它使用laravel,find()因为页面只能有作者(我假设)(注意:find()基于您的主键工作,在这种情况下,它将搜索最常见的主键列id列)3)它使用 pluck()
打开App,查看更多内容
随时随地看视频慕课网APP