Reputation: 45
I have created a new column in my database through a migration as below :
public function up(){
Schema::table('complains', function (Blueprint $table) {
$table->integer('user_id')->after('id');
});
}
When I fill in my form to post data into the database, I get the error below :
SQLSTATE[HY000]: General error: 1364 Field 'user_id' doesn't have a default value (SQL: insert into complains
(title
, body
, name
, regnumber
, updated_at
, created_at
) values (Testing user id, Testing user id, John, cs-282-2145/2010, 2017-06-08 18:47:53, 2017-06-08 18:47:53))
How do I fix this?
Upvotes: 1
Views: 4632
Reputation: 879
I've had a similar issue with User registration today and I was getting a
SQLSTATE[HY000]: General error: 1364 Field 'password' doesn't have a default value (SQL: insert into users
I fixed it by adding password to my protected $fillable array and it worked
protected $fillable = [
'name',
'email',
'password',
];
I hope this helps.
Upvotes: 4
Reputation: 45
Here is the code for my controller :
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Complain;
use Illuminate\Support\Facades\Redirect;
use Session;
use Auth;
class ComplainController 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('home');
}
/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function store(Request $request)
{
$this->validate($request, array(
'title' => 'required|max:255',
'body' => 'required'
));
$complain = new Complain;
$complain->user_id = Auth::user()->id;
$complain->title = $request->title;
$complain->body = $request->body;
$complain->save();
Session::flash('success', 'Your complain was sent to the operator, please wait for feedback.');
return redirect::back();
}
/**
* Display the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function show($id)
{
$complain = Complain::find($id);
return view('admin')->withPost($complain);
}
/**
* 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)
{
//
}
}
This is my model :
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Complain extends Model
{
}
Upvotes: 1
Reputation: 566
Probably you aren't sending a value for user_id
Other possibility, yout can "transform" the column user_id
nullable
Upvotes: 1