【问题标题】:Complex query and getting results by slug instead of ID in laravel复杂查询并通过 slug 而不是 laravel 中的 ID 获取结果
【发布时间】:2017-10-10 11:31:03
【问题描述】:

我有复杂的查询和关系,我不完全理解。我是 Laravel 的新手。无论如何,我正在寻找一种使用 slug 而不是 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');

目前正在加载像 http://example.com/list/1 这样的 URL,其中 1 是 ID。我想知道当前的设置是否可以做到,http://example.com/slug

我知道蛞蝓是如何工作的。我只是不明白如何在查询而不是 ID 中使用它们

【问题讨论】:

    标签: php laravel laravel-5 laravel-5.2


    【解决方案1】:

    在处理之前,您可以使用explicit Route Model Binding 通过 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);
    
        //...
    }
    

    【讨论】:

    • 这是bind 进入boot 函数吗?
    • @Peter 是的,如文档中所示,您需要在parent::boot();之后编写它
    • 在这一行下面//$category = Category::find($category_id); 有一个IF 语句正在使用这个变量$category 现在返回未定义。
    • 谢谢。哇,我很难转换这个网址真的很痛苦。
    • as long the names stay unique 是指数据库列名吗?
    【解决方案2】:

    尝试将您的索引函数参数从$category_id 更改为$category_slug

    删除这一行$Category = new Category;

    并更改此$category = Category::find($category_id);

    对此:$category = Category::where('slug', $category_slug)->first();

    *假设您在类别表中有一个唯一的slug

    【讨论】:

    • 当我更改为 slug 参数时,我在 $items = Item::where('category_id' ,$category_id)->where('published', 1)->paginate(5); 线上得到了 Undefined variable: category_id
    • 将索引函数中的所有变量$category_id更改为$category->id
    • 在您的数据库中,类别的 id 是什么?类别ID?还是身份证?
    • 在 Items 表中是 category_id 在 Category 表中是 id
    • 您在 Category 中有一个 slug 字段?
    猜你喜欢
    • 2018-07-29
    • 2016-02-29
    • 1970-01-01
    • 2014-11-17
    • 2020-09-22
    • 2019-02-01
    • 2011-11-30
    • 2022-01-25
    • 2020-09-08
    相关资源
    最近更新 更多