目录
2.复制home.blade.php模板到post下命名为create.blade.php
一、创建文章增删改成提交的控制器
1、注释文章查看权限,非登录状态可以查看文章列表页
注释HomeController.php下//$this->middleware('auth');
修改模板文件
运行Sooma
2、创建提交控制器post
php artisan make:model Post --migration --resource
创建资源控制器(自动生成增删改查方法)和数据表
成功多了以下文件
3、创建数据表
增加数据表字段
更新数据表
php artisan migrate
4、创建提交post资源路由
Route::get('/', [App\Http\Controllers\HomeController::class, 'index'])->name( 'home'); Route::resource('/posts',App\Http\Controllers\PostController::class)->middleware('auth');
5、创建post控制器view目录post
二、文章添加功能实现
1.模板显示
2.复制home.blade.php模板到post下命名为create.blade.php
@extends('layouts.app')
@section('content')
<div class="container">
<div class="row justify-content-center">
<div class="col-md-8">
<h2>提交</h2>
@if($errors->any())
<div class="alert alert-danger">
<ul>
@foreach( $errors->all() as $error)
<li>{{ $error }}</li>
@endforeach
</ul></div>
@endif
@if(session('success'))
<div class='alert alert-success'>Updated Successfully!</div>
@endif
<form action="{{ route('posts.store') }}" method="post">
@csrf
<div class="form-group">
<label for="title">标题</label>
<input type="text" class="form-control" id="title" name="title">
</div>
<div class="form-group">
<label for="title">内容</label>
<textarea class="form-control" id="content" name="content" role="4"></textarea>
</div>
<button type="submit" class="btn btn-primary">提交</button>
</form>
</div>
</div>
</div>
@endsection
3、控制器实现post
/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function store(Request $request)
{
//
$request->validate(['title'=>'required','content'=>'required']);
$post=new Post;
$post->title=request('title');
$post->content=request('content');
$post->uid=\Auth::id();
$post->save();
return redirect()->to('/');
}
三、文章修改
1、控制器显示post
/**
* Show the form for editing the specified resource.
*
* @param \App\Models\Post $post
* @return \Illuminate\Http\Response
*/
public function edit(Post $post)
{
//
return view('post.edit',compact('post'));
}
2、创建edit.blade.php模板文件
@extends('layouts.app')
@section('content')
<div class="container">
<div class="row justify-content-center">
<div class="col-md-8">
<h2>修改提交</h2>
@if($errors->any())
<div class="alert alert-danger">
<ul>
@foreach( $errors->all() as $error)
<li>{{ $error }}</li>
@endforeach
</ul></div>
@endif
@if(session('success'))
<div class='alert alert-success'>Updated Successfully!</div>
@endif
<form action="{{ route('posts.update',[$post->id]) }}" method="post">
@csrf
@method('put')
<div class="form-group">
<label for="title">标题</label>
<input type="text" class="form-control" id="title" name="title" value="{{ $post->title }}">
</div>
<div class="form-group">
<label for="title">内容</label>
<textarea class="form-control" id="content" name="content" role="4" >{{ old('content',$post->content) }}</textarea>
</div>
<button type="submit" class="btn btn-primary">提交</button>
</form>
</div>
</div>
</div>
@endsection
3、文章修改保存控制器
public function update(Request $request, Post $post)
{
$request->validate(['title'=>'required','content'=>'required']);
$post->title=request('title');
$post->content=request('content');
$post->uid=\Auth::id();
$post->save();
return redirect()->route('posts.edit',[$post->id])->with('success',true);
}
四、文章列表显示
1、模板文件home.blade.php
@extends('layouts.app')
@section('content')
<div class="container">
<div class="row justify-content-center">
<div class="col-md-8">
@foreach(App\Models\Post::orderBy('created_at','DESC')->get() as $post)
<div class="card">
<div class="card-header">{{ $post->id }}&&{{ $post->title }}*****{{ $post->created_at }}</div>
<a href="{{ route('posts.edit',[$post->id] ) }}">修改</a>
<form action="{{ route('posts.destroy',[$post->id]) }}" method="post">
<button type="submit" class="btn alert-danger"><a href="#">删除</a></button>
@csrf
@method('delete')
</form>
<div class="card-body">
{{ $post->content }}
</div>
</div>
@endforeach
</div>
</div>
</div>
@endsection
五、删除文章
1、模板home.blade.php
<form action="{{ route('posts.destroy',[$post->id]) }}" method="post">
<button type="submit" class="btn alert-danger"><a href="#">删除</a></button>
@csrf
@method('delete')
</form>
2、删除控制器post
public function destroy(Post $post)
{
//
$post->delete();
return redirect()->to('/');
}