-2

我正在尝试将数据插入数据库,既不显示错误也不将数据保存到我的数据库中,下面是代码。这是创建刀片 php 页面。

@extends('layouts.app')

@section('content')


<h1>Create a Camp</h1>

<!-- if there are creation errors, they will show here -->
{{ HTML::ul($errors->all()) }}

{{ Form::open(array('url' => 'camps')) }}


    <div class="form-group">
        {{ Form::label('name', 'Name') }}
        {{ Form::text('name', Request::old('name'), array('class' => 'form-control')) }}
    </div>

     <div class="form-group">
        {{ Form::label('district', 'District') }}
        {{ Form::text('district', Request::old('district'), array('class' => 'form-control')) }}
    </div>

     <div class="form-group">
        {{ Form::label('address', 'Address') }}
        {{ Form::text('address', Request::old('address'), array('class' => 'form-control')) }}
    </div>

     <div class="form-group">
        {{ Form::label('block', 'Block') }}
        {{ Form::text('block', Request::old('block'), array('class' => 'form-control')) }}
    </div>

     <div class="form-group">
        {{ Form::label('population', 'population') }}
        {{ Form::text('population', Request::old('population'), array('class' => 'form-control')) }}
    </div>


    {{ Form::submit('Create the Camp!', array('class' => 'btn btn-primary')) }}

{{ Form::close() }}

@endsection

这是控制器,当我手动输入从数据库中获取的数据时,索引控制器工作正常这是存储控制器,它也不验证表单数据,我是新手,我不知道该怎么做

  public function store(Request $request)
    {
        $rules = array(
            'name'    => 'required',
            'district'=> 'required',
            'address' => 'required',
            'block'   => 'required|numeric',
            'Population'   => 'required|numeric',
        );
        $validator = Validator::make(Request::all(), $rules);
        if ($validator->fails()) {
            return Redirect::to('camp/create')
                ->withErrors($validator)
                ->withRequest(Request::except('password'));
        } else {
            // store
            $camp = new Camp;
            $camp->name       = Request::get('name');
            $camp->district      = Request::get('district');
            $camp->address = Request::get('address');
            $camp->block      = Request::get('block');
            $camp->population = Request::get('population');
            $camp->save();

            // redirect
            Session::flash('message', 'Successfully created Camp!');
            return view('camp\camps',['camps'=>$camps]);
        }
    }
4

1 回答 1

0

您忘记在 Camp 之后添加括号,它应该是这样的:

$camp = new Camp();
$camp->name       = $request->name;
$camp->district   = $request->district;
$camp->save();
于 2020-06-09T10:33:19.493 回答