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');
}
}
By default Laravel has many built in components in model its just the example Customize as your need i am
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
MANIN THINGS IS IF YOU ARE USING PASSWORD FIELDS KINDLY USE THE
$hidden
Property