Problem with routes in the Laravel

Asked

Viewed 633 times

0

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

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

This route redirects to a controller where I’m trying to validate whether when a person presses a category view categories, if a user shows the view user profile.

What happens when I click on a category shows the category well without problems, but when accessing a user shows 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 this Slug exists in the database.

From what I understand of the error it returns as null no if ($result_categorias->slug == $slug) and the categories, I think how I’m trying to access a user he shouldn’t even enter the first if but I’m not sure I can count on your help in solving 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');
    }
}
  • Put the complete error Trying to get property of non-object, line and file type, the way it is there is no way to be sure.

  • I already put the link I put at the end has all the error that presents

  • This isn’t the whole mistake, it’s the Exception’s way, the mistake is just this ErrorException in SlugAppController.php line 24:
Trying to get property of non-object
in SlugAppController.php line 24, I’ll edit the question

  • Which line is 24?

  • What’s on line 24?

  • put in question

Show 1 more comment

2 answers

2

This must be returning null:

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

Because you accessed the URL as www.exemplo.com/slug, then he must be looking for something in the bank like this:

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

If it does not exist in the database it returns null even, what you can do is treat the page, like a 404 error page, just change first for 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();

1


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

Probably some other factor is making Laravel not find that $slug at 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 a mistake.

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

Behold:

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

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

I also realize that your code has a small logic problem: If I make one where to find out if the $slug is in the bank because I need to check again on if? It makes no sense! If there is no $slug, 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');
    }
}
  • And how do I fix it then

  • The question of ifs, looking better the code my suggestion would fail anyway, pq in both cases would cause notfound, unless there were equal slugs in both tables. + 1

Browser other questions tagged

You are not signed in. Login or sign up in order to post.