Home > Mobile >  Need to pass the variable with same value from one function to another in laravel
Need to pass the variable with same value from one function to another in laravel

Time:01-03

i'm trying to pass the $customer_id variable from customer function to customerUpdate function , but i'm facing the error like 'undefined variable'

<?php

namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Crypt;
class CustomerDetailController extends Controller {
  public function customer (Request $request, $id){
      $customer_id = $id;
  }
  public function customerUpdate(Request $request) {
      dd($customer_id);
  }
}

CodePudding user response:

You can do it using a class property:

namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Crypt;

class CustomerDetailController extends Controller {
  
  private $customer_id;

  public function customer (Request $request, $id){
      $this->customer_id = $id;
  }

  public function customerUpdate(Request $request) {
      dd($this->customer_id);
  }

}

UPDATE After your comment:

As I said, you should really look into how PHP OOP works first. Then, go ahead and read about the Laravel controllers and routes and see how they work. Only then try to do something.

The way you are using the controller methods is not the way you are supposed to use them.

https://www.php.net/manual/en/language.oop5.basic.php

https://laravel.com/docs/8.x/lifecycle

https://laravel.com/docs/8.x/routing

https://laravel.com/docs/8.x/controllers

https://laravel.com/docs/8.x/requests

CodePudding user response:

One of ways is to merge your variables to $request :

     public function customer (Request $request, $id) {
      $request->merge(['customer_id' => $id]);
     }

     public function customerUpdate(Request $request) {
      dd($request->customer_id);
     }
  • Related