Problem with routes in laravel

0

I have slugs stored in the database of different tables, I plan to use the url always so www.exemplo.com/slug . For this I created a single route for this that looks like this:

Route::get('{slug}', 'SlugAppController@view');

This route redirects to a controller where I am trying to validate if when the person loads a category it shows view categories, if it is a user it shows view user's profile.

What happens and when I load a category shows the category well without any problems, but when accessing a user it presents the following error.

  

ErrorException in SlugAppController.php line 24:   Trying to get property of non-object   in SlugAppController.php line 24

Line 24 corresponds to if ($result_categorias->slug == $slug)

This error happens when I am trying to access a user www.exemplo.com/cesar-sousa that slug exists in the database.

From what I understand of the error it returns as null in if ($result_categorias->slug == $slug) that is of the categories, I think as I am trying to access a user it neither should enter the first if but I do not know tto your help to solve this .

Controller

public function view (Request $request){

    $slug = $request->slug;

    $result_categorias = DB::table('colecoes')->where('activo', '=', '1')->where('slug', '=', $slug)->first();
    $result_users = DB::table('users_social')->where('activo', '=', '1')->where('slug', '=', $slug)->first();

    if ($result_categorias->slug == $slug) {
        return self::estabelecimentos($request, $slug);
    } else if ($result_users->slug == $slug) {
        return self::perfil($request, $slug);
    } else {
        return redirect('home');
    }
}
    
asked by anonymous 03.06.2017 / 19:47

2 answers

1

In your case $result_categorias->slug is returning NULL , so the error is generated.

Probably some other factor is causing Laravel not to find this $slug in the bank.

Note that when you use first , you have two possible returns: The object containing the result, or NULL .

That's what's causing such an error.

As far as I know, you could capture the value coming from $slug through the function parameter, not the $request variable.

See:

function view(Request $request, $slug) 
{
     $result = DB::table('minha_tabela')->where('slug', '=', $slug)->first();
}

I also suggest that if you use first , use isset before or is_null to check if the value is not NULL in context to avoid errors.

I also notice that your code has a small logic problem: If I make a where to know if $slug is in the database, why do I need to check back on if ? It makes no sense! If $slug does not exist, it will simply return NULL .

Change your code to:

public function view (Request $request, $slug) {


    $result_categorias = DB::table('colecoes')->where('activo', '=', '1')->where('slug', '=', $slug)->first();
    $result_users = DB::table('users_social')->where('activo', '=', '1')->where('slug', '=', $slug)->first();

    // Se não for NULL, é porque é o slug capturado acima
    if ($result_categorias !== null) {

        return self::estabelecimentos($request, $slug);
    } else if ($result_users !== null) {

        return self::perfil($request, $slug);

    } else {
        return redirect('home');
    }
}
    
03.06.2017 / 21:53
2

This should be returning null :

 $result_categorias = DB::table('colecoes')->where('activo', '=', '1')->where('slug', '=', $slug)->first();

Because if you accessed the URL as www.exemplo.com/slug , then it should be looking for something in the database like this:

... WHERE ativo=1 AND slug='slug'

If it does not exist in the database it returns null itself, what you can do is treat the page, type a 404 error page, just swap first with firstOrFail

$result_categorias = DB::table('colecoes')->where('activo', '=', '1')->where('slug', '=', $slug)->firstOrFail();
$result_users = DB::table('users_social')->where('activo', '=', '1')->where('slug', '=', $slug)->firstOrFail();
    
03.06.2017 / 20:44