General error: 1364 Field 'user_id' doesn't have a default value
So, after After reviewing the code again, I found the error.
I am wondering how no one notice that!
In the above code I wrote
Post::create(request([ // <= the error is Here!!!
'body' => request('body'),
'title' => request('title'),
'user_id' => auth()->id()
]));
actually, there is no need for the request function warping the body of the create function.
// this is right
Post::create([
'body' => request('body'),
'title' => request('title'),
'user_id' => auth()->id()
]);
Why this ERROR: General error: 1364 Field 'user_id' doesn't have a default value
the field user_id in your table is not nullable, and you tried to save post without user_id, or may be user_id is not passed by request.
Try this:
$this->validate($request, [
'question' => 'required',
'description' => 'required',
'tags' => 'required',
]);
$user = auth()->user();
$data = $request->all();
$data['user_id']=$user->id;
$user->postqs()->create($data);
$tags = explode(",", $request->tags);
$postqs = Postqs::create($data);
//$postqs->tag($tags);
$postqs->tag()->save($tags);
return redirect()->route('postqs.index')
->with('success','Your question created successfully');
General error: 1364 Field 'user_id' doesn't have a default value Laravel
you should add 'user_id' to the $fillable attribute in your Post model to support mass assignment
class Postextends Model
{
protected $fillable=['user_id']; // not only user_id but also all the fill able columns
}
Laravel - SQLSTATE[HY000]: General error: 1364
Error is clear.
Field 'user_id
' doesn't have a default value.
In your ptcs
table you have user_id
and it is not have a default value and you're not setting a value for user_id
in your php side.
$ptc = Ptc::create([
'title' => $request->title,
'details' => $request->details,
'duration' => $request->duration,
'rewards' => $request->rewards,
'ad_link' => $request->ad_link,
'hit' => $request->hit,
'type' =>1,
'count' =>0,
'membership_id' => $request->membership_id,
'user_id' => //Give here an id for testing like 1235
]);
if your $request
object has user_id
set it like others.
'user_id' => $request->name_of_your_user_id_parameter
According to your comment user_id
is not fillable for mass assignment.
So on your Ptc
model class you need something like;
class Ptc extends Eloquent {
protected $fillable = array('user_id', 'title', 'description', ... other fields);
}
https://laravel.com/docs/4.2/eloquent#mass-assignment
or on your table schema set a default value or nullable
SQLSTATE[HY000]: General error: 1364 Field 'user_id' doesn't have a default value on laravel 8
As error says you are not passing user_id
.
$result=collect($request->question)->map(function ($item)use($request) {
$item['user_id'] = $request->user_id;
$item['standart_id'] = $request->standart_id;
$item['files_link'] = $request->files_link;
$item['description'] = $request->description;
return $item;
});
Response::insert($result->toArray());
SQLSTATE[HY000]: General error: 1364 Field 'user_id' doesn't have a default value laravel 6.9.0
Change your model like
class Players extends Model
{
protected $guarded = [];
protected $table = 'players';
protected $fillable = [
'user_id','player','team_id','country_player','birth_day','age','length','weight','position','player_img','slug_player'
];
}
Related Topics
Codeigniter - Show Information in Modal Window
How to Get Multiple Data from Two Different Table from One Database in PHP
How to Redirect to the Same Page in PHP
Display Data from Database to Dropdown Codeigniter
How to Remove Duplicate Rows in Laravel
Why Does This Pdo Statement Silently Fail
Should We Ever Check For MySQLi_Connect() Errors Manually
Laravel, Sync() - How to Sync an Array and Also Pass Additional Pivot Fields
How to Get the MAC and the Ip Address of a Connected Client in PHP
Regex Optionally Match a Pattern Multiple Times
Add Space After Every 4Th Character
How to Not Make Phpmailer Send an Email With Multiple 'To' Addresses