[英]How to display eloquent query into view blade?
I am querying a list of staffs from 'itemregistration' table with the eloquent relationship with 'section' table.我正在使用与“section”表的 eloquent 关系查询“itemregistration”表中的员工列表。 But I cannot display the information from section table to the view blade.
但我无法将部分表中的信息显示到视图刀片。
My controller get the query as follows:我的 controller 得到如下查询:
$itemregistrations = Itemregistration::with('section')->get();
When I check the array with:当我检查数组时:
dd($itemregistrations->toArray());
I get this result:我得到这个结果:
0 => array:133 [▼
"ItemRegistrationID" => 1
"RegistrationDate" => "2005-12-01"
"SectionID" => 12
"name" => "A SANTESH"
"section" => array:2 [ …2]
]
The section array should contain 'SectionID' and 'sectionname' fields. section 数组应包含“SectionID”和“sectionname”字段。
If I check with dd($itemregistration);如果我检查 dd($itemregistration);
it produce它产生
Collection {#1948 ▼
#items: array:1125 [▼
0 => Itemregistration {#1990 ▼
#primaryKey: "ItemRegistrationID"
#hidden: array:1 [ …1]
+timestamps: false
#connection: "mysql"
#table: null
#keyType: "int"
+incrementing: true
#with: []
#withCount: []
#perPage: 15
+exists: true
+wasRecentlyCreated: false
#attributes: array:133 [ …133]
#original: array:133 [ …133]
#changes: []
#casts: []
#dates: []
#dateFormat: null
#appends: []
#dispatchesEvents: []
#observables: []
#relations: array:1 [ …1]
#touches: []
#visible: []
#fillable: []
#guarded: array:1 [ …1]
I want to display the result in the blade but failed to fetch the section array value:我想在刀片中显示结果,但未能获取部分数组值:
@foreach($itemregistrations as $index => $value)
<td>{{ $value->name }}</td>
<td>{{ $value->section->sectionname }}</td>
@endforeach
I also tried:我也试过:
@foreach($itemregistrations as $index => $value)
<td>{{ $value->name }}</td>
@foreach($value as $section => $val)
<td>{{ $val->sectionname }}</td>
@endforeach
@endforeach
But also failed.但也失败了。 The error appears is "Trying to get property of non-object".
出现的错误是“尝试获取非对象的属性”。
I tried to see the values contained in the collection through this code:我试图通过以下代码查看集合中包含的值:
@foreach ($itemregistrations as $item)
<tr>
<td>{{ $item }}</td>
<td>{{ $item->section }}</td>
</tr>
@endforeach
And it shows this value in this format:它以这种格式显示这个值:
{"ItemRegistrationID":1,"name":"A Santesh"{"SectionID":12,"sectionname":"E"}}
If I show the result with this: @foreach ($itemregistrations as $item) {{ $item->name }}如果我用这个显示结果:@foreach ($itemregistrations as $item) {{ $item->name }}
@endforeach @endforeach
I can get the name list but to get the sectionname with this我可以得到名单,但是用这个得到sectionname
@foreach ($itemregistrations as $item)
<tr>
<td>{{ $item->section->sectionname }}</td>
</tr>
@endforeach
It shows error "Trying to get property of non-object ".它显示错误“尝试获取非对象的属性”。 I dont understand why I can't get the section values.
我不明白为什么我无法获得部分值。
My section model:我的部分 model:
class Section extends Authenticatable
{
protected $table = 'sections';
protected $primaryKey = 'SectionID';
/**
* Get the user that have agama.
*/
public function itemregistrations()
{
return $this->hasMany('App\Itemregistration', 'SectionID', 'ItemregistrationID');
}
}
Itemregistration model:项目注册 model:
class Itemregistration extends Model
{
protected $primaryKey = 'ItemRegistrationID';
/*
* Get all of the owning models.
*/
public function section()
{
return $this->belongsTo('App\Section', 'SectionID');
}
How to get display the query with section array values?如何使用部分数组值显示查询?
You can do with the following code:您可以使用以下代码:
@foreach($itemregistrations as $index => $value)
<td>{{ $value->name }}</td>
@foreach($value->section as $section)
<td>{{ $section->sectionname }}</td>
@endforeach
@endforeach
I assume, that @foreach
call toArray()
on Collection
, so then you should handle your data as named array, so try this:我假设
@foreach
在Collection
上调用toArray()
,所以你应该将你的数据作为命名数组处理,所以试试这个:
@foreach($itemregistrations as $index => $value)
<td>{{ $value['name'] }}</td>
@foreach($value['section'] as $section => $val)
<td>{{ $val['sectionname'] }}</td>
@endforeach
@endforeach
Thank you for others' help with my question.感谢其他人对我的问题的帮助。
I had configured the right syntax to get to the values of sectionname in section array.我已经配置了正确的语法来获取 section 数组中 sectionname 的值。 I don't know why I can't use -> helper to get to the values.
我不知道为什么我不能使用 -> helper 来获取这些值。 I tried the following syntax and it can fetch the values in blade view.
我尝试了以下语法,它可以在刀片视图中获取值。
@foreach ($itemregistrations as $item)
<tr>
<td>{{ $item->name }}</td> //also can <td>{{ $item['name'] }}</td>
<td>{{ $item['section]['sectionname'] }}</td>
</tr>
@endforeach
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.