skip to Main Content

After data from a form is saved i wanted to get back to the admin page.
I checked the database and the new data was there but I got an error:
"Route [pages.admin] not defined."

My Admin Controller code:

<?php

namespace AppHttpControllers;

use IlluminateHttpRequest;
use IlluminateSupportFacadesValidator;
use IlluminateSupportFacadesDB;
use IlluminateSupportFacadesAuth;
use AppModelsAdmin;


class AdminController extends Controller
public function store(Request $request)
    {
        // Validation code

        // Saveing code

        return redirect()->route('pages.admin')
                         ->with('success', 'Admins created successfully.');
    }

My Page Controller:

<?php

namespace AppHttpControllers;
use IlluminateHttpRequest;

class PagesController extends Controllerpublic 
function admin(){
        return view('pages.admin');
    } 

Routes:

Route::get('/admin', 'PagesController@admin');
Route::post('admin_form', 'AdminController@store');

Would appreciate the help.

I looked in online sources but it didn’t help

2

Answers


  1. Chosen as BEST ANSWER

    I found a video and changed my code in the controller to

    return redirect('admin');
    

    and it worked.


  2. You are confusing the name of a view with the name of a route. Your view has the name pages.admin because there is a admin.blade.php view in the pages folder within the views folder of your application.

    For route('pages.admin') to work, you need to assign a name to a route. You may do this by using name() when defining your route.

    Route::get('/admin', 'PagesController@admin')->name('pages.admin');
    

    It is a good practise to always name routes. For example: it allows you to just change the url without having to worry about your redirects breaking, since they use the name that hasn’t changed.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search