2

I have a class like below :

class Giving extends Controller
{
    public function test()
    {
        $name; // i want to access $name from function below
        $email; // i want to access $email from function below
        return view('route/routes', [
            'name' => $name,
            'email' => $email
        ]);
    }

    public function sendGiving()
    {
        $name = $this->request->name;
        $email = $this->request->email;
    }
}

function sendGiving receives its value from ajax post (at blade view) :

data = {
    _token: '{{ csrf_token() }}',
    name: $('#txtName').val(),
    email: $('#txtEmail').val(),
    phone_number: $('#txtPhnum').val(),
}

$.ajax({
    url: "{{ route('sendGiving') }}",
    type: 'POST',
    data: data,
    dataType: 'JSON',
    success: function (response) {
        //do something
    }
});

Routes :

Route::get('/givingtest','Giving@test')->name('test');
Route::post('/givingsend', 'Giving@sendGiving')->name('sendGiving');

how can i access value from sendGiving() function to test() function ? Or can i make a second ajax post call in the view to send data to test() function ?

4
  • 1
    The only way to get those values for the subsequent request is to use Session. Store the values in session and get in the subsequent request Commented Jul 28, 2020 at 7:28
  • When do you want to access name and email? If you want to access them in another request you need to persist the values, maybe in DB or session Commented Jul 28, 2020 at 7:29
  • You can access one function from another by $this->test();. In your code you want to access variable from function test() where the variable are $email & $name, but both are undefined. So you cant do that. I saw, you gonna access /givingtest variable on route /givingsend in this case you need to send variable with form method from /givingtest to /givingsend route. Then you can access from function. Commented Jul 28, 2020 at 7:30
  • You can access the whole function or give them to constructor Commented Jul 28, 2020 at 7:34

1 Answer 1

1

As far as I understand your question I can see (at least) three ways here.

1. Use Session

public function sendGiving()
{
    session(['name' => $this->request->name, 'email' => $this->request->email]);
}

public function test()
{
    $name = session('name');
    $email = session('email');
    return view ('route/routes', compact('name', 'email'));
}

2. Pass the data to your function directly

Define givingtest as post route and just send the data to that function as well. So you can just do:

public function test()
{
    $name = $this->request->name;
    $email = $this->request->email;
    return view ('route/routes', compact('name', 'email'));
}

3. Persist the data in the database

If the data of sendGiving() is already valid and worthy to keep, why not save it in a database right away?

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.