1

我正在尝试通过 POST 从我的表单中存储数据,但它没有达到我的存储操作,我尝试了几种方法,但都没有对我有用。

ContactController 中的存储方法:

public function store(StoreMessage $request)
    {
        Log::info('Storing message: '.$request);
        Message::create([
            'name' => $request->name,
            'email' => $request->email,
            'phonenumber' => $request->phonenumber,
            'text' => $request->text
        ]);
    }

看法:

{!! Form::open(['action' => 'ContactController@store']) !!}
            {!! Form::label('name', 'Name') !!}
            {!! Form::text('name', null, ['class' => 'form-control', 'required']) !!}

            {!! Form::submit('Creëer de activiteit!', array('class' => 'btn btn-primary', 'id' => 'createSubmit')) !!}
        {!! Form::close() !!}

路线:

Route::get('/contact', 'ContactController@index');
Route::post('/contact', 'ContactController@store');

这是 StoreMessage 请求:

<?php
namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;

class StoreMessage extends FormRequest
{
    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        return true;
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        return [
            'name' => 'required',
            'email' => 'required',
            'text' => 'required',
        ];
    }
}
4

1 回答 1

0

您可以尝试像这样的正常请求Request $request,并检查它是否正常工作。

您还可以使用$request以下代码进行验证。

public function store(Request $request)
{
     $rules = [
        'name' => 'required',
        'email' => 'required',
        'text' => 'required',
     ];
     $this->validate($request,$rules);
     Log::info('Storing message: '.$request);
     Message::create([
        'name' => $request->name,
        'email' => $request->email,
        'phonenumber' => $request->phonenumber,
        'text' => $request->text
     ]);
}
于 2017-05-19T14:24:20.963 回答