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');
}
}