shavindip
shavindip

Reputation: 622

Laravel: Get id from database in controller and update data

I need to get the id from the $client and send the id to insert it into the database.

 $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}]

Anyone have an idea how to get id from $client?

Upvotes: 0

Views: 9325

Answers (2)

Salman Zafar
Salman Zafar

Reputation: 4035

You can use value method of query builder this will work fine have a look:

      $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();

Or First of method query builder:

$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();

Upvotes: 2

Mayuri Pansuriya
Mayuri Pansuriya

Reputation: 954

try

$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();

Upvotes: 4

Related Questions