分类自动完成
This commit is contained in:
parent
cfe1e6d987
commit
6beb14b25d
|
|
@ -4,6 +4,7 @@ namespace Beike\Models;
|
|||
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Relations\HasOne;
|
||||
|
||||
class CategoryPath extends Model
|
||||
{
|
||||
|
|
@ -14,4 +15,14 @@ class CategoryPath extends Model
|
|||
'path_id',
|
||||
'level',
|
||||
];
|
||||
|
||||
public function category(): HasOne
|
||||
{
|
||||
return $this->hasOne(Category::class, 'id', 'category_id');
|
||||
}
|
||||
|
||||
public function pathCategory(): HasOne
|
||||
{
|
||||
return $this->hasOne(Category::class, 'id', 'path_id');
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -64,16 +64,27 @@ class CategoryRepo
|
|||
|
||||
public static function autocomplete($name)
|
||||
{
|
||||
$categories = Category::query()->with('description')
|
||||
->whereHas('description', function ($query) use ($name) {
|
||||
$query->where('name', 'like', "{$name}%");
|
||||
})->limit(10)->get();
|
||||
$categories = Category::query()->with('paths.pathCategory.description')
|
||||
->whereHas('paths', function ($query) use ($name) {
|
||||
$query->whereHas('pathCategory', function ($query) use ($name) {
|
||||
$query->whereHas('description', function ($query) use ($name) {
|
||||
$query->where('name', 'like', "{$name}%");
|
||||
});
|
||||
});
|
||||
})
|
||||
->limit(10)->get();
|
||||
$results = [];
|
||||
foreach ($categories as $category) {
|
||||
$pathName = '';
|
||||
foreach ($category->paths->sortBy('level') as $path) {
|
||||
if ($pathName) {
|
||||
$pathName .= ' > ';
|
||||
}
|
||||
$pathName .= $path->pathCategory->description->name;
|
||||
}
|
||||
$results[] = [
|
||||
'id' => $category->id,
|
||||
'name' => $category->description->name,
|
||||
'image' => $category->image,
|
||||
'name' => $pathName,
|
||||
];
|
||||
}
|
||||
return $results;
|
||||
|
|
|
|||
Loading…
Reference in New Issue