Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

melx's avatar
Level 4

insert selected id into another table

I have already created my customer into database, in my HTML form i managed to select those customer

I want to insert customer_id into another, because this customer_id is carry multiple data/input into customersales table.

my code below is inserted the id but it create another id into customeraddresses table. how can i insert without create another customer_id??

My code i tried

                $data=$request->all();
        $lastid=customeraddress::create($data)->id;
        if(count($request->product_name) > 0)
        {
            foreach($request->product_name as $Customeraddress=>$v){
                $data2=array(
                    'customer_id'=>$lastid,
                    'payment_status'=>$request->payment_status[$Customeraddress],
                    'product_name'=>$request->product_name[$Customeraddress],
                    'quantity'=>$request->quantity[$Customeraddress],
                    'price'=>$request->price[$Customeraddress],
                    'amount'=>$request->amount[$Customeraddress],
                );

                customer::insert($data2);
0 likes
9 replies
munazzil's avatar

I think you have to get current auth user then simply change below line as follow $lastid=customeraddress::create($data)->id;

    $lastid=customeraddress::create($data)->auth::user()->id;
melx's avatar
Level 4

@MUNAZZIL - @munazzil i get an error "syntax error, unexpected '::' (T_PAAMAYIM_NEKUDOTAYIM)"

                $data=$request->all();
        $lastid=customeraddress::create($data)->auth::user()->id;
        if(count($request->product_name) > 0)
        {
            foreach($request->product_name as $Customeraddress=>$v){
                $data2=array(
                    'customer_id'=>$lastid,
                    'payment_status'=>$request->payment_status[$Customeraddress],
                    'product_name'=>$request->product_name[$Customeraddress],
                    'quantity'=>$request->quantity[$Customeraddress],
                    'price'=>$request->price[$Customeraddress],
                    'amount'=>$request->amount[$Customeraddress],
                );

                customer::insert($data2);
melx's avatar
Level 4

@DIPAKRATANIAGILE - php 5.6.4

       {
         "name": "laravel/laravel",
         "description": "The Laravel Framework.",
         "keywords": ["framework", "laravel"],
          "license": "MIT",
         "type": "project",
          "require": {
          "php": ">=5.6.4",
     "barryvdh/laravel-dompdf": "^0.8.2",
    "laravel/framework": "5.4.*",
    "laravel/tinker": "~1.0",
    "laravelcollective/html": "^5.4",
    "spatie/laravel-permission": "1.3"
          },
munazzil's avatar

Try with below code,

    $lastid=customeraddress::create($data)->\auth()->user()->id;
melx's avatar
Level 4

@MUNAZZIL - @munazzil , is this code will take the current selected customer_id and insert into another table?? right?

i get the error while running that code

      syntax error, unexpected '\' (T_NS_SEPARATOR), expecting identifier (T_STRING) or variable (T_VARIABLE) or '{' or '$'
munazzil's avatar

Yes just remove the front \ and check it auth()->user()->id; means current user,

 $lastid=customeraddress::auth()->user()->id->create($data);

else

 $lastid=customeraddress::\auth()->user()->id->create($data);
melx's avatar
Level 4

error

       Call to undefined method Illuminate\Database\Query\Builder::auth()

i used this

         $lastid=customeraddress::auth()->user()->id->create($data);

and the second i get this error

       syntax error, unexpected '\' (T_NS_SEPARATOR)
munazzil's avatar

Then use first one with below one in top of your controller,

  use Auth;

Please or to participate in this conversation.