如何修复使用Laravel不支持POST方法的错误[解决]

lokaqttq  于 2023-10-22  发布在  其他
关注(0)|答案(5)|浏览(149)

我想在我的Laravel网站上有一个帖子消息功能。
我尝试改变请求方法并模拟教程。
这是我的控制器

public function index()
    {
        $messages = DB::table('messages')->where('user_id', 1)->get();
        // dd($messages)
        return view('home', [
            'messages' => $messages
        ]);
    }

以下是我的观点

<div class="card">
    <div class="card-header">Recent updates</div>

    <div class="card-body">
        <form action="{{ route('home') }}" method="post">
            <textarea name="body" id="body "rows="3" class="form-control" placeholder="What's in your mind"></textarea>
            <button type="submit" name="post" class="btn btn-primary">Post</button>
        </form>
    </div>
    <hr>
    <div class="card-body">
        @foreach($messages as $message)
            {{ $message->body }}
            <br />
            <small>{{ $message->create_at }}</small>
        @endforeach
    </div>
</div>

这是我的路线

Route::get('/home', 'HomeController@index')->name('home');

我需要做一个留言功能。

vpfxa7rd

vpfxa7rd1#

您的home路由只为GET请求注册(Route::get),您还需要为POST请求定义一个路由。你可以这样做:

Route::post('/home', 'HomeController@create')->name('status-update');

然后,您还需要将表单操作更改为新路由

<form action="{{ route('status-update') }}" method="post">

参考Laravels优秀文档的路由章节以了解更多细节:https://laravel.com/docs/5.8/routing

nwnhqdif

nwnhqdif2#

这是非常简单的,你应该看一些教程和阅读laravel docs,这里是一个基本的post请求的例子。

public function post_request(Request $request)
{
    $this->validate($request, [
      'body' => 'required|string'
    ]);

   Model::create($request->only('body'));

   return redirect()->back();
}

web.php:

Route::post('sample/post','Mycontroller@post_request')->name('post.test')

view.blade:

<form action="{{ route('post.test') }}" method="post">
    @csrf
   <textarea name="body" id="body "rows="3" class="form-control" placeholder="What's in your mind"></textarea>
  <button type="submit" name="post" class="btn btn-primary">Post</button>
</form>

谢谢.

okxuctiv

okxuctiv3#

你必须创建一个这样的发布路由:

Route::post('/home','HomeController@homeFunction');

不要忘记将@csrf转换为html格式

ffscu2ro

ffscu2ro4#

//viewing

<div class="card">
    <div class="card-header">Recent updates</div>

    <div class="card-body">
        <form action="{{ route('home') }}" method="post">
             {{ csrf_field() }}
            <textarea name="body" id="body "rows="3" class="form-control" placeholder="What's in your mind" required></textarea>
            <button type="submit" name="post" class="btn btn-primary">Post</button>
        </form>
    </div>
    <hr>
    <div class="card-body">
        @foreach($messages as $message)
            {{ $message->body }}
            <br />
            <small>{{ $message->create_at }}</small>
        @endforeach
    </div>
</div>

//控制器

public function post_record(Request $request)
{
     $this->validate($request, [

          'body'      => 'required|string',
          ]);

     $message=$request->input('body');

     DB::table('table')->insert(['msgfield'=>$message]);
}

//route

Route::post('/home','YourController@post_record')->name('home');
sz81bmfz

sz81bmfz5#

正确答案由萨勒曼·扎法尔回答
控制器中

public function post_request(Request $request)
{
    $this->validate($request, [
      'body' => 'required|string'
    ]);

   Model::create($request->only('body'));

   return redirect()->back();
}

In途径

Route::post('sample/post','Mycontroller@post_request')->name('post.test');

鉴于

<form action="{{ route('post.test') }}" method="post">
    @csrf
   <textarea name="body" id="body "rows="3" class="form-control" placeholder="What's in your mind"></textarea>
   <button type="submit" name="post" class="btn btn-primary">Post</button>
</form>

相关问题