2

I have a simple task list project under Laravel.

When I click a checkbox it does not show a checked condition. (The second item is in the true condition in the database and thus shows checked. I cannot uncheck this item) I have searched for an answer to why on the net but cannot find a solution or reason.

Code:

home.blade.php (in views folder) -

@extends('layouts.main')

@section('content')

<h1>Tasks</h1>

<ul>
@foreach ($items as $item)
    <li>
        {{ Form::open() }}
            <input type="checkbox" onClick="this.form.submit()" {{     $item->done ? 'checked' : '' }}>

            <input type="hidden" name="id" value="{{ $item->id }}">

            {{ $item->name }}
        {{ Form::close() }}
    </li>   
@endforeach
</ul>

@stop   

HomeController.php (inControllers folder) -

<?php

class HomeController extends BaseController {

public function getIndex() {
    $items = Auth::user()->items;

    return View::make('home', array(
        'items' => $items
    ));
}

public function postIndex() {
    $id = Input::get('id');
    $user_id = Auth::user()->id;

    $item = Item::findOrFail($id);

    if($item->owner_id == $userId) {
        $item->mark();
    }

    return Redirect::route('home');
}
}

Item.php (in models folder) -

<?php

class Item extends Eloquent {
public function mark() {
    $this->$done = $this->done ? false : true;

    $this->save();
}
}

routes.php -

<?php

Route::get('/', array('as' => 'home', 'uses' => 'HomeController@getIndex'))->before('auth');
Route::post('/', array('uses' => 'HomeController@postIndex'))->before('csrf');

Route::get('/login', array('as' => 'login', 'uses' => 'AuthController@getLogin'))->before('guest');  
Route::post('login', array('uses' => 'AuthController@postLogin'))->before('csrf');
2
  • im still not sure what your problem is ... can you explain it in a different way Commented Apr 28, 2014 at 2:43
  • I don't know a ton about laravel, but your checkbox doesn't have a name or id attribute so I don't understand how you are accessing the value of that checkbox from your controller. It looks like you are getting the value of that hidden field below it though. Commented Apr 28, 2014 at 2:54

2 Answers 2

2

in your code, you never update the model's done value. i assume, you want to change it with the post method. so you'd need to take the value from the checkbox name (e.g. Input::get('box-ID'))

you could also create a checkbox using the form class:

// public function checkbox($name, $value = 1, $checked = null, $options = array())
{{ Form::checkbox('name', 'value', true, ['onClick' => 'alert(123)']) }}

reference: Formbuilder -> checkbox

Sign up to request clarification or add additional context in comments.

Comments

0

You should modify your form like this. It works me I hope will work for you also.

{{ Form::open(['route' => ['items.update', $items->id], 'class' => 'form-inline', 'method' => 'put']) }}

Thanks

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.