Create方法停止工作

时间:2018-04-27 12:46:44

标签: laravel controller

我有这个简单的代码

    public function store(Request $request)
{
    $this->Validate($request, [
        'name' => 'required|min:3|max:20',
        'age' => 'required|integer|between:18,99',
        'height' => 'required|integer|between:120,230',
        'weight' => 'required|integer|between:30,150',
        'region' => 'required|integer',
        'city' => 'required',
        'phone' => 'required|integer'
    ]);
    Person::create([
        'name' => $request->name,
        'age' => $request->age,
        'height' => $request->height,
        'weight' => $request->weight,
        'city_id' => $request->city,
        'region_id' => $request->region,
        'phone' => $request->phone,
        'user_id' => Auth::user()->id
    ]);
}

此代码停止工作,但它不会显示错误。我只有白页。此代码以前有效。并且记录不会创造。发生了什么事? @编辑 现在它可以工作但是...在phpmyadmin一切都很好。新记录被添加到coccectly但修补程序显示我在phpmyadmin中手动添加的唯一一条记录。如果我尝试选择我添加的最新记录:

$latest = Person::where('user_id', Auth::user()->id)->latest()->first();

它向我展示了我在phpmyadmin中创建的这个元素。怎么了?

2 个答案:

答案 0 :(得分:1)

将商店替换如下:

public function store(Request $request) {
      $this->Validate($request->all(), [
        'name' => 'required|min:3|max:20',
        'age' => 'required|integer|between:18,99',
        'height' => 'required|integer|between:120,230',
        'weight' => 'required|integer|between:30,150',
        'region' => 'required|integer',
        'city' => 'required',
        'phone' => 'required|integer'
    ]);
    $person= new Person();
    $person->username= $request['username'];
    $person->age = $request['age'];
    $person->height = $request['height'];
    $person->weight = $request['weight'];
    $person->region = $request['region'];
    $person->city = $request['city'];
    $person->phone = $request['phone'];
    $person->save();

}

答案 1 :(得分:0)

在您的控制器中:

   public function store(Request $request)
   {
    $this->validate($request, [
        'name' => 'required|min:3|max:20',
        'age' => 'required|integer|between:18,99',
        'height' => 'required|integer|between:120,230',
        'weight' => 'required|integer|between:30,150',
        'region' => 'required|integer',
        'city' => 'required',
        'phone' => 'required|integer'
    ]);

    Person::create([
        'name' => $request->name,
        'age' => $request->age,
        'height' => $request->height,
        'weight' => $request->weight,
        'city_id' => $request->city,
        'region_id' => $request->region,
        'phone' => $request->phone,
        'user_id' => Auth::user()->id
    ]);
    return redirect()->back()->with('success','Person Created');

}
在您的刀片视图中

@if (count($errors) > 0)
  @foreach ($errors->all() as $error)
    <p class="alert alert-danger alert-dismissible fade show" role="alert">{{ $error }}
    <button type="button" class="close" data-dismiss="alert" aria-label="Close">
    <span aria-hidden="true">&times;</span>
    </button>
  </p>
  @endforeach
@endif