数据未存储在数据库中[Laravel 5.5]

时间:2017-10-31 13:45:28

标签: php html mysql laravel laravel-5

我正在使用Laravel 5.5。我想将数据存储在数据库中,但是当我尝试这样做时 - 什么也没发生。这是我的MarketController代码

public function store(Request $request)
{


    $this->validate($request, [
        'name' => 'required|unique:markets|max:255',
        'website' => 'required',
        'city' => 'required',
    ]);
    Market::create($request->all());
    return redirect('markets'); 
}

我的save.blade.php代码

<!DOCTYPE html>
 <html>
 <head>
   <title>Create Market</title>
 </head>
 <body>
  @extends('layouts.app')
  @section('main')
  <form action="{{ route('markets.store') }}" method="post">
    {{ csrf_field() }}
    <input type="hidden" name="_token" value="DaBSErIRWPPI">
    <label for="name">Name</label>
    <input type="text" name="name" id="name">
    <label for="city">Location</label>
    <input type="text" name="city" id="city">
    <label for="website">Market's website</label>
    <input type="text" name="website" id="website">
    <button type="submit"><a href="{{route('markets.store')}}">Add Market</a>
</button>
</form>
</form>

 

这是index.blade.php代码

<h1><a href="{{ route('markets.create', $market) }}">Add Market</a></h1>

什么都没发生。请帮忙。谢谢你!

1 个答案:

答案 0 :(得分:0)

您的路线应该显示为我的示例,因为我不知道您的路线页面标题:

Route::post('store', 'MarketController@store')->name('store');

实际上你应该运行:php artisan make:request StoreRequest

StorageRequest.php中的

authorizereturn false;更改为return true;,然后添加规则:

return [
    'name' => 'required|max:255',
    'website' => 'required',
    'city' => 'required'
]

然后可以简化您的控制器:

public function store(StoreRequest $storeRequest)
{
    $form = $storeRequest->all();

    Market::create([
        'name' => $form['name'],
        'website' => $form['website'],
        'city' => $form['city']
    ]);

    return redirect('markets');

}

您的表单稍微挤满了,这是一个细分版本:

<form action="{{ route('store') }}" method="post">
    {{ csrf_field() }}
    <label for="name">Name</label>
    <input type="text" name="name" id="name">

    <label for="city">Location</label>
    <input type="text" name="city" id="city">

    <label for="website">Market's website</label>
    <input type="text" name="website" id="website">

    <input type="submit" value="Submit">
</form>

<强>已更新

您的示例不会将错误返回到表单,因此这是另一个更新版本:

<form action="{{ route('store') }}" method="post">
    {{ csrf_field() }}
    <label for="name">Name</label>
    <input type="text" name="name" id="name">

    @if ($errors->has('name'))
    <span class="help-block">
        <strong>{{ $errors->first('name') }}</strong>
    </span>
    @endif

    <label for="city">Location</label>
    <input type="text" name="city" id="city">

    @if ($errors->has('city'))
    <span class="help-block">
        <strong>{{ $errors->first('city') }}</strong>
    </span>
    @endif

    <label for="website">Market's website</label>
    <input type="text" name="website" id="website">

    @if ($errors->has('website'))
    <span class="help-block">
        <strong>{{ $errors->first('website') }}</strong>
    </span>
    @endif

    <input type="submit" value="Submit">
</form>

现在,如果必填字段尚未填写,则会在表单上返回错误,说明哪个输入字段未填写。