我为刀片视图创建一个数组。从表中收集数据并存储在数组$post_data
中。对于laravel生成的数组,foreach
循环将完成此工作。但是,与自己创建的数组似乎有些不同。请帮忙。
我使用2 foreach
,因为该数组由二维数组组成。
The result of dd($post_data)
The result of var_dump($data)
The result of var_dump($item)
错误:尝试获取非对象的属性“ title”
控制器
$RMM = DB::table('companies')->where('branch', 'RMM')->get();
foreach ($RMM as $RMM)
{
$post = post::find($RMM->id);//get post for each department
if (isset($post))
{
$post_data[] = array('title' => $post->article_title,
'name' => $RMM->department.'@'.$post->author,
'date' => date('Y-m-d', strtotime($post->date)),
);
}
}
刀片
@foreach($post_data as $key => $data)
@foreach ($data as $item)
<div class="col bg-light ">
<a class="nav-link pb-0 pt-0" href="#">{{ $item->title }}</a>
<a class="nav-link pb-0 pt-0" href="#">{{ $item->name }}
@ {{$item->department}}</a>
</div>
@endforeach
@endforeach
公司
```migrations```
Schema::create('companies', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('department');
$table->string('branch');
$table->timestamps();
});
发布
Schema::create('posts', function (Blueprint $table) {
$table->bigIncrements('id');
$table->timestamps();
$table->unsignedBigInteger('user_id');
$table->unsignedBigInteger('company_id');
$table->string('event_title')->nullable();
$table->string('article_title')->nullable();
$table->string('date');
$table->string('author')->nullable();
$table->string('article')->nullable();
$table->string('description')->nullable();
$table->string('file')->nullable();
$table->string('banner_image')->nullable();;
$table->string('gallery_image')->nullable();
$table->index('user_id');
$table->index('company_id');
});
答案 0 :(得分:0)
问题是您正试图访问一个非对象属性,如错误所述。
item
不是对象,而是数组。
所以您必须使用数组访问方法来访问其值。
@foreach($post_data as $key => $data)
@foreach ($data as $item)
<div class="col bg-light ">
<a class="nav-link pb-0 pt-0" href="#">{{ $item['title'] }}</a>
<a class="nav-link pb-0 pt-0" href="#">{{ $item['name'] }}
@ {{$item['department'] }}</a>
</div>
@endforeach
@endforeach
更新
通过查看$post_data
的转储,您不必再次循环数组的每个元素。
@foreach($post_data as $key => $data)
<div class="col bg-light ">
<a class="nav-link pb-0 pt-0" href="#">{{ $data['title'] }}</a>
<a class="nav-link pb-0 pt-0" href="#">{{ $data['name'] }}</a>
</div>
@endforeach
答案 1 :(得分:0)
如果只想获得标题,名称和日期,那很简单
控制器
post_data = Array();
$RMM = DB::table('companies')->where('branch', 'RMM')->get();
foreach ($RMM as $RMM)
{
$post = post::find($RMM->id);//get post for each department
if (isset($post))
{
$post_data[] = array(
'title' => $post->article_title,
'name' => $RMM->department.'@'.$post->author,
'date' => date('Y-m-d', strtotime($post->date)),
);
}
}
// You can send a array of objects
return view('example')->with('posts', $post_data);
查看
@foreach($posts as $post)
<div class="col bg-light ">
<a class="nav-link pb-0 pt-0" href="#">{{ $item['title'] }}</a>
<a class="nav-link pb-0 pt-0" href="#">{{ $item['name'] }}
@ {{$item['department']}}</a>
</div>
@endforeach
答案 2 :(得分:0)
我纠正了。感谢您的所有意见和建议。这是我的方法。
$post_data[]
是正确的,因为我需要存储find
函数的很多返回结果。因为find
已经返回了一个对应ID的数组,所以我不需要放入另一个数组,只需按原样使用它即可,如@Ghost所述。因此,一个foreach
足以完成工作。
控制器
$RMM = DB::table('companies')->where('branch', 'RMM')->get();
foreach ($RMM as $RMM)
{
$post = post::find($RMM->id);//get post for each department
if (isset($post))
{
$post_data[] = $post;
}
return view('home')->with('posts', $post_data);
}
刀片
@foreach($posts as $post)
<div class="col bg-light ">
<a class="nav-link pb-0 pt-0" href="#">{{ $item['title'] }}</a>
<a class="nav-link pb-0 pt-0" href="#">{{ $item['name'] }}
@ {{$item['department']}}</a>
</div>
@endforeach
答案 3 :(得分:0)
更新答案 另一种方法是将数组转换为对象。该错误与非对象有关,直接对其进行处理将有所帮助。
错误:试图获取非对象的属性“ title”。
替代方式
$RMM = DB::table('companies')->where('branch', 'RMM')->get();
foreach ($RMM as $RMM)
{
$post = post::find($RMM->id);//get post for each department
if (isset($post))
{
$post_data[] = array('title' => $post->article_title,
'name' => $RMM->department.'@'.$post->author,
'date' => date('Y-m-d', strtotime($post->date)),
);
}
}
//change to object before send to blade
$object = json_decode(json_encode($post_data), FALSE);
return view('home')->with('posts', $object);