Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQLSTATE[HY000]: General error: 1364 Field 'name' doesn't have a default value laravel 5.5

I'm staring with Laravel and I'm having troubles trying to make a simple insert, but It seems that all of my fillable fields are not being included. This is the error:

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))

As you can see, only created_at and updated_at are about to be inserted, I thought that maybe I forgot my fillable vars, but this is my Model:

<?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'
];

}

And the Controller

<?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'));
 }
}

Echo the request() values works! So I'm missing right now, I have some other Models and Controller working good in the same way. Please Help!

like image 882
DaveSanchez Avatar asked Dec 26 '17 05:12

DaveSanchez


4 Answers

This error show because you the NAME field is required on your database. Try to edit your migration script and put default value on this field or make it nullable.

eg.

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

OR

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

Then run a migration refresh.

like image 161
jun drie Avatar answered Nov 15 '22 23:11

jun drie


Goto "phpmyadmin" >> "Variables" then find "sql_mode" edit and remove "STRICT_ALL_TABLES or STRICT_TRANS_TABLES"

It is working for me. Hope it will help for All.

enter image description here

like image 34
Pukhraj Suthar Avatar answered Nov 15 '22 21:11

Pukhraj Suthar


This is late answer for this question, but it might help for others. This error can be occurred due to error in $fillable data in modal. You can try using

protected $guarded = []

instead of

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

But You have to validate the data that you passed within the controller.

like image 33
MD40 Avatar answered Nov 15 '22 22:11

MD40


The error occurs due to the strict mode of MYSQL5.7. Please change your config/database.php in the connections.mysql section by putting 'strict' => false.

like image 20
Umar Niazi Avatar answered Nov 15 '22 22:11

Umar Niazi