2

在尝试了来自其他线程的所有现有解决方案后,我遇到了这个问题,就像这个:MySql Error: 1364 Field 'display_name' doesn't have default value if I do->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');
    }
}
4

4 回答 4

1

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

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

最后运行命令:php artisan migrate

于 2018-12-31T06:54:17.433 回答
1

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

于 2018-12-30T23:36:25.813 回答
0

设置登录列可为空。表示如果默认情况下没有为此字段传递数据,则它应该始终为空。

于 2018-12-31T07:00:00.560 回答
0

默认情况下,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'];
于 2018-12-31T06:22:54.343 回答