写了个laravel控制器:

legs+之专栏 legs+之专栏 1046 人阅读 | 1 人回复 | 2023-05-24

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?立即注册

x
  1. <?php

  2. namespace App\Http\Controllers;

  3. use Illuminate\Http\Request;
  4. use App\Models\Post;

  5. class PostController extends Controller
  6. {
  7.     //
  8.     public function addPost(){
  9.         return view('add-post');

  10.     }
  11.    
  12.     public function createPost(Request $request){

  13.         $post=new Post();
  14.         $post->title=$request->title;
  15.         $post->body=$request->body;
  16.         $post->save();
  17.         return back()->with('post_created','Post has been created successfully !!');

  18.     }

  19.     public function getPost(){
  20.         $posts= Post::orderBy('id','DESC')->get();
  21.         return view('posts',compact('posts'));

  22. }
  23. public function getPostById($id){
  24.     $post=Post::where('id',$id)->first();
  25.     return view('single-post',compact('post'));

  26. }
  27. public function deletePost($id){
  28.     Post::where('id',$id)->delete();
  29.     return back()->with('post_deleted','Post has been deleted successfully..!');

  30. }
  31. public function editPost($id){
  32.     $post=Post::find($id);
  33.     return view('edit-post',compact('post'));

  34. }
  35. public function updatePost(Request $request){

  36.     $post=Post::find($request->id);
  37.     $post->title=$request->title;
  38.     $post->body=$request->body;
  39.     $post->save();
  40.     return back()->with('post_updated','Post has been updated successfully !!');

  41. }

  42. }
复制代码

回答|共 1 个

legs+ 发表于 2023-5-24 11:49:30| 字数 549 | 显示全部楼层

  1. Route::get('/', function () {
  2.     return view('welcome');
  3. });

  4. Route::get('/add-post',[PostController::class,'addPost']);
  5. Route::post('/create-post',[PostController::class,'createPost'])->name('post.create');
  6. Route::get('/posts',[PostController::class,'getPost']);
  7. Route::get('/posts/{id}',[PostController::class,'getPostById']);
  8. Route::get('/delete-post/{id}',[PostController::class,'deletePost']);
  9. Route::get('/edit-post/{id}',[PostController::class,'editPost']);
  10. Route::post('/update-post',[PostController::class,'updatePost'])->name('post.update');
复制代码
上面是路由
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则