Видеокурс Laravel: создание ToDo List
Создаем чекбоксы для задач
Добавляем код в index.blade.php
@extends('todo.layouts.default')
@section('content')
<h1>Items</h1>
<ul>
@foreach ($items as $item)
<li>
{{ Form::open() }}
<input type="checkbox" name="item" id="item_{{ $item->id }}" {{ $item->done ? 'checked' : '' }}
onClick="this.form.submit()">
<input type="hidden" name="item_id" value="{{ $item->id }}" />
<label for="item_{{ $item->id }}">{{ e($item->name) }}</label>
{{ Form::close() }}
</li>
@endforeach
</ul>
@stop
Создадим маршрут для обработки функционала
Route::post('/todo', [
'uses' => 'TodoController@postIndex'
])->before('csrf');
Создаем метод обработки сохранения изменений в TodoController
public function postIndex() {
$id = Input::get('item_id');
$item = Item::findOrFail($id);
$userId = Auth::user()->id;
if($item->user_id == $userId) {
$item->mark();
}
return Redirect::route('todo');
}
Создаем в модели Item метод для сохранения изменений
class Item extends Eloquent{
public $timestamps = false;
public function mark() {
$this->done = $this->done ? false : true;
$this->save();
}
}