markdown 与Laravel Nova合作的注意事项。

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了markdown 与Laravel Nova合作的注意事项。相关的知识,希望对你有一定的参考价值。

# [Laravel Nova](https://nova.laravel.com/)

## Docs 
- https://nova.laravel.com/docs/
  - [Resource Field Types](https://nova.laravel.com/docs/1.0/resources/fields.html#field-types)
  - [Validation Rules](https://laravel.com/docs/5.7/validation#available-validation-rules)

## Issues
- https://github.com/laravel/nova-issues/

## Packages
- https://novapackages.com/

## Themes
- https://novathemes.beyondco.de/

## Tutorials
- https://laracasts.com/series/laravel-nova-mastery/

## [Installation](https://nova.laravel.com/docs/1.0/installation.html#installing-nova)
Require the Nova package in your `composer.json` and run the `nova:install` and `migrate` artisan commands:
```sh
php artisan nova:install
php artisan migrate
```

### Login
Default path: `http://localhost/nova`

### Create a new user
```sh
php artisan nova:user
```

### Localization
Update the language setting in your `config/app.php` config file:
```php
'locale' => 'de'
```
Copy translations from [`franzdumfart/laravel-nova-localizations`](https://github.com/franzdumfart/laravel-nova-localizations) to your repo:
```sh
php -r "copy('https://raw.githubusercontent.com/franzdumfart/laravel-nova-localizations/master/lang/de.json', './resources/lang/vendor/nova/de.json') || exit (1);"
```

## [Resources](https://nova.laravel.com/docs/1.0/resources/#defining-resources)
Generate a new Nova resource from an existing model:
```sh
php artisan nova:resource Post
```

## [Resource Authorization](https://nova.laravel.com/docs/1.0/resources/authorization.html)
Generate a new Laravel policy for the Post model:
```sh
php artisan make:policy PostPolicy -m Post
```

After that, map the new policy to the model in `app/Providers/AuthServiceProvider.php`:
```php
protected $policies = [
    'App\Post' => 'App\Policies\PostPolicy',
];
```

### Restrict the index query to the current User
Modify the Nova resource index query in `app/Nova/Post.php` so that Users can only see their own Posts:
```php
public static function indexQuery(NovaRequest $request, $query)
{
    return $query->where('user_id', $request->user()->id);
}
```

### Search

#### [Global Search](https://nova.laravel.com/docs/1.0/search/global-search.html)
Remove the `public static $title = 'title';` in `app/Nova/Post.php` to define a custom title property on the resource class to modify the global search results:
```php
public function title()
{
    return $this->title . ' - ' . $this->category;
}
```
```php
public function subtitle()
{
    return "Author: {$this->user->name}";
}
```

To exclude the resource from the global search add the `$globallySearchabl` property on the resource `app/Nova/Post.php`:
```php
public static $globallySearchable = false;
```

#### Resource Search
An empty `$search` array in `app/Nova/Post.php` will remove the searchbar of the resource:
```php
public static $search = [];
```

### [Filters](https://nova.laravel.com/docs/1.0/filters/defining-filters.html)
Generate a new filter using the available artisan command:
```sh
php artisan nova:filter PostPubliished
```

Attach the filter to a resource via the `filters()` method.
```php
public function filters(Request $request)
{
    return [
        new PostPubliished()
    ];
}
```

### [Lenses](https://nova.laravel.com/docs/1.0/lenses/defining-lenses.html)
Lenses are custom views in the admin panel. 
Generate a new lens:
```sh
php artisan nova:lens MostViews
```

Attach the lens to a resource via the `lenses()` method.

### [Actions](https://nova.laravel.com/docs/1.0/actions/defining-actions.html)
Actions are custom tasks that can be performed on eloquent models.
Generate a new action:
```sh
php artisan nova:action PublishPost
```

Attach the action to a resource via the `actions()` method.

#### [Action Fields](https://nova.laravel.com/docs/1.0/actions/defining-actions.html#action-fields)
Populate the `fields()` method to request additional information (in a prompt) before running an action. The data is available in the `handle()` method:
```php
Action::message($fields->message);
```

#### [Action Authorization](https://nova.laravel.com/docs/1.0/actions/registering-actions.html#authorization)
Customize to whom and where actions are available with the `canSee()` with `canRun()` resource methods:
```php
public function actions(Request $request)
{
    return [
        (new PublishPost())->canSee(function ($request) {
            // Restrict to user with ID 1.
            return $request->user()->id === 1;
        })->canRun(function ($request, $post) {
            // Restrict to post with ID 1.
            return $post->id === 1;
        })
    ];
}
```

#### [Action Log](https://nova.laravel.com/docs/1.0/actions/defining-actions.html#action-log)
Add the `Actionable` trait to the `Post` model to enable a detailled log of all performed actions:
```php
class Post extends Model
{
    use Actionable;
}
```

### [Metrics](https://nova.laravel.com/docs/1.0/metrics/defining-metrics.html)

#### [Value Metrics](https://nova.laravel.com/docs/1.0/metrics/defining-metrics.html#value-metrics)
```sh
php artisan nova:value PostCount
```

#### [Trend Metrics](https://nova.laravel.com/docs/1.0/metrics/defining-metrics.html#trend-metrics)
```sh
php artisan nova:trend PostsPerDay
```

#### [Partition Metrics](https://nova.laravel.com/docs/1.0/metrics/defining-metrics.html#partition-metrics)
```sh
php artisan nova:partition PostsPerCategory
```

### [Tools](https://nova.laravel.com/docs/1.0/customization/tools.html)
```sh
php artisan nova:tool acme/nova-tool
```
This command prompts to:
> 1. Would you like to install the tool's NPM dependencies? (yes/no)
>
> 2. Would you like to compile the tool's assets? (yes/no)
>
> 3. Would you like to update your Composer packages? (yes/no) 

### [Cards](https://nova.laravel.com/docs/1.0/customization/cards.html)
```sh
php artisan nova:card acme/nova-card
```
This command prompts to:
> Would you like to install the card's NPM dependencies? (yes/no)
>
> Would you like to compile the card's assets? (yes/no)
>
> Would you like to update your Composer packages? (yes/no)

### [Fields](https://nova.laravel.com/docs/1.0/customization/fields.html)
```sh
php artisan nova:field acme/nova-field
```
This command prompts to:
> Would you like to install the field's NPM dependencies? (yes/no)
>
> Would you like to compile the field's assets? (yes/no)
>
> Would you like to update your Composer packages? (yes/no)

### Themes
```sh
php artisan nova:theme acme/nova-theme
```
This command prompts to:
> Would you like to update your Composer packages? (yes/no)

以上是关于markdown 与Laravel Nova合作的注意事项。的主要内容,如果未能解决你的问题,请参考以下文章

Laravel Nova 无法与 Laravel Spark 一起使用:位置 1213 的 JSON 中出现意外的令牌 P

将 Laravel Nova 移至子域

Laravel Nova 中的自定义 Vue 组件

有没有办法在 Laravel 中为 Nova 资源显式定义策略?

Laravel Nova Actions BelongsTo 字段不起作用

php 在Laravel Nova资源中覆盖标签