php 数据库状态[HY000]:一般错误:1364字段'name'没有默认值laravel 5.5

67up9zun  于 2022-12-17  发布在  PHP
关注(0)|答案(8)|浏览(267)

我盯着Laravel,我有麻烦尝试做一个简单的插入,但似乎我所有的可填充字段都没有被包括在内.这是错误:

SQLSTATE[HY000]: General error: 1364 Field 'name' doesn't have a default value 
(SQL: insert into `addresses` (`updated_at`, `created_at`) 
values (2017-12-25 09:31:49, 2017-12-25 09:31:49))

如您所见,只有created_atupdated_at将要插入,我想可能我忘记了我的fillable变量,但这是我的模型:

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Addresses extends Model
{

protected $fillable = [
    'name',
    'city',
    'suburb',
    'street',
    'o_number',
    'i_number',
    'postal_code',
    'phone_s',
    'email_s',
    'google_map',
    'customer_id'
];

}

以及主计长

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Addresses;
use App\Customers;

class AddressesController extends Controller
{
public function __construct()
{
    $this->middleware('auth');
}

public function store(Request $request){

    $create = Addresses::create([
        'name' => request('name'),
        'city' => request('city'),
        'suburb' => request('suburb'),
        'street' => request('street'),
        'o_number' => request('o_number'),
        'i_number' => request('i_number'),
        'postal_code' => request('postal_code'),
        'phone_s' => request('phone_s'),
        'email_s' => request('email_s'),
        'google_map' => request('google_map'),
        'customer_id' => Customers::where('code',$request->session()->get('customer_code'))->first()->id
    ]);

    $success = $create ? $request->session()->flash('success', '¡Registro exitoso!') : $request->session()->flash('success', 'Ooops! Algo salio mal :(');

    return redirect('addresses/'.$request->session()->get('customer_code'));
 }
}

回显request()值的作品!所以我错过了现在,我有一些其他的模型和控制器工作良好,以同样的方式。请帮助!

mbzjlibv

mbzjlibv1#

显示此错误的原因是数据库中需要NAME字段。请尝试编辑迁移脚本并在此字段中设置默认值或使其可为空。
例如
$table->string('name')->nullable();

$table->string('name')->default('');
然后运行迁移刷新。

wribegjk

wribegjk2#

后藤“phpmyadmin”〉〉“变量”然后找到“sql_mode”编辑并删除“STRICT_ALL_TABLES或STRICT_TRANS_TABLES”
这对我很有效,希望对所有人都有帮助。

xzv2uavs

xzv2uavs3#

这是此问题的最新答案,但可能对其他人有所帮助。此错误可能是由于模式中$fillable数据中的错误而导致的。您可以尝试使用

protected $guarded = []

代替

protected $fillable = [
    'name',
    'city',
    'suburb',
    'street',
    'o_number',
    'i_number',
    'postal_code',
    'phone_s',
    'email_s',
    'google_map',
    'customer_id'
];

但是你必须验证你在控制器中传递的数据。

0vvn1miw

0vvn1miw4#

该错误是由MYSQL5.7的strict模式引起的,请修改connections.mysql中的config/database.php文件,在这里输入'strict' =〉false.

lfapxunr

lfapxunr5#

我用save()解出来的

$addresses = new Addresses;
$customer_id = Customers::where('code',$request->session()->get('customer_code'))->first()->id;

    $addresses->name = $request->name;
    $addresses->city = $request->city;
    $addresses->suburb = $request->suburb;
    $addresses->street = $request->street;
    $addresses->o_number = $request->onumber;
    $addresses->i_number = $request->inumber;
    $addresses->postal_code = $request->postal_code;
    $addresses->phone_s = $request->phone_s;
    $addresses->email_s = $request->email_s;
    $addresses->google_map = $request->map;
    $addresses->customer_id = $customer_id;

    $success = $addresses->save() ? $request->session()->flash('success', '¡Registro exitoso!') : $request->session()->flash('success', 'Ooops! Algo salio mal :(');

    return redirect('addresses/'.$request->session()->get('customer_code'));

工作正常

k10s72fa

k10s72fa6#

当您对字段使用nullable()方法时,该字段将默认为NULL。
例如,将以下内容添加到迁移文件中:

$table->string('name')->nullable();

更新日期:

您可以添加:

$table->string('name')->nullable()->default(null);
pcww981p

pcww981p7#

可以在表结构中勾选Null选项,如下所示-

sirbozc5

sirbozc58#

请确保request()具有密钥“name”。请将request('name ')替换为随机字符串,然后重试。

相关问题