Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Laravel 5.3 - Basic Task List
public function up() {
Schema::create(‘tasks’, function(Blueprint $table){
$table->increments(‘id’);
$table->string(‘name’); // 追加
$table->timestamps();
});
}
$task = new Task;
$task->name = $request->get(‘name’);
$task->save();
return redirect('/');
@extends('layouts.app')
@section('content')
<div class="container">
<div class="panel-body">
<!-- Display Validation Errors -->
@include('common.errors')
<!-- New Task Form -->
<form action="{{ url('task') }}" method="POST" class="form-horizontal">
{{ csrf_field() }}
<!-- Task Name -->
<div class="form-group">
<label for="task" 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>
<!-- Add Task Button -->
<div class="form-group">
<div class="col-sm-offset-3 col-sm-6">
<button type="submit" class="btn btn-default">
<i class="fa fa-plus"></i> Add Task
</button>
</div>
</div>
</form>
</div>
<!-- TODO: Current Tasks -->
</div>
@endsection
<!DOCTYPE html>
<html lang="en">
<head>
<title>Laravel Quickstart - Basic</title>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link href="https://fonts.googleapis.com/css?family=Raleway:300,400,500,700" rel="stylesheet" type="text/css">
<link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.4.0/css/font-awesome.min.css" rel="stylesheet" type="text/css">
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css" rel="stylesheet" type="text/css">
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.0.0-alpha1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>
<style>
body {
font-family: 'Raleway';
margin-top: 25px;
}
button .fa {
margin-right: 6px;
}
.table-text div {
padding-top: 6px;
}
</style>
<script>
$(function () {
$('#task-name').focus();
});
</script>
</head>
<body>
<div class="container">
<!-- Create Task Form... -->
<nav class="navbar navbar-default">
<div class="container-fluid">
<div class="navbar-header">
<button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#navbar" aria-expanded="false" aria-controls="navbar">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand" href="#">Task List</a>
</div>
<div id="navbar" class="navbar-collapse collapse">
<ul class="nav navbar-nav">
&nbsp;
</ul>
</div>
</div>
</nav>
<!-- Current Tasks -->
</div>
@yield('content')
</body>
</html>
Route::get('/', function () {
//
});
Route::post('/task', function () {
//
});
Route::delete('/task/{task}', function () {
//
});
@if (count($tasks) > 0)
<div class="panel panel-default">
<div class="panel-heading">
Current Tasks
</div>
<div class="panel-body">
<table class="table table-striped task-table">
<!-- Table Headings -->
<thead>
<tr>
<th>Task</th>
<th>&nbsp;</th>
</tr>
</thead>
<!-- Table Body -->
<tbody>
@foreach ($tasks as $task)
<tr>
<!-- Task Name -->
<td class="table-text">
<div>{{ $task->name }}</div>
</td>
<td>
<!-- TODO: Delete Button -->
</td>
</tr>
@endforeach
</tbody>
</table>
</div>
</div>
@endif
<td>
<form action="{{ url('task/'.$task->id) }}" method="POST">
{{ csrf_field() }}
{{ method_field('DELETE') }}
<button type="submit" class="btn btn-danger">
<i class="fa fa-trash"></i> Delete
</button>
</form>
</td>
$task->delete();
return redirect('/');
@if (count($errors) > 0)
<!-- Form Error List -->
<div class="alert alert-danger">
<strong>Whoops! Something went wrong!</strong>
<br><br>
<ul>
@foreach ($errors->all() as $error)
<li>{{ $error }}</li>
@endforeach
</ul>
</div>
@endif
$tasks = Task::orderBy('created_at', 'asc')->get();
return view('task', [
'tasks' => $tasks // このデータがtask.blade.phpで使える
]);
use App\Task;
use Illuminate\Http\Request;
$validator = Validator::make($request->all(), [
'name' => 'required|max:10',
]);
if ($validator->fails()) {
return redirect('/')
->withInput()
->withErrors($validator);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment