I am defining some resources in Nova and I ran into a bit of a problem. I have a Team
resource has the fields name
and display_name
. I only want display_name
visible on the dashboard to work with but the way I have the Team
model, name
gets populated by turning display_name
into a slug. Is there a way to populate name
based on the contents of display_name
with Nova when creating the resource?
Text::make('Name')->displayUsing(function(){
return Str::slug($this->display_name, '_');
})->hideFromIndex()
->hideFromDetail()
->hideWhenCreating()
->hideWhenUpdating(),
Text::make('Display Name')
->rules('required', 'max:254')
->creationRules('unique:teams,name')
->updateRules('unique:teams,name,{{resourceId}}'),
Textarea::make('Description')
->rules('required'),
This is what i've got now and it does give me the right output for name
with resources that have already been created but when I try to create a new team I get this error:
SQLSTATE[HY000]: General error: 1364 Field 'name' doesn't have a default value (SQL: insert into 'teams' ('display_name', 'description', 'updated_at', 'created_at')
You can solve this problem by use Laravel Mutator. Read it at here:
https://laravel.com/docs/5.8/eloquent-mutators
Reference my code:
// app\Team.php
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Str;
class Team extends Model
{
public function setDisplayNameAttribute($value)
{
$this->attributes['display_name'] = $value;
$this->attributes['name'] = Str::slug($this->display_name, '_');
}
}
// app\Nova\Team.php
public function fields(Request $request)
{
return [
ID::make()->sortable(),
Text::make('Display Name','display_name'),
Text::make('Name')->onlyOnIndex(),
Text::make('Description'),
];
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.