Home > Mobile >  problems with routes laravel
problems with routes laravel

Time:01-06

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 App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Auth;
use App\Models\Admin;


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 App\Http\Controllers;
use Illuminate\Http\Request;

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

CodePudding user response:

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.

CodePudding user response:

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

return redirect('admin');

and it worked.

  • Related