Diego Cea López
Diego Cea López

Reputation: 25

Laravel 5 - Route with parameter not rendering the view correctly

So I have this routes defined in my routes.php file.

Route::group(['prefix' => 'personaje'], function () {

    Route::get('{pjid}', 'PersonajeController@verPj');
    Route::get('editar/{pjid}', 'PersonajeController@editarPj');
});

And this is my controller functions:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Auth;
use DB;

class PersonajeController extends Controller {

    public function editarPj($pjId) {

        if (Auth::check())
        {   
            $usuario = Auth::user();
            $id = $usuario->id;
            $personaje = DB::table('personajes')->where('idusuario', '=', $id)->where('id', '=', $pjId)->get();
            return view('editarpj', ['pj' => $personaje]);
        }

        return redirect('/login');
    }

    public function verPj($pjId) {

        if (Auth::check())
        {   
            $usuario = Auth::user();
            $id = $usuario->id;
            $personaje = DB::table('personajes')->where('idusuario', '=', $id)->where('id', '=', $pjId)->get();
            return view('verpj', ['pj' => $pjId]);
        }
        return redirect('/login');
    }
}

When I try to access' localhost/personaje/5' for example, the view will not render using the blade template. It appears like this:

https://gyazo.com/689f64007e239e072c56b8307278d178

But if I remove the parameter in the routes.php file and the controller functions, when I try to access 'localhost/personaje/', the view renders correctly, like this:

https://gyazo.com/8fbb7166df1b4aea3cd12748ad25df2e

The view only has a navbar and that's it, it doesn't use the parameter for anything yet. Why does this happen? I have been messing with this for days and I can't find a solution.

Thanks.

Upvotes: 1

Views: 930

Answers (2)

Black-Phoenix
Black-Phoenix

Reputation: 11

I don't know if it's still relevant, but for those who still seek the solution:
when linking your CSS or JS instead of

<link rel="stylesheet" href="css/myCSS.css"> <script type="text/javascript" src="js/myJS.js"></script>

do

<link rel="stylesheet" href={{ asset("css/myCSS.css") }}> <script type="text/javascript" src={{ asset("js/myJS.js") }}></script>

Had the same problem recently, this is what solved it.
Cheers!

Upvotes: 1

user1669496
user1669496

Reputation: 33048

The first route is likely catching both routes.

Try

Route::group(['prefix' => 'personaje'], function () {
    Route::get('editar/{pjid}', 'PersonajeController@editarPj');
    Route::get('{pjid}', 'PersonajeController@verPj');
});

It's good practice to place the most specific routes on top so the more generic routes don't get in the way.

Upvotes: 1

Related Questions