Php 在laravel中通过slug而不是ID进行复杂查询并获取结果
我有复杂的查询和关系,我不完全理解。我是拉雷维尔的新手。不管怎样,我正在寻找一种方法,用鼻涕虫而不是ID来加载它 这是控制器中的功能Php 在laravel中通过slug而不是ID进行复杂查询并获取结果,php,laravel,laravel-5,laravel-5.2,Php,Laravel,Laravel 5,Laravel 5.2,我有复杂的查询和关系,我不完全理解。我是拉雷维尔的新手。不管怎样,我正在寻找一种方法,用鼻涕虫而不是ID来加载它 这是控制器中的功能 public function index( $category_id) { $Category = new Category; $allCategories = $Category->getCategories(); $category = Category::find($category_id); if($categor
public function index( $category_id)
{
$Category = new Category;
$allCategories = $Category->getCategories();
$category = Category::find($category_id);
if($category->parent_id == 0) {
$ids = Category::select('id')->where('parent_id', $category_id)->where('parent_id','!=',0)->get();
$array = array();
foreach ($ids as $id) {
$array[] = (int) $id->id;
}
$items = Item::whereIn('category_id',$array)->where('published', 1)->paginate(5);
} else {
$items = Item::where('category_id' ,$category_id)->where('published', 1)->paginate(5);
}
return view('list', compact('allCategories','items'));
}
这些是模型中的关系
public function item()
{
return $this->hasMany('App\Item','category_id');
}
public function children()
{
return $this->hasMany('App\Category', 'parent_id');
}
public function getCategories()
{
$categoires = Category::where('parent_id',0)->get();
$categoires = $this->addRelation($categoires);
return $categoires;
}
public function selectChild( $id )
{
$categoires = Category::where('parent_id',$id)->where('published', 1)->paginate(40);
$categoires = $this->addRelation($categoires);
return $categoires;
}
public function addRelation( $categoires )
{
$categoires->map(function( $item, $key)
{
$sub = $this->selectChild($item->id);
$item->itemCount = $this->getItemCount($item->id , $item->parent_id );
return $item = array_add($item, 'subCategory', $sub);
});
return $categoires;
}
public function getItemCount( $category_id )
{
return Item::where('category_id', $category_id)->count();
}
这就是我的路线
Route::get('list/{category}', 'ListController@index')->name('list');
当前正在加载URL,如其中1是ID。我想知道是否可以使用当前设置使其类似
我知道鼻涕虫是如何工作的。我只是不明白如何在查询中使用它们而不是ID尝试将索引函数参数从
$category\u ID
更改为$category\u slug
删除此行$Category=新类别代码>
并更改此$category=category::find($category\u id)代码>
对此:$category=category::where('slug',$category_slug)->first()代码>
*假设您在类别表中有一个唯一的slug
,,您可以使用它在处理类别之前按slug抓取类别
在您的RouteServiceProvider
中,您需要绑定模型:
Route::bind('category', function ($value) {
//Change slug to your column name
return App\Category::where('slug', $value)->firstOrFail();
});
然后,您可以键入类别提示
例如,在索引方法中:
public function index(Category $category)
{
$Category = new Category;
$allCategories = $Category->getCategories();
//This line is obsolete now:
//$category = Category::find($category_id);
//...
}
我在$items=Item::where('category\u id',$category\u id)->where('published',1)->分页(5)行上获得未定义变量:category\u id
当我更改为slug参数时,将索引函数中的所有变量$category\u id
更改为数据库中的$category->id
类别的id是什么??分类号?或者id?在Items表中是category\u id
在category表中是id
您在category中有一个slug字段?这是bind
进入boot
函数吗?@Peter是的,如文档所示,您需要在parent::boot()之后编写它代码>在这行下面/$category=category::find($category\u id)
有一个IF语句正在使用这个变量$category
,现在返回undefined。谢谢。哇,我知道转换这个URL会很痛苦。只要名称保持唯一,你是说DB列名吗?