Laravel:从控制器中的数据库获取ID并更新数据

时间:2018-10-17 09:33:14

标签: database laravel laravel-query-builder

我需要从$ client获取ID,然后发送ID将其插入数据库。

 $client = DB::table('clients')->where('product_id', '=', $request->get('product'))->get();
 $invoice = new Invoice();
 $invoice->total = ($request->get('subscription'))*1.15;
 $invoice->client_id = //get id from $client;
 $invoice->save();

$ client = [{“ id”:23,“ name”:“ testname”,“ product_id”:678}]

有人知道如何从$ client获取ID吗?

2 个答案:

答案 0 :(得分:4)

尝试

$client = DB::table('clients')
            ->where('product_id', '=', $request->get('product'))
            ->first();
 $invoice = new Invoice();
 $invoice->total = ($request->get('subscription'))*1.15;
 $invoice->client_id = $client->id;
 $invoice->save();

答案 1 :(得分:2)

您可以使用查询构建器的API方法,这样看起来很好:

Spring OAuth2

或方法查询构建器的value

      $client = DB::table('clients')
         ->where('product_id', '=', $request->get('product'))
         ->value('id');
         $invoice = new Invoice();
         $invoice->total = ($request->get('subscription'))*1.15;
         $invoice->client_id = $client;
         $invoice->save();