laravel“sqlstate[hy000]:常规错误:1364字段'login'没有默认值…”

o2gm4chl  于 2021-06-17  发布在  Mysql
关注(0)|答案(4)|浏览(282)

在尝试了其他线程的所有现有解决方案后,我遇到了这个问题,没有一个有效的解决方案,例如:mysql错误:1364字段“display\u name”没有默认值,如果我这样做的话 ->nullable() 我所有的插页都是空的。这就是我的代码:
控制器:

<?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)
    {
        //
    }
}

模型:

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

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

数据库:

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

8wigbo561#

将登录列设置为空。意味着如果默认情况下没有为该字段传递数据,则该字段应始终为空。

bxfogqkk

bxfogqkk2#

如果您使用的是版本laravel 5,则可以在单独的迁移文件(如updateutilisateurscolumn)中尝试使用此迁移代码来更新列:

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

最后运行命令:php artisan migrate

thigvfpy

thigvfpy3#

表中有一个名为display\u name的字段名,或者应该将其添加到模型中

zf9nrax1

zf9nrax14#

默认情况下,laravel在模型中有许多内置组件,它只是一个示例,可以根据您的需要进行自定义

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

}

现在与描述一致
如果缺少可填充数组中的任何文件,则可能会发生错误
jusd将表的所有字段添加到可填充数组

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

如果您使用的是密码字段,请使用 $hidden 财产

protected $hidden = ['Password'];

相关问题