I have a form:
<label for="task-name" class="col-sm-3 control-label">Task</label>
<div class="col-sm-6">
<input type="text" name="name" id="task-name" class="form-control">
</div>
</div>
<input name="project_id" id="task-project_id" type="hidden" value="{{ $project->id }}">
And a method to store the two form fields:
public function store(Request $request)
{
$this->validate($request, [
'name' => 'required|max:255',
'project_id' => 'required',
]);
Task::create([
'project_id' => $request->project_id,
'name' => $request->name,
]);
return redirect()->back();;
}
But, only the 'name' is saving to the db table. The Task table has both column names & while I can echo the correct $request->project_id
back (to test that it passes) the saved project_id
is always '0'.
Here's the table:
mysql> explain tasks;
+------------+------------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +------------+------------------+------+-----+---------+----------------+ | id | int(10) unsigned | NO | PRI | NULL | auto_increment | | project_id | int(11) | NO | MUL | NULL | | | name | varchar(255) | NO | | NULL | | | created_at | timestamp | YES | | NULL | | | updated_at | timestamp | YES | | NULL | | +------------+------------------+------+-----+---------+----------------+
Checked the create manually also :
App\Task::create(['project_id' => 20,'name' =>'testtt']);
Output :
App\Task {
name: "testtt",
updated_at: "2016-07-29 01:08:09",
created_at: "2016-07-29 01:08:09",
id: 25
}
Thanks.