简体   繁体   中英

Laravel "SQLSTATE[HY000]: General error: 1364 Field 'Login' doesn't have a default value..."

I was having this issue after trying all existing solutions from other threads none works, like this one : MySql Error: 1364 Field 'display_name' doesn't have default value if i do ->nullable() all my inserts would be empty. thats my code :

The controller:

<?php

namespace App\Http\Controllers;
use App\Utilisateur;

use Illuminate\Http\Request;

class UtilisateursController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        //
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        return view('login.create');
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $this->validate($request, [
            'username' => 'required',
            //'email' => 'required',
            'password' => 'required',
            //'password_confirmation' => 'required',
            //'telephone' => 'required'
          ]);
          $inscription = new Utilisateur([
            'Login' => $request->get('username'),
            'E-mail' => 'email',
            'Password' => $request->get('password'),
            'Telephone' => 'telephone',
            'created_at' => $request->get(date("Y-m-d H:i:s")),
            'updated_at' => $request->get(date("Y-m-d H:i:s"))
          ]);
          $inscription->save();
          return redirect()->route('login.create')->with('success', 'inscription réussite');
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
    }
}

The model:

    <?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Utilisateur extends Model
{
    protected $fillable = ['username', 'email', 'password', 'telephone'];
}

The database:

<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateUtilisateursTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('utilisateurs', function (Blueprint $table) {
            $table->increments('ID-User');
            $table->string('Login', 250);
            $table->string('Password', 250);
            $table->string('Telephone', 250);
            $table->string('E-mail', 250)->unique();
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('utilisateurs');
    }
}

您的表中有一个名为 display_name 的字段名称,或者应该添加到模型中

If you are using version laravel 5, you can try with this migration code in a separate migration file like UpdateUtilisateursColumn to update the column:

Schema::table('utilisateurs', function($table)
{
    $table->string('Login', 250)->nullable()->change();
});

Finally run command: php artisan migrate

By default Laravel has many built in components in model its just the example Customize as your need i am

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;


/**
 * Class Utilisateur
 *
 * @package App
*/

class UtilisateurModel
{


    /**
     * The connection name for the model.
     *
     * @var string
     */
    protected $connection ='';

    /**
     * The attributes that aren't mass assignable.
     *
     * @var array
     */
    protected $guarded = ['id'];

     /**
     * The table associated with the model.
     *
     * @var string
     */
    protected $table = 'utilisateurs';

    /**
     * The primary key for the model.
     *
     * @var string
     */

    protected $primaryKey = 'id';

    /**
     * The "type" of the auto-incrementing ID.
     *
     * @var string
     */
    protected $keyType = 'int';

    /**
     * Indicates if the IDs are auto-incrementing.
     *
     * @var bool
     */
    public $incrementing = true;

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [];

     /**
     * The attributes that should be hidden for arrays.
     *
     * @var array
     */
    protected $hidden = [];

    /**
     * @var array
     */
    protected $dates = ['created_at','updated_at'];

    /**
     * Indicates if the model should be timestamped.
     *
     * @var bool
     */
    public $timestamps = '';

    /**
     * The storage format of the model's date columns.
     *
     * @var string
     */
    protected $dateFormat ='';




}

Now Comming to the Description

if you missed any of the fileds un the fillable array the error may occur

Jusd add All Fields of the table to fillable array

$fillable = ['ID-User','Login','Password','Telephone','E-mail'];

MANIN THINGS IS IF YOU ARE USING PASSWORD FIELDS KINDLY USE THE $hidden Property

protected $hidden = ['Password'];

set login column nullable. means if no data passed for this field by default it should be always null.

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM