You are on page 1of 8

TodosController

CRUD Example

<?php

namespace App\Http\Controllers;

use Session;
use App\Todo;
use Illuminate\Http\Request;

class TodosController extends Controller


{
public function index()
{
$todos = Todo::all();
return view ('todos')->with('todos',$todos);

public function store(Request $request)


{

//find table todo indatabase


//request connection with table
//return a view after task is done

$todo = new Todo; //Model

$todo->todo = $request->todo;

$todo->save();

Session::flash('success','Your todo is created');

return redirect()->back();

public function delete($id)


{
$todo = Todo::find($id);

$todo->delete();

Session::flash('success','Your todo is deleted');


return redirect()->route('todos');
}

public function update($id)


{
$todo = Todo::find($id);

$todo->update();

Session::flash('success','Your todo is updated');

return view ('update')->with('todo',$todo);


}

public function save(Request $request,$id )


{
$todo = Todo::find($id);

$todo->todo = $request->todo;

$todo->save();

return redirect()->route('todos');

public function completed($id)


{
$todo = Todo::find($id);

$todo->completed = 1;

$todo->save();

Session::flash('success','Your todo is finished !');

return redirect()->back();

}
Layout.blade.php

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">

<title>Todos</title>

<!-- Fonts -->


<link href="https://fonts.googleapis.com/css?family=Raleway:100,600"
rel="stylesheet" type="text/css">
<link rel="stylesheet"
href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css
" integrity="sha384-
MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO"
crossorigin="anonymous">

<!-- Styles -->


<style>
html, body {
background-color: #fff;
color: #636b6f;
font-family: 'Raleway';
font-weight: 100;
height: 100vh;
margin: 0;
}

.full-height {
height: 100vh;
}

.flex-center {
align-items: center;
display: flex;
justify-content: center;
}

.position-ref {
position: relative;
}

.top-right {
position: absolute;
right: 10px;
top: 18px;
}

.content {
text-align: center;
}

.title {
font-size: 30px;
}

.links > a {
color: #636b6f;
padding: 0 25px;
font-size: 12px;
font-weight: 600;
letter-spacing: .1rem;
text-decoration: none;
text-transform: uppercase;
}

.m-b-md {
margin-bottom: 30px;
}
</style>
</head>
<body>

@if(Session::has('success'))
<div class="alert alert-success" role="alert">
{{Session::get('success')}}
</div>

@endif
<div class="flex-center position-ref full-height">
@if (Route::has('login'))
<div class="top-right links">
<a href="{{ url('/login') }}">Login</a>
<a href="{{ url('/register') }}">Register</a>
</div>
@endif

<div class="content">
<div class="title m-b-md">
@yield('content')

</div>
</div>
</div>
</body>
</html>

Todos.blade.php

@extends('layout')

@section('content')

<div class="row">
<div class="col-lg-6 col-lg-offset-3">

<form action="/create/todo" method="post">

{{csrf_field()}}

<input type="text" class="form-control input -lg" name="todo"


placeholder="Insert a todo">

</form>

</div>
</div>

@foreach ($todos as $todo)


{{$todo->todo}} <a href="{{ route('todo.delete',['id'=>$todo->id]) }}"
class ="btn btn-danger">X</a>

<a href="{{ route('todo.update',['id'=>$todo->id]) }}"


class ="btn btn-info btn-xs">Update</a>

@if(!$todo->completed)
<a href= "{{ route('todo.completed',['id'=>$todo->id]) }}" class ="btn
btn- btn-success">✓</a>

@else

<span class="text-success">✓</span>

@endif
<hr>

@endforeach

@stop

Update.blade.php

@extends('layout')

@section('content')

<div class="row">
<div class="col-lg-18">

<form action= "{{route ('todo.save',['id'=>$todo->id])}}"


method="post">

{{csrf_field()}}

<input type="text" class="form-control input -lg" name="todo"


value="{{$todo->todo}}" placeholder="Insert a todo">

</form>

</div>
</div>

<a href="{{ route('todo.update',['id'=>$todo->id]) }}" class ="btn btn-


btn-xs">Update</a>
<hr>

@stop
Routes

<?php

/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| This file is where you may define all of the routes that are handled
| by your application. Just tell Laravel the URIs it should respond
| to using a Closure or controller method. Build something great!
|
*/

Route::get('/', function () {
return view('welcome');
});

Route::get('/new',[
'uses' => 'PagesController@new'

]);

Route::get('/todos',[
'uses' => 'TodosController@index',
'as'=> 'todos'

]);

Route::get('/todo/delete/{id}',[
'uses'=>'TodosController@delete',
'as'=>'todo.delete'
]);

Route::post('/create/todo',[
'uses'=> 'TodosController@store'

]);

Route::get('todo/update/{id}',[
'uses'=> 'TodosController@update',
'as'=>'todo.update'
]);

Route::post('/todo/save/{id}',[
'uses'=> 'TodosController@save',
'as'=>'todo.save'

]);

Route::get('/todos/completed/{id}',[
'uses'=> 'TodosController@completed',
'as'=>'todo.completed'

]);

You might also like