Return View or Return Redirect

Published 6 months ago by Sun

My original url looks like this: http://localhost:8888/textapp/public/receivers?organization_id=2

In my receiver controller, I have code below. Which is working, but the success message can't show up. Also, the url will change to http://localhost:8888/textapp/public/receivers. Which is not correct.

return view('receivers.index', compact('organization', 'receivers'))->with('success', $user->phone_number . ' created successfully.');

If I do the code below. The page will not work, it will just say "Oops, something goes wrong".

redirect->route('receivers.index', compact('organization', 'receivers'))->with('success', $user->phone_number . ' created successfully.');

Any ideas on how to fix this? I am assuming my route list is not right. I should have something like (/organization/receivers/{id}/, 'ReceiverController')?

Route::get('receivers/find', '[email protected]')->name('receivers.find');

Route::resource('receivers', 'ReceiverController');
Best Answer (As Selected By Sun)
Cronix

The parameters come from the array that you're sending to the route.

return redirect()
    ->route('receivers.index', ['organization_id' => $request->organization_id]);
Cronix
Cronix
6 months ago (796,540 XP)

You're not redirecting the right way. Is the route name the same as the view name? You're using receivers.index for both.

return redirect()
    ->route('receivers.index', compact('organization', 'receivers'))
    ->with('receivers.index', $user->phone_number . ' created successfully.');

That redirects to a route with the name receivers.index, which should go to the [email protected] method in your Route::resource() route.

https://laravel.com/docs/5.6/redirects#redirecting-named-routes

To see your full route list, use artisan route:list from the cli to list them all (it will show all individual routes for the Resource route, as well as the Auth routes and stuff)

Sun
Sun
6 months ago (3,600 XP)

@Cronix Thank you for helping me again. I think my logic is messed up. Here is the workflow: As an admin, I can choose an organization, then go to the organization index page to CRUD the receiver list for the organization.

The first step I did is create a find function in my ReceiverController, which is asking me which organization do you want to see.

public function find()
    {
        $organizations = Organization::pluck('organization_name', 'id')->except([1]);
        return view('receivers.find', compact('organizations'));
    }

The second step is going to the index page with my index function, the organization_id will be the one I passed from my first step. The url will be like http://localhost:8888/textapp/public/receivers?organization_id=2

public function index(Request $request)
    {
        $organization = Organization::findOrFail($request->organization_id);
        $receivers = User::where('organization_id', '=', $request->organization_id)->where('role_id', '=', 5)->get();

        return view('receivers.index', compact('receivers', 'organization'));
    }

The third step is add a receiver, then add a new receiver, after save the receiver, I used the store function for that.

public function store(Request $request)
    {
        $user = User::create($request->all());

        $user->save();

        $organization = Organization::findOrFail($request->organization_id);

        $receivers = User::where('organization_id', '=', $organization->id)->where('role_id', '=', 5)->get();

//        return redirect()
//            ->route('receivers.index', compact('organization', 'receivers'))
//            ->with('success', $user->phone_number . ' created successfully.');

        return view('receivers.index', compact('organization', 'receivers'))->with('success', $user->phone_number . ' created successfully.');
    }

But it is not returning me to the index page with my message showing up.

Sun
Sun
6 months ago (3,600 XP)

@Cronix Do you think I should create a new function to replace the index function? I feel my logic is not right.

Cronix
Cronix
6 months ago (796,540 XP)

I think in your store() method, you want to return back() to the previous page (where the form was) and not return a view?

see return back();

https://laravel.com/docs/5.6/redirects#creating-redirects

Also you don't need $user->save():

$user = User::create($request->all());

// you already created and saved the user above,
// so you're running a 2nd query doing the same save.
// $user->save();
Sun
Sun
6 months ago (3,600 XP)

@Cronix Return back() will store the information and return to the create view instead of index view.

Cronix
Cronix
6 months ago (796,540 XP)

You didn't answer whether you tried my first post?

return redirect()
    ->route('receivers.index', compact('organization', 'receivers'))
    ->with('receivers.index', $user->phone_number . ' created successfully.');
Sun
Sun
6 months ago (3,600 XP)

Yes, the URL is showing the right link too, http://localhost:8888/textapp/public/receivers?organization=2 But it says "Sorry, the page you are looking for could not be found."

Sun
Sun
6 months ago (3,600 XP)

@Cronix Found the problem. When I store the information, the redirect url is http://localhost:8888/textapp/public/receivers?organization=2, but it should be http://localhost:8888/textapp/public/receivers?organization_id=2. I am trying to fix it right now, then close this question. Thank you very much!

Cronix
Cronix
6 months ago (796,540 XP)

You're welcome

Sun
Sun
6 months ago (3,600 XP)

@Cronix I tried different ways, but it is still showing me organization=2 instead of organization_id=2. Do you know where I can adjust that?

Cronix
Cronix
6 months ago (796,540 XP)

The parameters come from the array that you're sending to the route.

return redirect()
    ->route('receivers.index', ['organization_id' => $request->organization_id]);
Sun
Sun
6 months ago (3,600 XP)

@Cronix Thank you! It is working!!! I was surprised that I don't need to have compact('organization', 'receivers'), and it is still working, I will close this question. If you have time, do you know what is the right syntax to include both ['organization_id' => $request->organization_id] and compact('organization', 'receivers')? If not, that's fine too. I will just keep play around and figure that out. Thank you for your time today!

Cronix
Cronix
6 months ago (796,540 XP)

You don't need to pass them anyway because they are retrieved in the index method, based on organization_id passed in via $request.

You have in public function index(Request $request)

$organization = Organization::findOrFail($request->organization_id);

$receivers = User::where('organization_id', '=', $request->organization_id)
    ->where('role_id', '=', 5)
    ->get();

But, to answer the question, you could do it several ways. compact() just makes an associative array, and the parameters sent to the route need to be an associative array.

return redirect()
    ->route('receivers.index', [
        'organization_id' => $request->organization_id,
        'organization' => $organization,
        'receivers' => $receivers
]);

another way using compact()...

$organization_id[] = $request->organization_id;

 return redirect()
    ->route('receivers.index', compact('organization_id', 'organization', 'receivers'));

However, since your index() method retrieves them anyway, you don't need to send them. They'll (organizations and receivers) be ignored.

Sun
Sun
6 months ago (3,600 XP)

@Cronix Got it! Thank you for your detailed explaination. I learnt a lot these couple days. Thank you for your support! Now, I need to do some independent thinkings, if I have questions can’t figure out forever, then I will come back. Thank you!

Cronix
Cronix
6 months ago (796,540 XP)

@yuzesun I admire your willingness to learn and try to figure things out on your own and asking questions only if you really get stuck. That's what good coders do, and is basically what the job requires... problem solving skills. That's all coding is... solving problems. If you get stuck, we're happy to help.

Please sign in or create an account to participate in this conversation.