Php 使用Laravel 5.8创建页面视图
我正在尝试为我在Laravel中的配方应用程序创建一个页面视图计数器。我一直在使用这篇SO帖子作为指导: 然而,当我尝试访问我的食谱时,它会给我一个404错误。有人能看看出了什么问题吗?谢谢大家! 迁移Php 使用Laravel 5.8创建页面视图,php,laravel,eloquent,Php,Laravel,Eloquent,我正在尝试为我在Laravel中的配方应用程序创建一个页面视图计数器。我一直在使用这篇SO帖子作为指导: 然而,当我尝试访问我的食谱时,它会给我一个404错误。有人能看看出了什么问题吗?谢谢大家! 迁移 Schema::create('recipe_views', function (Blueprint $table) { $table->increments('id'); $table->unsignedInteger("recip
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');
我真的很感谢你在这方面的帮助。我不知道自己做错了什么。谢谢 您需要在路由中指定函数名+变量。在你的例子中show()
你能看看你的/index.php/recipes是否有效吗 如果是,则需要通过以下步骤进行修复
<Directory /var/www/>
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>
选项索引跟随符号链接
允许超越所有
要求所有授权
在
配方控制器中
-可以更改firstOrFail();首先();并通过dd($recipeView)记录结果;(你能给我看一下路线文件吗?)看起来像是一条路线error@KoenHollander我的食谱路线是一个资源路线,我把它添加到了问题中。路由列表在显示路由下有“recipes/{recipe}”,其中{recipe}是配方id。dd($recipeView)返回null您能看到插入是否发生在DB中吗?你能试着用->all()代替firstOrFail()吗?@DanishHakimKhan它告诉我->all()是对一个未定义函数的调用。你知道为什么吗?没有插入数据库号。这是一条资源路由。这不需要我来定义。拉威尔在兜帽下这样做,这肯定是路线的问题。检查两次。
<Directory /var/www/>
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>