4
votes

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.

4
Please try to add all the code of your action & schema o your table if possible.Zakaria Acharki
full action code added. working on how to paste explain tasks; from phpmyadmin.......ComfortMoose

4 Answers

2
votes

I couldn't see the project_id attribute at all in your returned task object :

App\Task {
    name: "testtt",
    updated_at: "2016-07-29 01:08:09", 
    created_at: "2016-07-29 01:08:09", 
    id: 25
}

That mean you're not allowed the project_id attribute in your Task model in the $fillable array, so make sure that your $fillable looks like :

protected $fillable = ['name', 'project_id'];

Hope this helps.

2
votes

Ahhhhhhhhh

    class Task extends Model
    {
      protected $fillable = ['name', 'project_id'];

      public function tasks()
      {
        return $this->belongsTo(Project::Class);
      }
    }

project_id needed to be added to the $fillable in the Task Model.

0
votes

Make you sure that:

  • The value $project->id is setted and higher than 0.
  • Variable $request->project_id is setted and higher than 0.
  • The column project_id exists.

Tell me if you encounter any trouble on these 3 points and I'll try to help you, but I really think that by checking these 3 points in your code you'll find the error! Try function var_dump($var) and Browser Inspector to check if all variables are set correctly.

Good Luck!

0
votes

On that function try this:

public function store(Request $request)
{
    $this->validate($request, [
       'name' => 'required|max:255',
       'project_id' => 'required',
    ]);

    $task = new Task;
    $task->project_id = $request->project_id;
    $task->name = $request->name;
    $task->save();

    return redirect()->back();;
}

Ref: https://laravel.com/docs/5.1/eloquent#inserting-and-updating-models