0

I'm trying to reuse a variable from one function to another.

public function function1($id)
{
    $data1 = DB::table('table1 as t1')
        ->select("t1.name", "t1.address", "t2.designation")
        ->join('table2 as t2', 't1.name', '=', 't2.name')
        ->where('t2.u_id', $id)
        ->get();
    //dd($data1); //Getting data.

    $data2 = Model::select('service_name', 'service_qty')->where('u_id', $id)->get();
    //dd($data2); //Getting data.

    $this->function2($id, $data1, $data2);
    
    return response()->json(array(
      'data1' => $data1,
      'data2' => $data2
    ));
}
public function function2($id, $data1, $data2)
{
    return response()->json(array(
      'data1' => $data1,
      'data2' => $data2
    ));
}

But it's throwing an error.

Too few arguments to function App\Http\Controllers\MyController::function2(), 1 passed and exactly 3 expected

I'm suspecting it's an route parameter issue.

Route::get('getData/{id}', 'MyController@function2');

Or a more elegant way to do this?

glitchy
  • 161
  • 3
  • 12
  • Take a look at [this question/answer](https://stackoverflow.com/questions/31681715/passing-multiple-parameters-to-controller-in-laravel-5) – Peppermintology Aug 07 '20 at 09:36
  • Because I have already tried passing 3 parameters in route but it gave me "Symfony\Component\HttpKernel\Exception\NotFoundHttpException" – glitchy Aug 07 '20 at 10:02
  • But this Route::get('getData/{id}/{data1}/{data2}', 'MyController@function2'); didn't work :( – glitchy Aug 07 '20 at 10:16
  • Erm... whats the point of calling function2 in function1 withouth using the data to anything? – mrhn Aug 07 '20 at 10:31
  • @mrhn $this is passing variable from one function to another to avoid code repetition, ref https://stackoverflow.com/a/55002077 – glitchy Aug 07 '20 at 10:37
  • But there is no logical sense of calling function2 when running function 1. It does exactly the same. – mrhn Aug 07 '20 at 10:37
  • Then how can I reuse those variable($data1 and $data2) in another function? – glitchy Aug 07 '20 at 10:39
  • You are not answering the question... your logic is similar, you can call other functions in the controller, but then you should not use these functions to be used as a route. – mrhn Aug 07 '20 at 10:41
  • I tried to write up an answer, but what should data1 and data2 be when calling function2 these variables are not defined on the route. – mrhn Aug 07 '20 at 10:45
  • No problem, is there any other way to achieve this? – glitchy Aug 07 '20 at 10:48

1 Answers1

1

The following works for me on a fresh install of Laravel.

web.php

// define the route to function1
Route::get('/getData/{id}', 'MyController@function1');

MyController.php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class MyController extends Controller
{
    public function function1($id)
    {
        $data1 = 'abc';
        $data2 = 'xyz';

        /**
         * Commented out as OP doesn't want function1 to call function2
         * as is shown in original question
         */
        // $this->function2($id, $data1, $data2);

        return response()->json(array(
            'data1' => $data1,
            'data2' => $data2
        ));
    }

    public function function2($id, $data1, $data2)
    {
        // This will dump out the incoming data for you to view
        ddd($id, $data1, $data2);

        return response()->json(array(
            'data1' => $data1,
            'data2' => $data2
        ));
    }
}

Note that if you want to call function2 via a URL, you'll need to define a route for it, like so:

Route::get('/getData/{id}/{data1}/{data2}', 'MyController@function2');

Edit 1

OK. So based on you not wanting function1 to call function2, I have updated my answer (removed $this->function2($id, $data1, $data2);). function1 will return your $data1 and $data2 values in your JSON response. Extract them from the response and use them to make your call to function2.

Peppermintology
  • 9,343
  • 3
  • 27
  • 51
  • Nice Explanation – STA Aug 07 '20 at 11:22
  • When calling function1 it also calls function2, I don't want that, both functions are independent, function1 is search and function2 is print, I just want to reuse that join query of search function in print function. – glitchy Aug 07 '20 at 12:20
  • @glitchy - I have made some changes based on your comment. – Peppermintology Aug 07 '20 at 12:34
  • @Unflux Now, how can I use those variables in function2? – glitchy Aug 07 '20 at 12:49
  • Well once they have been returned in your `JSON` response from calling `function1`, extract them from that response before making your next call to `function2`. We don't know the technologies you're using (Postman, axios, etc.) to make the calls to give specifics. – Peppermintology Aug 07 '20 at 13:05
  • But both are separate functions, it won't work this way. – glitchy Aug 07 '20 at 13:42
  • Why won't it work this way? `function1` returns some data in `JSON`, you use the values from within that response in your call to `function2`. – Peppermintology Aug 07 '20 at 13:51