尝试使用其他线程中的所有现有解决方案后,我遇到了这个问题,就像这样:MySql Error: 1364 Field 'display_name' doesn't have default value如果我做->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');
}
}
答案 0 :(得分:1)
表中有一个名为display_name的字段名称,或者应该在模型中添加该字段名称
答案 1 :(得分:1)
如果您使用的是laravel 5版本,则可以在单独的迁移文件(如UpdateUtilisateursColumn)中尝试使用此迁移代码来更新列:
Schema::table('utilisateurs', function($table)
{
$table->string('Login', 250)->nullable()->change();
});
最后运行命令:php artisan migration
答案 2 :(得分: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 ='';
}
现在进入说明
如果您错过了可填充数组中的任何文件,则可能会发生错误
将表的所有字段添加到可填充数组
$fillable = ['ID-User','Login','Password','Telephone','E-mail'];
如果您正在使用密码字段,请使用$hidden
属性
protected $hidden = ['Password'];
答案 3 :(得分:0)
设置登录栏为空。表示如果默认情况下没有为此字段传递任何数据,则应始终为null。