美丽心灵公益论坛

查看: 1539|回复: 1

写了个laravel控制器:

[复制链接]
累计签到:534 天
连续签到:1 天

887

主题

3114

回帖

4万

积分

版主

Rank: 7Rank: 7Rank: 7

积分
48404
发表于 2023-5-24 11:48:03| 字数 1,079 | 显示全部楼层 |阅读模式

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

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

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. }
复制代码

累计签到:534 天
连续签到:1 天

887

主题

3114

回帖

4万

积分

版主

Rank: 7Rank: 7Rank: 7

积分
48404
 楼主| 发表于 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');
复制代码
上面是路由
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

Archiver|手机版|小黑屋|免责及版权声明|关于|美丽心灵公益论坛

GMT+8, 2025-10-29 15:01 , Processed in 0.039600 second(s), 27 queries .

Powered by Discuz! X3.4

!copyright!

快速回复 返回顶部 返回列表