Я пытаюсь создать счетчик просмотров страниц для моего приложения рецептов в Laravel. Я использовал этот пост в качестве руководства: Подсчет просмотров страниц с помощью Laravel
Однако, когда я пытаюсь получить доступ к своим рецептам, это выдает ошибку 404. Может кто-нибудь, пожалуйста, посмотрите и посмотрите, что идет не так? Спасибо!
Миграция
Schema::create('recipe_views', function (Blueprint $table) {
$table->increments('id');
$table->unsignedInteger("recipe_id");
$table->string("titleslug");
$table->string("url");
$table->string("session_id");
$table->unsignedInteger('user_id')->nullable();
$table->string("ip");
$table->string("agent");
$table->timestamps();
});
Модель
class RecipeView extends Model
{
public static function createViewLog($recipe) {
$recipeViews= new RecipeView();
$recipeViews->listing_id = $recipe->id;
$recipeViews->url = \Request::url();
$recipeViews->session_id = \Request::getSession()->getId();
$recipeViews->user_id = (\Auth::check())?\Auth::id():null;
$recipeViews->ip = \Request::getClientIp();
$recipeViews->agent = \Request::header('User-Agent');
$recipeViews->save();
}
}
Контроллер рецептов
public function show($id)
{
$recipeView = RecipeView::where('id', '=' ,$id)->firstOrFail();
RecipeView::createViewLog($recipeView);
$recipe = Recipe::find($id);
$ingredients = explode("\n", $recipe->ingredients);
$directions = explode("\n", $recipe->directions);
return view('recipes.show')->with('recipe', $recipe)->with('directions', $directions)->with('ingredients', $ingredients);
}
Маршрут
Route::resource('/recipes', 'RecipesController');
Был бы очень признателен за помощь в этом. Я немного растерялся относительно того, что я делаю неправильно. Спасибо!