$typ= category3::where('type_name', '=', $request->name)->where('category','=',$request->category)->get();
上面是文件中的代码,用于从表类别3中选择行。
现在该怎么做才能将所选行的id存储到名为$ type_id的变量中?
答案 0 :(得分:1)
使用pluck()
方法选择一列。例如:
$typ = category3::where('type_name', '=', $request->name)
->where('category','=',$request->category)
->pluck('id');
它只返回与您的查询匹配的行中id
的值,如果多行与您的查询匹配,它将返回第一行的结果。您可以将$typ
用作$type_id
,也可以将查询结果存储在$type_id
而不是$typ
。
或者您可以将get()
方法中的数组中的所有必需列名称作为参数传递。例如:
$typ= category3::where('type_name', '=', $request->name)
->where('category','=',$request->category)
->get(['id','otherIfNeeded']); // if no parameter it will return all column's value
它将返回与查询匹配的数据集合。要检索blade
文件
@foreach($typ as $type_id)
{{ $type_id->id }}
@endofreach
答案 1 :(得分:0)
要从这些行中获取ID,只需使用foreach并访问您要使用的属性即可。
foreach($typ as $type){
echo $type->id;
}