从 blade 中的 laravel 列表输出值
Outputing values from a laravel list in blade
我在控制器中定义了一个 laravel 列表,如下所示:
$industries = Industry::lists('id', 'name');
$salaries = Salary::lists('id', 'range', 'rate');
如何输出或访问 blade 模板中的列?
我执行以下操作后出现错误 'trying to get property of non object':
@foreach ($industries as $industry)
<div class="checkbox margin-top-0 ">
<label>
{!! Form::checkbox('industry_list[]', $industry->id) !!}
{{$industry->name}}
</label>
</div>
@endforeach
我如何使用 for 循环进行迭代以及我正在尝试确定第一次迭代 - 使用下面的方法我得到偏移错误。
@for ($i = 0; $i <= count($salaries); $i++)
<div class="checkbox @if($i == 0) margin-top-0 @endif ">
<label>
{!! Form::checkbox('salary_list[]', $salaries->id) !!}
{{$salaries->name}}
</label>
</div>
@endfor
以及如何迭代 $salaries 数组 - 我是否需要将其设为 collection 因为 Salary::lists('id', 'range', 'rate');
数组仅包含两列并且奇怪的是当我执行 'dd($salaries); 时` 为什么数组定义为 'range' 值作为键,'id' 作为值,尽管它被声明为 'id' 是键?
array:33 [
"10,000 - 15,000" => 24
"15,000 - 20,000" => 25
]
恐怕您理解得 lists()
有点不对。首先,它构建了一个数组,数组有一个key和一个value。没有第三个属性的空间,因此 lists()
函数只需要两个参数:
public function lists($column, $key = null)
此外,第一个参数是 value,第二个参数是 key。这是因为您也可以仅使用数字键构建数组。
所以你应该这样做:
$industries = Industry::lists('name', 'id');
然后,在您的 foreach 循环中,您不必将其视为对象,它只是值:
@foreach ($industries as $id => $industry)
<div class="checkbox margin-top-0 ">
<label>
{!! Form::checkbox('industry_list[]', $id) !!}
{{$industry}}
</label>
</div>
@endforeach
但是,对于您的情况,我并没有真正看到使用 lists()
的好处。只需使用 Industry::all()
检索完整的模型集合,您就可以执行 $industry->id
和 $industry->name
之类的操作,您将能够处理两个以上的值。
使用:
$industries = Industry::all('id', 'name');
$salaries = Salary::all('id', 'range', 'rate');
static Collection|Model[] all(array $columns = array('*'))
Get all of the models from the database.
Parameters array $columns
Return
Value Collection|Model[]
参考:http://laravel.com/api/5.0/Illuminate/Database/Eloquent/Model.html
我在控制器中定义了一个 laravel 列表,如下所示:
$industries = Industry::lists('id', 'name');
$salaries = Salary::lists('id', 'range', 'rate');
如何输出或访问 blade 模板中的列?
我执行以下操作后出现错误 'trying to get property of non object':
@foreach ($industries as $industry)
<div class="checkbox margin-top-0 ">
<label>
{!! Form::checkbox('industry_list[]', $industry->id) !!}
{{$industry->name}}
</label>
</div>
@endforeach
我如何使用 for 循环进行迭代以及我正在尝试确定第一次迭代 - 使用下面的方法我得到偏移错误。
@for ($i = 0; $i <= count($salaries); $i++)
<div class="checkbox @if($i == 0) margin-top-0 @endif ">
<label>
{!! Form::checkbox('salary_list[]', $salaries->id) !!}
{{$salaries->name}}
</label>
</div>
@endfor
以及如何迭代 $salaries 数组 - 我是否需要将其设为 collection 因为 Salary::lists('id', 'range', 'rate');
数组仅包含两列并且奇怪的是当我执行 'dd($salaries); 时` 为什么数组定义为 'range' 值作为键,'id' 作为值,尽管它被声明为 'id' 是键?
array:33 [
"10,000 - 15,000" => 24
"15,000 - 20,000" => 25
]
恐怕您理解得 lists()
有点不对。首先,它构建了一个数组,数组有一个key和一个value。没有第三个属性的空间,因此 lists()
函数只需要两个参数:
public function lists($column, $key = null)
此外,第一个参数是 value,第二个参数是 key。这是因为您也可以仅使用数字键构建数组。
所以你应该这样做:
$industries = Industry::lists('name', 'id');
然后,在您的 foreach 循环中,您不必将其视为对象,它只是值:
@foreach ($industries as $id => $industry)
<div class="checkbox margin-top-0 ">
<label>
{!! Form::checkbox('industry_list[]', $id) !!}
{{$industry}}
</label>
</div>
@endforeach
但是,对于您的情况,我并没有真正看到使用 lists()
的好处。只需使用 Industry::all()
检索完整的模型集合,您就可以执行 $industry->id
和 $industry->name
之类的操作,您将能够处理两个以上的值。
使用:
$industries = Industry::all('id', 'name');
$salaries = Salary::all('id', 'range', 'rate');
static Collection|Model[] all(array $columns = array('*'))
Get all of the models from the database.
Parameters array $columns
Return Value Collection|Model[]
参考:http://laravel.com/api/5.0/Illuminate/Database/Eloquent/Model.html