【问题标题】:Neither it show an error nor save the Data into my database PHP Laravel?它既不显示错误也不将数据保存到我的数据库 PHP Laravel 中?
【发布时间】:2020-09-28 07:46:39
【问题描述】:

我正在尝试将数据插入数据库,既没有显示错误,也没有将数据保存到我的数据库中,下面是代码。这是创建刀片 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]);
        }
    }

【问题讨论】:

  • 能否添加try catch块看看问题。
  • 您返回了$validator 对象。但是这应该是 $validator-&gt;errors() 以返回错误。
  • 我改变了 return $validator->errors();但它仍然没有将数据保存到我的表中,也没有显示错误
  • @Deepak 这个例子可以使用$this-&gt;validate($request, $rules) 来简化。我不明白为什么你需要在这里手动创建一个错误包并返回一个重定向响应......

标签: php laravel laravel-blade laravel-controller laravel-datatables


【解决方案1】:

Camp后面忘了加括号应该是这样的:

$camp = new Camp();
$camp->name       = $request->name;
$camp->district   = $request->district;
$camp->save();

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-03-12
    • 2019-09-22
    • 2015-02-05
    • 1970-01-01
    • 2019-10-09
    • 1970-01-01
    • 2018-01-08
    • 1970-01-01
    相关资源
    最近更新 更多