【问题标题】:Update edited post更新已编辑的帖子
【发布时间】:2021-05-02 11:07:35
【问题描述】:

嘿,我想知道是否有人可以提供帮助,我需要编辑和更新帖子,我已经完成了编辑部分,但更新我已经坚持了好几个小时了

编辑blade.php

  <h2 class="text-center">Edit</h2>
<form action="{{ route('posts.update',['post' => $post]) }}" method="Post">

 @method('PUT')
    
    @csrf
    <textarea input="" class="col-12 pb-5 mt-4"  name="edit">{{$post->content}} 
    </textarea>
    <div class="row justify-content-center">
            <button type="submit"  class="btn btn-primary col-2 mt-4">Edit</button>
        </div>              
</form>

@endsection

web.php

Route::get('/posts/{post}/edit', [PostController::class, 'edit'])->middleware(['auth']);
Route::put('post/{post}','PostController@update')->middleware(['auth']);

后控制器

public function store(Request $request)
{
    //get the current user
    $user = Auth::user();
    //attach the content to allow comment 
    $user->posts()->create([
        'content' => $request->input('content'),
        'allowComment' => true,
    ]);

    // return to posts page
    return redirect('/posts');
}

后控制器

//edit returns a view 
public function edit(Post $post)
{
    return view('edit', ['post' => $post]); 
}

更新

/**
 * Update the specified resource in storage.
 *
 * @param  \Illuminate\Http\Request  $request
 * @param  \App\Models\Post  $post
 * @return \Illuminate\Http\Response
 */
 public function update(Request $request, Post $post)
 {
     //get the current user
     $user = Auth::user();
     //attach the content to allow comment 
     $user->posts()->create([
         'content' => $request->input('content'),
         'allowComment' => true,
     ]);
     // return to posts page
 

     return redirect('/posts');
 }

【问题讨论】:

  • 欢迎来到 SO ... 那有什么问题?
  • 嘿,我有一个帖子,我可以编辑帖子,但我无法更新帖子,我是初学者,我认为它只是找不到更新的帖子
  • 为什么不能,问题是什么,实际发生了什么,是不是报错了......
  • 所以我收到此错误 rn 此路由不支持 PUT 方法。支持的方法:GET、HEAD、POST。
  • 但我认为它根本没有链接到更新的功能

标签: php laravel routes


【解决方案1】:
<form action="{{ route('posts.update',['post' => $post]) }}" method="Post">

 @method('PUT')
    
    @csrf
    <textarea input="" class="col-12 pb-5 mt-4"  name="edit">{{$post->content}} 
    </textarea>
    <div class="row justify-content-center">
            <button type="submit"  class="btn btn-primary col-2 mt-4">Edit</button>
        </div>              
</form>

将整个对象传递给路由而不是传递id

【讨论】:

  • 嘿,出现了一个不同的错误 Action PostController@update 未定义。 (查看:/Users/ash/Code/socialnetwork/resources/views/edit.blade.php)
  • 不是我在 web.php 中所做的 Route::put('/posts/{post}', [PostController::class, 'update'])->middleware(['auth' ]);
  • 请将您的路由定义为 Route::put('post/{post}','PostController@update')->middleware(['auth']);
  • 目标类 [PostController] 不存在。
  • 请检查您的控制器名称,您的控制器似乎被命名为 postController 而不是 PostController
【解决方案2】:

在所有的cmets之后......

<form action="/posts" method="Post">

应该是

<form action="/posts/{{ $post->id }}" method="Post">

【讨论】:

  • 未定义变量:post_id(查看:/Users/ash/Code/socialnetwork/resources/views/edit.blade.php)
  • 你应该使用来自你的模型的任何东西,它应该是 {{$post->getKey()}}
  • @ashley 这只是这个答案的错字。我编辑了,现在试试
  • 在我的终端中,这是域 |方法 |网址 |姓名 |动作 PUT|补丁 |帖子/{帖子} |帖子更新 | App\Http\Controllers\PostController@update
  • 它让我回到帖子页面,但没有更新
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-05-21
  • 2023-04-02
  • 1970-01-01
  • 2015-07-18
  • 2020-01-20
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多