【问题标题】:I want to get only user id without text我只想获取没有文本的用户 ID
【发布时间】:2021-03-12 04:24:10
【问题描述】:

我使用了一些 Laravel 代码,

$cid = $request->input('cusid');

$cus = DB::table('customers')->select('id')->where('cusid', $cid)->get();

在 cuctomers 表中引用“cusid”的代码选择客户“id”。

结果我得到了“[{'id': 3}]”。 但我只想得到“3”的结果。

错误:

SQLSTATE[22007]:1366 Incorrect integer value: '[{"id":3}]' for column `teaapp2`.`daily_infos`.`customer_id` at row 1 (SQL: insert into `daily_infos` (`customer_id`, `numofkillo`, `advance`, `admin_id`, `updated_at`, `created_at`) values ([{"id":3}], 10, 10, 1, 2020-11-30 08:43:15, 2020-11-30 08:43:15))

我怎么会找到它。 请帮忙。

【问题讨论】:

    标签: php mysql laravel authentication


    【解决方案1】:

    最好的方法是拨打value('id'),比如:

    DB::table('customers')->where('cusid', $cid)->value('id');
    

    查询生成器将仅自动选择id 列。另外,尽量使用 eloquent 模型,而不是直接查询表。

    【讨论】:

      【解决方案2】:

      假设此查询仅从数据库中产生一条记录,您可以将其修改为:

      $cus = DB::table('customers')->where('cusid', $cid)->pluck('id')->first();
      

      或者

      $cus = DB::table('customers')->where('cusid', $cid)->first()->id;
      

      或者,如果您有 Customer 模型:

      $cus = Customer::where('cusid', $cid)->first()->id;
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2020-12-22
        • 1970-01-01
        • 1970-01-01
        • 2012-05-03
        • 1970-01-01
        • 2016-12-03
        • 2020-01-30
        相关资源
        最近更新 更多