设置 Laravel Eloquent URL 基于参数的模型过滤
Setup Laravel Eloquent URL parameter based model filtering
我正在用 Laravel 构建一个 RESTful API 并且我正在尝试开发一个功能,它允许将基于 URL 参数的复杂查询发送到我的数据库模型。
我找到了一个很有前途的存储库,它似乎实现了所需的功能。 https://github.com/heroicpixels/filterable
我很难理解包装器和特征。我像文档 class Event extends Filterable {}
中提到的那样扩展了我的模型。但我不知道如何正确实施 FilterableTrait 和 FilterableWrapper.
当我将 use FilterableTrait
放入我的 class 时,我收到一条错误消息
Heroicpixels\Filterable\Filterable and Heroicpixels\Filterable\FilterableTrait define the same property ($filterable) in the composition of App\Event. This might be incompatible, to improve maintainability consider using accessor methods in traits instead. Class was composed
目前我的事件模型看起来像这样
<?php namespace App;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use Carbon\Carbon;
use Heroicpixels\Filterable\Filterable;
class Event extends Filterable {
/*
use FilterableTrait;
*/
protected $dates = ['deleted_at','starts_at','ends_at'];
protected $fillable = [
'deleted_at',
'locale',
'sports_id',
'starts_at',
'ends_at',
'locations_id',
'creators_id',
'capacity',
'attendees',
'information',
'level',
'price',
'lat',
'lng'
];
protected $hidden = ['created_at','updated_at','deleted_at'];
public function creator() {
return $this->hasOne('App\User','id','creators_id');
}
public function scopeFuture($query)
{
return $query->where('starts_at', '>=', Carbon::now());
}
public function scopeFreespots($query)
{
return $query->where('capacity', '>', 'attendees');
}
}
我错过了什么?如何实现 Trait 和 Wrapper,以便将我的 URL 参数用作查询参数?
非常感谢您的帮助!
据我所知,您只能选择一种方法(三种方法中的一种)。
您正在尝试扩展 Filterable class 并使用特征。如果你想使用特征方法,你必须扩展 Eloquent class.
最好扩展 Filterable class,因为文档中也使用了此方法。
我之前遇到过 运行 同样的问题。这就是我最终所做的。让我们的 api url 看起来像这样
$url = /query?id=999&quantity=10&action=update&api=98546
在你的路线中创建一条路线,例如我使用闭包
route::get('/query/{name}',function($name){
$modal = $name;
$params = Request::all();
});
//output products
{"id":"999","quantity":"10","action":"update","api":"98546"}
在您的控制器中,您可以使用 switch case
来调整此任何模型并从 url 参数构建您的查询。
我正在用 Laravel 构建一个 RESTful API 并且我正在尝试开发一个功能,它允许将基于 URL 参数的复杂查询发送到我的数据库模型。
我找到了一个很有前途的存储库,它似乎实现了所需的功能。 https://github.com/heroicpixels/filterable
我很难理解包装器和特征。我像文档 class Event extends Filterable {}
中提到的那样扩展了我的模型。但我不知道如何正确实施 FilterableTrait 和 FilterableWrapper.
当我将 use FilterableTrait
放入我的 class 时,我收到一条错误消息
Heroicpixels\Filterable\Filterable and Heroicpixels\Filterable\FilterableTrait define the same property ($filterable) in the composition of App\Event. This might be incompatible, to improve maintainability consider using accessor methods in traits instead. Class was composed
目前我的事件模型看起来像这样
<?php namespace App;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use Carbon\Carbon;
use Heroicpixels\Filterable\Filterable;
class Event extends Filterable {
/*
use FilterableTrait;
*/
protected $dates = ['deleted_at','starts_at','ends_at'];
protected $fillable = [
'deleted_at',
'locale',
'sports_id',
'starts_at',
'ends_at',
'locations_id',
'creators_id',
'capacity',
'attendees',
'information',
'level',
'price',
'lat',
'lng'
];
protected $hidden = ['created_at','updated_at','deleted_at'];
public function creator() {
return $this->hasOne('App\User','id','creators_id');
}
public function scopeFuture($query)
{
return $query->where('starts_at', '>=', Carbon::now());
}
public function scopeFreespots($query)
{
return $query->where('capacity', '>', 'attendees');
}
}
我错过了什么?如何实现 Trait 和 Wrapper,以便将我的 URL 参数用作查询参数? 非常感谢您的帮助!
据我所知,您只能选择一种方法(三种方法中的一种)。
您正在尝试扩展 Filterable class 并使用特征。如果你想使用特征方法,你必须扩展 Eloquent class.
最好扩展 Filterable class,因为文档中也使用了此方法。
我之前遇到过 运行 同样的问题。这就是我最终所做的。让我们的 api url 看起来像这样
$url = /query?id=999&quantity=10&action=update&api=98546
在你的路线中创建一条路线,例如我使用闭包
route::get('/query/{name}',function($name){
$modal = $name;
$params = Request::all();
});
//output products
{"id":"999","quantity":"10","action":"update","api":"98546"}
在您的控制器中,您可以使用 switch case
来调整此任何模型并从 url 参数构建您的查询。