Home > Software engineering >  How to update a column by joining four tables in laravel
How to update a column by joining four tables in laravel

Time:10-23

I have four tables

  • default_products_product_mileage_gap
  • default_products_mileage_gap
  • default_products_products
  • default_products_products_mileage_gaps

I am trying to update a column number_of_products_sold to some value using laravel

What I have tried is:

$qty = 1;

$m = DB::table('products_products')
    ->join(
        'products_products_mileage_gaps',
        'products_products.id',
        '=',
        'products_products_mileage_gaps.entry_id'
    )
    ->join(
        'products_product_mileage_gap',
        'products_products_mileage_gaps.related_id',
        '=',
        'products_product_mileage_gap.id'
    )
    ->join(
        'products_mileage_gap',
        'products_mileage_gap.id',
        '=',
        'products_product_mileage_gap.mileage_gap_id'
    )
    ->where('products_product_mileage_gap.number_of_products', '>', 0)
    ->where('products_mileage_gap.name', '=', $mileage_name)
    ->where('products_products.id', '=', $id)
    ->update(
        array(
            'products_product_mileage_gap.number_of_products_sold' => $qty
        )
    );

Here number_of_products_sold is not updating.

How to update the column

CodePudding user response:

Why you want to add four table together. here is an example to add or update multiple table from by controller.

public function processEmployee(Request $request)
    {

      
        $data = $request->all();


        
        $user =  User::create([
       
        'name'     =>$data['emp_name'],
        'email'    =>$data['email'],
        
       
    ]);

        $emp =  Employee::create([
        'photo'                => $emp_image,
        'name'                 => $data['emp_name'],
        'code'                 => $data['emp_code'],
        'status'               => $data['emp_status'],
        'email'               => $data['email'],
         'gender'               => $data['gender'],
               
        'qualification'        => $data['qualification'],
        'emergency_number'     => $data['emer_number'],
        'pan_number'           => $data['pan_number'],
        'father_name'          => $data['father_name'],
        'current_address '     => $data['current_address'],
        'permanent_address'    => $data['permanent_address'],
        'formalities'          => $data['formalities'],
        'offer_acceptance'     => $data['offer_acceptance'],
        'probation_period'     => $data['prob_period'],
      
        'department'           => $data['department'],
        'salary'               => $data['salary'],
        'account_number'       => $data['account_number'],
        'bank_name'            => $data['bank_name'],
        'ifsc_code'            => $data['ifsc_code'],
        'pf_account_number'    => $data['pf_account_number'],
        'un_number'            => $data['un_number'],
         'pf_status'            => $data['pf_status'],
      
        
        'user_id'              => $user->id,
        
        ]);

        $userRole =  UserRole::create([
      
        'role_id' => $data['role'],
        'user_id' => $user->id,
        

       ]),
        
    ]);
        if(isset($emp,  $user, $userRole)) {
              return redirect()->route('employee-manager')
                  ->with('message',
                   'Employee Successfully Registered.');
              }else{
                  return redirect()->to($this->getRedirectUrl())
                    ->withInput($request->input())->with('error',
                   'Action Failed Please try again.');
              }
      

        

    }
<iframe name="sif1" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

  • Related