我现在正在使用全新安装的Laravel 5.4,我还安装了https://github.com/appzcoder/crud-generator。我使用生成器生成了Tickets CRUD。我能够发布"门票"使用" php artisan migrate"
将表转换为mysql数据库如果其中一个文本输入缺少输入,我目前无法尝试显示$ errors。
TicketsController.php
<?php
namespace App\Http\Controllers\Users;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\Ticket;
use Illuminate\Http\Request;
use Session;
class TicketsController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\View\View
*/
public function index(Request $request)
{
$keyword = $request->get('search');
$perPage = 25;
if (!empty($keyword)) {
$tickets = Ticket::where('user_id', 'LIKE', "%$keyword%")
->orWhere('subject', 'LIKE', "%$keyword%")
->orWhere('description', 'LIKE', "%$keyword%")
->paginate($perPage);
} else {
$tickets = Ticket::paginate($perPage);
}
return view('user.tickets.index', compact('tickets'));
}
/**
* Show the form for creating a new resource.
*
* @return \Illuminate\View\View
*/
public function create()
{
return view('user.tickets.create');
}
/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
*
* @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
*/
public function store(Request $request)
{
$requestData = $request->all();
Ticket::create($requestData);
Session::flash('flash_message', 'Ticket added!');
return redirect('tickets');
}
}
查看
<div class="panel panel-default">
<div class="panel-heading">Create New Ticket</div>
<div class="panel-body">
<a href="{{ url('/tickets') }}" title="Back"><button class="btn btn-warning btn-xs"><i class="fa fa-arrow-left" aria-hidden="true"></i> Back</button></a>
<br />
<br />
@if ($errors->any())
<ul class="alert alert-danger">
@foreach ($errors->all() as $error)
<li>{{ $error }}</li>
@endforeach
</ul>
@endif
{!! Form::open(['url' => '/tickets', 'class' => 'form-horizontal', 'files' => true]) !!}
@include ('user.tickets.form')
{!! Form::close() !!}
</div>
</div>
form.blade.php
<div class="form-group {{ $errors->has('user_id') ? 'has-error' : ''}}">
{!! Form::label('user_id', 'User Id', ['class' => 'col-md-4 control-label']) !!}
<div class="col-md-6">
{!! Form::number('user_id', null, ['class' => 'form-control']) !!}
{!! $errors->first('user_id', '<p class="help-block">:message</p>') !!}
</div>
</div><div class="form-group {{ $errors->has('subject') ? 'has-error' : ''}}">
{!! Form::label('subject', 'Subject', ['class' => 'col-md-4 control-label']) !!}
<div class="col-md-6">
{!! Form::text('subject', null, ['class' => 'form-control']) !!}
{!! $errors->first('subject', '<p class="help-block">:message</p>') !!}
</div>
</div><div class="form-group {{ $errors->has('description') ? 'has-error' : ''}}">
{!! Form::label('description', 'Description', ['class' => 'col-md-4 control-label']) !!}
<div class="col-md-6">
{!! Form::text('description', null, ['class' => 'form-control']) !!}
{!! $errors->first('description', '<p class="help-block">:message</p>') !!}
</div>
</div>
<div class="form-group">
<div class="col-md-offset-4 col-md-4">
{!! Form::submit(isset($submitButtonText) ? $submitButtonText : 'Create', ['class' => 'btn btn-primary']) !!}
</div>
</div>
如果没有输入主题文字输入,我会收到页面错误提示:
SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'subject' cannot be null (SQL: insert into `tickets` (`user_id`, `subject`, `description`, `updated_at`, `created_at`) values (1, , dsa, 2017-03-08 13:05:22, 2017-03-08 13:05:22))
而不是$ errors功能
我需要在TicketsController中包含一些内容吗?和中间件?
感谢任何帮助。
答案 0 :(得分:0)
对于这项工作,
$this->validate($request, [
'subject' => 'required',
]);
在动作方法中写上面的代码,然后检查。
以下是您可以预先形成validation in laravel
的方法