1、快速创建 2、给你的项目取名字 3、这个页面等待一会(如果时间过长,改变composer镜像地址) 4、重新加载Apache,出现以下界面,说明创建成功 5、创建模型
1、index.blade.php 用于列表展示 在视图中引入bootstrap模板
@extends('layouts.app') @section('title', 'TODO LIST') @section('content') <h1 class="text-center my-5">Todo Page</h1> <div class="row justify justify-content-center"> <div class="col-md-8"> <div class="card"> <div class="card-header"> Todo List </div> <div class="card-body"> <ul class="list-group"> @foreach($todos ?? '' as $todo) <li class="list-group-item"> <span @if($todo->is_completed) style="text-decoration: line-through"@endif> {{$todo->name}} </span> {{-- {{$todo->name}} @if($todo->is_completed) <span class="badge badge-pill badge-success">已完成</span> @else <span class="badge badge-pill badge-secondary">未完成</span> @endif --}} <a class="btn btn-primary float-right" href="/todos/{{$todo->id}}">View</a> <form action="/todos/{{$todo->id}}" method="post" class="d-inline-block float-right mr-2"> @method('delete') @csrf <button type="submit" class="btn btn-danger">Delete</button> </form> @if(!$todo->is_completed) <form action="/todos/{{$todo->id}}/completed" method="post" class="d-inline-block float-right mr-2"> @method('patch') @csrf <button type="submit" class="btn btn-warning">Complete</button> </form> @endif </li> @endforeach </ul> </div> </div> </div> </div> @endsection2、show.blade.php用于展示单个资源
@extends('layouts.app') @section('title', 'TODO SHOW') @section('content') <h1 class="text-center my-5">{{$todo->name}}</h1> <div class="row justify justify-content-center"> <div class="col-md-8"> <div class="card"> <div class="card-header font-weight-bold d-flex align-items-center justify-content-between"> Detail @if($todo->is_completed) <span class="badge badge-pill badge-success float-right">已完成</span> @else <span class="badge badge-pill badge-danger float-right">未完成</span> @endif </div> <div class="card-body"> {{$todo->description}} </div> <div class="card-footer"> <a href="/todos/{{$todo->id}}/edit" class="btn btn-block btn-outline-success">Edit</a> </div> </div> </div> </div> @endsection3、create.blade.php创建页的实现
@extends('layouts.app') @section('title', 'TODO CREATE') @section('content') <h1 class="text-center my-5">CREATE A NEW TODO</h1> <div class="row justify justify-content-center"> <div class="col-md-8"> <div class="card"> <div class="card-header"> New </div> <div class="card-body"> <form action="/todos" method="post"> @csrf <div class="form-group"> <label for="name">Name</label> <input type="text" name="name" id="name" class="form-control @error('name') is-invalid @enderror"> @error('name') <div class="invalid-feedback"> {{$message}} </div> @enderror </div> <div class="form-group"> <label for="description">Description</label> <textarea name="description" id="description" rows="3" class="form-control @error('description') is-invalid @enderror"></textarea> @error('description') <div class="invalid-feedback"> {{$message}} </div> @enderror </div> <button type="submit" class="btn btn-primary float-right">Submit</button> </form> </div> </div> </div> </div> @endsection4、edit.blade.php编辑页的实现
// @extends('la youts.app') @section('title', 'TODO EDIT') @section('content') <h1 class="text-center my-5">EDIT TODO</h1> <div class="row justify justify-content-center"> <div class="col-md-8"> <div class="card"> <div class="card-header"> EDIT </div> <div class="card-body"> <form action="/todos/{{$todo->id}}" method="post"> @csrf <div class="form-group"> <label for="name">姓名</label> <input type="text" name="name" id="name" value="{{$todo->name}}" class="form-control @error('name') is-invalid @enderror"> @error('name') <div class="invalid-feedback"> {{$message}} </div> @enderror </div> <div class="form-group"> <label for="description">学号</label> <textarea name="description" id="description" rows="3" class="form-control @error('description') is-invalid @enderror"> {{$todo->description}}</textarea> @error('description') <div class="invalid-feedback"> {{$message}} </div> @enderror </div> <button type="submit" class="btn btn-primary float-right">Submit</button> </form> </div> </div> </div> </div> @endsection1、获取方法 创建页
public function show(Todo $todo) { //dd($todoId); //$todo = Todo::findOrFail($todoId); //return $todo; return view('todos.show')->with(['todo'=> $todo]); } public function create() { return view('todos.create'); }2、记录时间按倒序排列
public function index() { $todos = Todo::query()->orderByDesc('created_at')->get(); //$todos = Todo::all();//取得模型中的所有数据 //$items = Todo::all(); //return view('todos.index'); return view('todos.index')->with(['todos' => $todos]);//将数据送给视图 }3、创建页的提交
public function store(Request $request) { $request->validate([ 'name' => 'required|min:3|max:20', 'description' => 'required' ]); $name = $request->get('name'); $description = $request->get('description'); $todo = new Todo(); $todo->name = $name; $todo->description = $description; $todo->save(); $request->session()->flash('success-info', 'Create successful!'); return redirect('/todos'); }4、编辑页获取
public function edit(Todo $todo) { //dd($todoId); //$todo = Todo::findOrFail($todoId); //return $todo; return view( 'todos.edit')->with(['todo'=> $todo]); }5、添加后端验证
$request->validate([ 'name' => 'required|min:3|max:50', 'description' => 'required' ]);6、编辑页提交
public function update(Todo $todo, Request $request) { $request->validate([ 'name' => 'required|min:3|max:50', 'description' => 'required' ]); $name = $request->get('name'); $description = $request->get('description'); //$todo = Todo::findOrFail($todoId); $todo->name = $name; $todo->description = $description; $todo->save(); $request->session()->flash('success-info', 'Update successful!'); return redirect("/todos/{$todo->id}"); }7、删除
```php public function destroy(Todo $todo, Request $request) { //$todo = Todo::findOrFail($todoId); try{ $todo->delete(); }catch (\Exception $exception){ dd($exception); } $request->session()->flash('success-info', 'Delete successful!'); return redirect('/todos'); }8、完成
public function complete(To do $todo) { $todo->is_completed = true; $todo->save(); return redirect('/todos'); } }