Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/296.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 在laravel中通过slug而不是ID进行复杂查询并获取结果_Php_Laravel_Laravel 5_Laravel 5.2 - Fatal编程技术网

Php 在laravel中通过slug而不是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

我有复杂的查询和关系,我不完全理解。我是拉雷维尔的新手。不管怎样,我正在寻找一种方法,用鼻涕虫而不是ID来加载它

这是控制器中的功能

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列名吗?