我如何处理laravel中的表单?

我如何处理laravel中的表单?,laravel,Laravel,我在laravel中处理表单,我已经编写了代码,但是一旦我点击按钮提交表单,它就会重置页面 下面是我的视图create.blade.php @extends('layouts.app') @section('content') <div class="container"> <form action="/p" enctype="multipart/form-data" method="post"> @csrf

我在laravel中处理表单,我已经编写了代码,但是一旦我点击按钮提交表单,它就会重置页面

下面是我的视图create.blade.php

@extends('layouts.app')

@section('content')
    <div class="container">
        <form action="/p" enctype="multipart/form-data" method="post">
            @csrf

            <div class="row">
                <div class="col-8 offset-2">

                    <div class="row">
                        <h1>Add New Post</h1>
                    </div>
                    <div class="form-group row">
                        <label for="caption" class="col-md-4 col-form-label">Post Caption</label>

                        <input id="caption"
                               type="text"
                               class="form-control{{ $errors->has('caption') ? ' is-invalid' : '' }}"
                               name="caption"
                               value="{{ old('caption') }}"
                               autocomplete="caption" autofocus>

                        @if ($errors->has('caption'))
                            <span class="invalid-feedback" role="alert">
                            <strong>{{ $errors->first('caption') }}</strong>
                        </span>
                        @endif
                    </div>

                    <div class="row">
                        <label for="image" class="col-md-4 col-form-label">Post Image</label>

                        <input type="file" class="form-control-file" id="image" name="image">

                        @if ($errors->has('image'))
                            <strong>{{ $errors->first('image') }}</strong>
                        @endif
                    </div>

                    <div class="row pt-4">
                        <button class="btn btn-primary">Add New Post</button>
                    </div>

                </div>
            </div>
        </form>
    </div>
@endsection
@extends('layouts.app'))
@节(“内容”)
@csrf
增加新职位
后标题
@如果($errors->has('caption'))
{{$errors->first('caption')}
@恩迪夫
后像
@如果($errors->has('image'))
{{$errors->first('image')}
@恩迪夫
增加新职位
@端部
还有我的PostsController.php

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class PostsController extends Controller
{
    public function create(){
        return view('posts.create');
    }

    public function store(){
        $data = request()->validate([
            'caption' => 'required',
            'image' => ['required', 'image'],
        ]);

        Post::create($data);

        dd(request()->all());
    }
}

问题出在您的路线上。get和post路由都将转到控制器的create方法。邮递路线会像

Route::post('/p', 'PostsController@store');

问题在你的路线上。get和post路由都将转到控制器的create方法。邮递路线会像

Route::post('/p', 'PostsController@store');

web.php
文件中更改这一行,从

Route::post('/p', 'PostsController@create');

正如您在上面的修改中所看到的,您指向了错误的控制器方法

此外,最好使用命名路由

使用上述命名路由,您现在可以使用route helper,而不用担心表单中的url:

<form action="{{ route('p.store') }}" enctype="multipart/form-data" method="post">
</form>
请注意,您以前使用的是全局请求帮助程序
request()
。您不需要再这样做了,因为请求对象现在作为参数传入。还请注意,使用路由时不需要传递任何实际参数。参数由Laravel自动传入

更新2:

另外,使用
$filleble
数组更新您的Post模型


更改
web.php
文件中的这一行,从

Route::post('/p', 'PostsController@create');

正如您在上面的修改中所看到的,您指向了错误的控制器方法

此外,最好使用命名路由

使用上述命名路由,您现在可以使用route helper,而不用担心表单中的url:

<form action="{{ route('p.store') }}" enctype="multipart/form-data" method="post">
</form>
请注意,您以前使用的是全局请求帮助程序
request()
。您不需要再这样做了,因为请求对象现在作为参数传入。还请注意,使用路由时不需要传递任何实际参数。参数由Laravel自动传入

更新2:

另外,使用
$filleble
数组更新您的Post模型


这是我现在面临的错误“Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException此路由不支持GET方法。支持的方法:POST。”仍然获取此错误Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException此路由不支持GET方法。支持的方法:POST。您确定您有
Route::POST('/p',…
),并且已将所有相关代码更新为答案代码吗?这是在我的routes web中。phpAuth::routes();Route::get('/p/create','PostsController@create路由::post('/p','PostsController@store');路由::get('/profile/{user}','ProfilesController@index')->名称('profile.show');下面是我现在面临的错误“Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException此路由不支持GET方法。支持的方法:POST。”仍在获取此错误Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException此路由不支持GET方法。支持的方法:POST。是否确实有
route::POST('/p',…
),并且已将所有相关代码更新为应答代码?这是我的路由web中的。phpAuth::routes();route::GET(“/p/create”,”PostsController@create路由::post('/p','PostsController@store');路由::get('/profile/{user}','ProfilesController@index“)->name('profile.show');仍然是。拉威尔是如此复杂。我仍然是新程序员。仍然是。拉威尔是如此复杂。我仍然是新程序员