Rlemm1991
Rlemm1991

Reputation: 505

How to pass/access data correctly to controller in Laravel 5.2

New to laravel and followed their intermediate task list through to try grasp the concepts.

I did a php artisan migrate in CLI to add 2 new columns to a task table,The first column is DATE called due & The Second column is TEXT called description,i have also added to the view so it now looks like this

<form action="/task" method="POST" class="form-horizontal">
    {{ csrf_field() }}

    <!-- Task Name -->
    <div class="form-group">
        <label for="task-name" class="col-sm-1 control-label">Task</label>

        <div class="col-sm-3">
            <input type="text" name="name" id="task-name" class="form-control" value="{{ old('task') }}">
        </div>

        <label for="task-due" class="col-sm-2 control-label">Due Date</label>

        <div class="col-sm-4">
            <input type="date" name="due_date" id="task-due" class="form-control" value="">
        </div>


    </div>

    <!-- Add Task Button -->
    <div class="form-group">
        <label for="task-due" class="col-sm-1 control-label">Description</label>

        <div class="col-sm-9">
            <input type="text" name="description" id="task-description" class="form-control" value="">
        </div>
        <div class="col-sm-2">
            <button type="submit" class="btn btn-default">
                <i class="fa fa-btn fa-plus"></i>Add Task
            </button>
        </div>
    </div>
</form>

Just some extra inputs called, due_date & description which I want to fill in a post to the database.

Controller :

public function store(Request $request)
{
    $this->validate($request, [
        'name' => 'required|max:255',
    ]);
    $request->user()->tasks()->create([
        'name' => $request->name,
        'due'=>$request->due_date,
        'description'=>$request->description,
    ]);
    return redirect('/tasks');
}

I'm not sure if I've written it right as when I post, the name of the task does into the database, but the columns due go to 0000-00-00 and column description is empty.

Am I calling the fields properly in my controller? I've tried swapping names around but I thought the $Request variable contained the form data.

All help and explanations are welcome.

Upvotes: 1

Views: 93

Answers (2)

Rlemm1991
Rlemm1991

Reputation: 505

Found the solution,

My fault for not reading the page through properly.

 protected $fillable = ['name','due','description'];

I had forgot to increase the $fillable variable set inside the model, so the create() method was only putting data inside the $fillable in

Apologies for wasting anyones time

Upvotes: 1

Marcin Nabiałek
Marcin Nabiałek

Reputation: 111829

You should make sure you have for your Task model set $fillable property with name, due and description. Now you probably have only name so other are filled by default values and not by those from input

Upvotes: 1

Related Questions