我想在我的文章路线中同时使用ID和slug.而不是/articles/ID我想要的/articles/ID/slug.
我实际上并不需要slug变量; 它只是为了使URL更具可读性和SEO,所以我将ID用作检索文章的标识符.
如果/articles/ID输入了URL ,我想重定向到/articles/ID/slug.必须有一个例外,/articles/ID/edit因为这会打开用于编辑文章的表单.
我用谷歌搜索并查看了这个网站,但我只找到了用slug替换ID的例子,不包括两者.
我怎样才能做到这一点?我可以使用URL该类获取文章的完整URL(/articles/ID/slug)吗?
当前路由配置:
Route::resource('articles', 'ArticlesController');
Run Code Online (Sandbox Code Playgroud) 在Laravel 4; 我有模型Project和Part,他们有一个透视表许多一对多的关系project_part.数据透视表有一列count,其中包含项目中使用的部件ID的编号,例如:
id project_id part_id count
24 6 230 3
Run Code Online (Sandbox Code Playgroud)
这里的project_id6,是使用3件part_id230.
对于同一项目,可以多次列出一个部分,例如:
id project_id part_id count
24 6 230 3
92 6 230 1
Run Code Online (Sandbox Code Playgroud)
当我显示项目的零件清单时,我不想显示part_id两次,所以我将结果分组.
我的项目模型有:
public function parts()
{
return $this->belongsToMany('Part', 'project_part', 'project_id', 'part_id')
->withPivot('count')
->withTimestamps()
->groupBy('pivot_part_id')
}
Run Code Online (Sandbox Code Playgroud)
但当然我的count价值不正确,这就是我的问题:如何获得项目所有分组部分的总和?
这意味着我的零件清单project_id应该如下所示:
part_id count
230 4
Run Code Online (Sandbox Code Playgroud)
我真的很想把它放在Projects- Parts关系中,所以我可以急于加载它.
如果没有得到N + 1问题,我无法解决如何做到这一点,任何见解都表示赞赏.
更新:作为临时解决方案,我创建了一个presenter方法来获取项目中的总计数.但这给了我N + 1的问题.
public …Run Code Online (Sandbox Code Playgroud) 使用Eloquent模型可以轻松添加范围:
public function scopeMyScope($query)
{
// Do stuff to that $query
}
Run Code Online (Sandbox Code Playgroud)
但是如何添加范围DB::table呢?
我使用此查询来获取页面视图:
$views = DB::table('page_views')
->where('id', $this->id)
->where('agent', 'NOT LIKE', '%bot%')
->count(DB::raw('distinct session, DATE(created_at)'));
Run Code Online (Sandbox Code Playgroud)
我还用其他查询显示最受欢迎的页面等,但具有相同的 where条件.所以我想只定义where一次条件并在所有其他页面查看DB::table查询中重用它们.
我的 Laravel 会话存在身份验证问题。(之前是在同一个系统中运行的)
\n\n这是我的用户控制器
\n\npublic function postLogin()\n{\n $username = Input::get("username");\n $password = Input::get("password");\n $credentials = array\n (\n "username"=> $username,\n "password" => $password,\n );\n if (Auth::attempt($credentials)) \n {\n echo Auth::user()->username;\n exit();\n\n }\n else\n {\n return Redirect::to("user/login")->with("error_msg","Giri\xc5\x9f Ba\xc5\x9far\xc4\xb1s\xc4\xb1z! <br>L\xc3\xbctfen girdi\xc4\x9finiz bilgileri kontrol edip yeniden deneyin.");\n }\n}\nRun Code Online (Sandbox Code Playgroud)\n\n它返回用户名..但是当我重定向页面时会话丢失。
\n\npublic function postLogin()\n{\n $username = Input::get("username");\n $password = Input::get("password");\n $credentials = array\n (\n "username"=> $username,\n "password" => $password,\n );\n if (Auth::attempt($credentials)) \n {\n return Redirect::to(\'check_login\');\n\n }\n else\n {\n return Redirect::to("user/login")->with("error_msg","Giri\xc5\x9f Ba\xc5\x9far\xc4\xb1s\xc4\xb1z! …Run Code Online (Sandbox Code Playgroud) 在我的Image模型中,我与我的Article模型有两个关系,一个是多对多,一个是一对多:
public function articlesAlbums()
{
return $this->belongsToMany('Article', 'article_image', 'image_id', 'article_id')->publishedFilter();
}
public function articleThumb()
{
return $this->hasMany('Article')->publishedFilter();
}
Run Code Online (Sandbox Code Playgroud)
我合并了这些结果,以获得所有使用的图像Article:
public function getArticlesAllAttribute()
{
return $this->articlesAlbums->merge($this->articleThumb);
}
Run Code Online (Sandbox Code Playgroud)
在我的Article模型中,我与我的Image模型有两种关系:
public function images()
{
return $this->belongsToMany('Image', 'article_image', 'article_id', 'image_id');
}
public function thumbnail()
{
return $this->belongsTo('Image', 'image_id');
}
Run Code Online (Sandbox Code Playgroud)
我想合并这些,就像我在Image模型中一样:
public function getImagesAllAttribute()
{
return $this->images->merge($this->thumbnail);
}
Run Code Online (Sandbox Code Playgroud)
但这不起作用,似乎是因为我的缩略图关系belongsTo不是hasMany.所以也许它不是一个集合.当我尝试时,我得到一个例外:
Call to a member function getKey() on a non-object
Run Code Online (Sandbox Code Playgroud)
我尝试将其转换为集合: …
我通过将每个页面视图与会话 ID 一起存储在一个表中来跟踪文章点击,然后我使用会话 IDcount(distinct session)在用户会话生命周期中清除 ( ) 多次点击。
但是 Laravel 会话 ID 有多独特?我是否还应该考虑其他因素,例如避免错误分组的时间?
更新:我调整了我的 SQL 以考虑日期:
select count(distinct session, DATE(created_at)) as aggregate from `article_views` where `article_id` = ? and `agent` NOT LIKE '%bot%'
Run Code Online (Sandbox Code Playgroud)