DaveSanchez
DaveSanchez

Reputation: 399

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!

Upvotes: 21

Views: 120607

Answers (13)

jun drie
jun drie

Reputation: 872

This error occurs because the NAME field is required in your database. Try to edit your migration script and set a default value for this field or make it nullable.

eg.

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

OR

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

Then run:

php artisan migrate:fresh

Upvotes: 17

Andath
Andath

Reputation: 22724

The previous feedback solved your issue. I just would like to add that once can get the exact same error message if the related field is not declared mass assignable (in the $fillable array) but tries to save an entity (model instance) by assigning a value to it.

Upvotes: 0

MrX
MrX

Reputation: 23

As mentioned by @pukhraj_suthar (https://stackoverflow.com/users/987135/pukhraj-suthar) this happens when there are more columns in table than that are being added/updated, if so, error contains the name of first column in table that's extra/not being added/updated, deleting or altering sequence doesn't help, error just shifts to next column name (in case more than extra columns), until all except required are deleted. But changing SQL MODE either directly in phpmyadmin or in your database config file does the job.

Upvotes: 1

Daniel Kaniu
Daniel Kaniu

Reputation: 11

Valid the request data in the controller.

$request->validate(['name'=>'required']);

Upvotes: 0

DevExcite
DevExcite

Reputation: 429

I was having this problem because I didn't add a correct column under $fillable list.

class Chirp extends Model
{
    use HasFactory;

    protected $fillable = [
        'message', // This line should be added.
    ];
}

Upvotes: 1

KaZaiya
KaZaiya

Reputation: 87

In my case I forgot to change - this is my code on my Controller

public function store(Request $request)
{

    $message = new Message();
    $message->name = $request->input('name');
    $message->name = $request->input('message');
    $message->save();
}

I made a duplication of the 'name' that's why it happened and made this

public function store(Request $request)
{

    $message = new Message();
    $message->name = $request->input('name');
    $message->message = $request->input('message');
    $message->save();
}

This way the solution to the problem.

Upvotes: 0

Mohammed Bensouna
Mohammed Bensouna

Reputation: 107

When you use the nullable() method on a field, that field will default to NULL.

For example, add this to your migration file:

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

Update:

You can add:

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

Upvotes: 3

MD40
MD40

Reputation: 325

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.

Upvotes: 14

Umar Niazi
Umar Niazi

Reputation: 534

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.

Upvotes: 6

Pukhraj Suthar
Pukhraj Suthar

Reputation: 198

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

Upvotes: 14

LegoBoy
LegoBoy

Reputation: 140

Make sure request() has key 'name'.Replace request('name') to random string and try again.

Upvotes: 1

DaveSanchez
DaveSanchez

Reputation: 399

I solved it using 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'));

It's working properly

Upvotes: 3

Sachin Vairagi
Sachin Vairagi

Reputation: 5334

You can check Null option in table structure, like this -

enter image description here

Upvotes: 1

Related Questions