laravel - Laravel Nova 使用 ->fillUsing() 忽略字段如果为空
问题描述
我有 3 个字段“名称”、“电子邮件”和“网址”。这 3 个字段在我的数据库的 1 列中转换为 json。
现在,如果您只填写我只想保存{url: "value"}
在数据库中的 url。如果您填写电子邮件和姓名我只想保存{name: "john", email: "john@gmail.com"}
在数据库中。
这就是我尝试这样做的方式:
Text::make('To Name', 'toName')
->sortable()
->fillUsing(
function ($request, $model) {
return $request->toName;
}
),
Text::make('To Email', 'toEmail')
->sortable()
->fillUsing(
function ($request, $model) {
return $request->toEmail;
}
),
Text::make('To Url', 'toUrl')
->sortable()
->fillUsing(
function ($request, $model) {
return $request->toUrl;
}
),
但我不断收到此错误:
一般错误:1364 字段 'to' 没有默认值
我返回的东西有问题吗?
解决方案
检查此代码,使用 Laravel mutator 并在您的 fillUsing 中进行一些更改。注意toName、toEmail、toUrl是虚拟属性,而to_json是model中这些列的json值!
// database\migrations\2019_08_28_045853_create_infos_table.php
...
Schema::create('infos', function (Blueprint $table) {
$table->bigIncrements('id');
$table->timestamps();
$table->string('label',100);
$table->json('to_json');
});
// app\Nova\Info.php
...
public function fields(Request $request)
{
return [
ID::make()->sortable(),
Text::make('Label'),
Text::make('To Name', 'toName')
->sortable()
->fillUsing(
function ($request, $model) {
if(!empty($request->toName)){
if(!empty($model['to_json'])){
$json = json_decode($model['to_json'],true);
}else{
$json = [];
}
$json['name'] = $request->toName;
$model['to_json'] = json_encode($json);
}
}
),
Text::make('To Email', 'toEmail')
->sortable()
->fillUsing(
function ($request, $model) {
if(!empty($request->toEmail)){
if(!empty($model['to_json'])){
$json = json_decode($model['to_json'],true);
}else{
$json = [];
}
$json['email'] = $request->toEmail;
$model['to_json'] = json_encode($json);
}
}
),
Text::make('To Url', 'toUrl')
->sortable()
->fillUsing(
function ($request, $model) {
if(!empty($request->toUrl)){
if(!empty($model['to_json'])){
$json = json_decode($model['to_json'],true);
}else{
$json = [];
}
$json['url'] = $request->toUrl;
$model['to_json'] = json_encode($json);
}
}
),
];
}
// app\Info.php
...
class Info extends Model
{
public function gettoNameAttribute(){
if(empty($this->attributes['to_json'])){
return "";
}
$json = $this->attributes['to_json'];
$result = json_decode($json,true);
if(!empty($result['name'])){
return $result['name'];
}else{
return "";
}
}
public function gettoEmailAttribute(){
if(empty($this->attributes['to_json'])){
return "";
}
$json = $this->attributes['to_json'];
$result = json_decode($json,true);
if(!empty($result['email'])){
return $result['email'];
}else{
return "";
}
}
public function gettoUrlAttribute(){
if(empty($this->attributes['to_json'])){
return "";
}
$json = $this->attributes['to_json'];
$result = json_decode($json,true);
if(!empty($result['url'])){
return $result['url'];
}else{
return "";
}
}
}
推荐阅读
- android - 将 Curl Post 与请求队列 Kotlin Android 相结合
- sql-server - 使用以 Kafka 为目标的 AWS DMS 获取数据和控制记录
- c# - 仅允许向前日期并阻止向后日期
- java - 使用 CommonCrypto 框架在 Objective-C 中以编程方式创建 x509 证书
- javascript - 在 ul 中的 Nth li 之后添加一个 Scroll
- stata - 多项 Logit 固定效应:Stata 和 R
- java - 如何从 API 显示一定数量的图像 - Kotlin - 改造
- java - java检查char是否是switch case语句中的数字
- elasticsearch - elasticsearch sum 聚合值
- java - 发送 Outlook 邀请时出现错误“不支持的 message.ics”