laravel nova 的一些资源和几个常见问题
2022-11-25 tech laravel php 8 mins 1 图 3051 字
最近使用 nova 比较多,整理了一些常用的资源链接和几个我遇到的问题:
-
NOVAPACKAGES: nova 包搜索站
-
Nova ChartJS : A Laravel Nova Dashboard with Chart JS.
-
public static function indexQuery(NovaRequest $request, $query) { return $query->where('frontpage', true); }
-
Access models within fields function in Nova Actions - github
# app/Nova/{your-resource}.php public function actions(Request $request) { return [ new BookEventAction, (new AddAnonymousUserAction($this->model()))->showOnTableRow() // here $this->model() returns resource model. ]; } # app/Nova/Actions/{your-action}.php class AddAnonymousUser extends Action { ... public $model; function __construct($model = null) { $this->model = $model; if(!is_null($resourceId = request('resourceId'))){ $this->model = Event::find($resourceId); //Event is my Resource class, you can make this one dynamic too. nova/route/api.php get Actions route. ;) } } ... public function fields() { return [ Select::make('Customer') ->options(\App\Models\Customer::all()->pluck('company', 'id')) ->searchable(), ->withMeta(['value' => optional($this->model)->customer_id], ]; } }
-
public function comments() { return $this->hasMany('Comment')->orderBy('column'); }
-
public function posts() { return $this->hasMany('App\Models\Post', 'cat_id')->take(4); }
-
$data = \App\User::with(['articles' => function($query) { // user_id is required here* $query->select(['id', 'title', 'user_id']); }])->get(); public function articleTitles() { // user_id is required here* return $this->hasMany('App\Article')->select(['id', 'title', 'user_id']); } $data = \App\User::with('articleTitles')->get();
-
protected static function boot() { parent::boot(); static::updating(function ($address) { $address->full_phone_number = $address->country_code . $address->number; }); }
移动端自适应包
这个插件可以让 3.0 及 更早的 nova 适应手机上的显示。https://github.com/gregoriohc/laravel-nova-theme-responsive
当然自动4.0官方支持响应式后就不再更新了。我目前还没有升级的需要,就一直用着3.x了。
composer require gregoriohc/laravel-nova-theme-responsive
升级
可把我难住了:
在compose.json
里添加:
"repositories": [
{
"type": "path",
"url": "./src-nova"
}
],
解压官方压缩包到目录 ./src-nova
composer update "laravel/nova @3.32" --ignore-platform-reqs