【问题标题】:i need to store the id in a selected row in laravel我需要将 id 存储在 laravel 的选定行中
【发布时间】:2016-04-30 17:01:19
【问题描述】:
$typ= category3::where('type_name', '=', $request->name)->where('category','=',$request->category)->get();

以上是文件中用于从表 category3 中选择行的代码。

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

【问题讨论】:

  • 我输入了,获取存储到变量的 id。我可以用它来将一些数据存储在另一个表中

标签: laravel laravel-4 laravel-5 laravel-5.1 laravel-routing


【解决方案1】:

要从这些行中获取 id,很简单,只需使用 foreach 并访问您要使用的属性。

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

【讨论】:

    【解决方案2】:

    使用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文件中的id

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

    【讨论】:

      猜你喜欢
      • 2019-10-07
      • 1970-01-01
      • 1970-01-01
      • 2016-06-10
      • 1970-01-01
      • 2013-05-31
      • 2016-12-28
      • 2013-07-08
      • 1970-01-01
      相关资源
      最近更新 更多