我需要将 ID 存储在 laravel 中的选定行中

i need to store the id in a selected row in laravel

$typ= category3::where('type_name', '=', $request->name)->where('category','=',$request->category)->get();

以上是文件中 select 来自 table 类别 3 的行的代码。

现在我应该怎么做才能将 selected 行的 ID 存储到名为 $type_id 的变量中?

要从这些行中获取 ID,只需使用 foreach 并访问您想要使用的 属性 即可。

foreach($typ as $type){
echo $type->id;
}

使用pluck()方法select一列。示例:

$typ = category3::where('type_name', '=', $request->name)
    ->where('category','=',$request->category)
    ->pluck('id');

它会 return 只有与您的查询匹配的行中 id 的值,如果有多个行与您的查询匹配,它会 return 第一行的结果。您可以将 $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

它将return 与查询匹配的数据集合。检索 blade 文件

中的 ID
@foreach($typ as $type_id)
    {{ $type_id->id }}
@endofreach