Laravel validation with unique fields - php

I have been trying unique validation in laravel 5.7. Below is my code for validation.
$this->validate($request, [
'name' => 'required|unique:permissions,name',
'slug' => 'required|unique:permissions,slug',
]);
And the html is below:
<div class='container'>
{!! Form::open(array('route' => 'permission.save','method'=>'POST')) !!}
<div class="row">
<div class="col-xs-12 col-sm-12 col-md-12">
<div class="form-group">
<strong>Name:</strong>
{!! Form::text('name', null, array('placeholder' => 'Name','class' => 'form-control')) !!}
</div>
<div class="form-group">
<strong>Slug:</strong>
{!! Form::text('slug', null, array('placeholder' => 'Slug','class' => 'form-control')) !!}
</div>
<div class="form-group">
<strong>Description:</strong>
{!! Form::textarea('description', null, array('placeholder' => 'Description','class' => 'form-control')) !!}
</div>
</div>
<div class="col-xs-12 col-sm-12 col-md-12 text-center">
<button type="submit" class="btn btn-primary">Submit</button>
</div>
</div>
{!! Form::close() !!}
</div>
Unique validation for 'name' is working fine but it is not working for 'slug'. It's really weird i can't get it. Please provide suggestions to fix this issue. Any help would be great.

You need to specify column name from database table in unique validation rule
https://laravel.com/docs/5.6/validation#rule-unique
for example your column name for slug is : column_slug
$this->validate($request, [
'name' => 'required|unique:permissions,name',
'slug' => 'required|unique:permissions,column_slug', // column_slug may be different in your case
]);
And ensure that you have set unique key for that column in database table
Hope it helps !

Related

Laravel blade views not showing updated content

I have a Laravel site running on a GoDaddy share hosting plan. I'm on PHP version 7.1. It has functioned fine up until yesterday. Now the blade views no longer show changes that occur on the database.
I see that the POST functions are working, as the changes are reflected in the database. They're just not showing up in the views themselves.
I've tried to clear the view cache as suggested here: Blade view not reflecting changes
I have also changed the timezone in config/app to 'America/Phoenix' to try and match godaddy's servers: https://www.godaddy.com/community/cPanel-Hosting/How-To-Change-Timezone-on-a-shared-server/td-p/102712
I've contacted GoDaddy's tech support and they couldn't find anything they believed could cause it.
Example Route:
//Resource
Route::resource('beer', 'BeerController');
Example Controller:
public function update(Request $request, Beer $beer)
{
$this->validate($request, ['name' => 'required']);
$beer->update(request(['name', 'beer_style_id', 'style', 'abv', 'ibus', 'srm', 'brewery_id', 'on_tap']));
return view('beers.show', compact('beer'));
}
Example View
#extends('layouts.master')
#section('content')
<div class="row">
<div class="col-sm-12">
<h1>Edit {{ $beer->name }}</h1>
<hr />
{!! Form::model($beer, ['route' => ['beer.update', $beer->id], 'method' => 'patch']) !!}
<div class="form-group">
{!! Form::label('name', 'Name') !!}
{!! Form::text('name', $value = null, ['class' => 'form-control']) !!}
</div>
<div class="form-group">
{!! Form::label('brewery_id', 'Brewery') !!}<br />
{!! Form::select('brewery_id', $breweries, null, ['placeholder' => 'Select Brewery', 'class' => 'custom-select mb-2', 'style' => 'width:100%;']) !!}
</div>
<div class="row" style="margin-bottom: 1rem;">
<div class="col">
{!! Form::label('beer_style_id', 'Style Family') !!}<br />
{!! Form::select('beer_style_id', $beerstyles, null, ['placeholder' => 'Select Style', 'class' => 'custom-select mb-2', 'style' => 'width:100%;']) !!}
</div>
<div class="col">
{!! Form::label('style', 'Style') !!}
{!! Form::text('style', $value = null, ['class' => 'form-control']) !!}
</div>
</div>
<div class="row" style="margin-bottom: 1.5rem;">
<div class="col">
{!! Form::label('abv', 'ABV') !!}
<div class="input-group">
{!! Form::number('abv', $value = null, ['class' => 'form-control', 'step' => '.1']) !!}
<span class="input-group-addon">%</span>
</div>
</div>
<div class="col">
{!! Form::label('ibus', 'IBUs') !!}
{!! Form::number('ibus', $value = null, ['class' => 'form-control']) !!}
</div>
<div class="col">
{!! Form::label('srm', 'SRM') !!}
{!! Form::number('srm', $value = null, ['class' => 'form-control', 'step' => '.1']) !!}
</div>
</div>
<div class="form-group" id="on-tap-checkbox">
{!! Form::checkbox('on_tap', '1') !!} On Tap
</div>
<div class="form-group">
{!! Form::submit('Save', ['class' => 'btn btn-primary']) !!}
Cancel
</div>
#include('layouts.errors')
{!! Form::close() !!}
</div>
</div>
#endsection

Search data from database in textbox to display - Laravel

my problem is i cannot search and cannot disply the values in my texboxes.
what i want is to search the id of each user and display its data to my textbox
How can I do this on this video This Video in Laravel?
as of now I have this
here is my page
View
{!! Form::open(['action' => 'Admin\EmployeeFilemController#search', 'method' => 'POST', 'enctype' => 'multipart/form-data']) !!}
<input type="text" name="id" class="form-control" placeholder="Enter ID to Search"><br>
<input type="submit" class="btn btn-primary btn-md" name="search" value="Search Data">
{!! Form::close() !!}
Controller
public function search(Request $request){
$output = "";
$employees = DB::table('employeefms')->where('id')->get();
return redirect('/admin/employeemaintenance');
}
my View Inputs
<div class="form-group col-md-2">
{{Form::label('employee_no', 'Employee No.')}}
{{Form::text('employee_no', '',['class' => 'form-control', 'placeholder' => 'Employee No.'])}}
</div>
<div class="row">
<div class="form-group col-md-4">
{{Form::label('last_name', 'Last Name')}}
{{Form::text('last_name', '',['class' => 'form-control', 'placeholder' => 'Last Name'])}}
</div>
<div class="form-group col-md-4">
{{Form::label('first_name', 'First Name')}}
{{Form::text('first_name', '',['class' => 'form-control', 'placeholder' => 'First Name'])}}
</div>
</div>
<div class="row">
<div class="form-group col-md-4">
{{Form::label('middle_name', 'Middle Name')}}
{{Form::text('middle_name', '',['class' => 'form-control', 'placeholder' => 'Middle Name'])}}
</div>
<div class="form-group col-md-4">
{{Form::label('nick_name', 'Nick Name')}}
{{Form::text('nick_name', '',['class' => 'form-control', 'placeholder' => 'Nick Name'])}}
</div>
</div>
You don't seem to pass the id entered by the user in your controller function.
$employees = DB::table('employeefms')->where('id')->get();
You may have to do the following changes
$input = $request->all();
$id = $input['id']
// $employees = DB::table('employeefms')->where('id', $id)->get();
// actually, if 'id' is the primary key, you should be doing
$employee = DB::table('employeefms')->find($id);
// now pass the data to the view where you want to display the record
// like so
return view('name_of_view', compact('employee'));
Then, use Laravel's Form-Model binding
{!! Form::model($employee,
['action' => ['Admin\EmployeeFilemController#update', $employee->id],
'method' => 'patch' // or whatever method you have defined
]) !!}
// your form fields specified above will go here
{!! Form::close() !!}

Laravel form with <div> tags in the middle

I am trying to use a form with div tags in the middle. However, it doesnt seem to work, the form works fine when used without them. Is there any way to stylise the form and use it ?
{!! Form::open(['action' => 'ProductController#store', 'method' => 'POST', 'enctype' => 'multipart/form-data']) !!}
<div class="centre col-lg-4 col-lg-offset-4 ">
<h2>Product</h2>
{{Form::text('', '', ['class' => 'form-control', 'placeholder' => 'Product'])}}
</div>
<div>
<div class="section3 ">
<div class="container centre">
{{Form::submit('Submit', ['class'=>'btn btn-primary'])}}
</div>
</div>
</div>
{!! Form::close() !!}
You have some unwanted </div> in your code which leads form to close in a wrong place, fix the <div> and it will start working again. Basically, this is an error due to the non-well-formatted code.
<div>
{!! Form::open(['action' => 'ProductController#store', 'method' => 'POST', 'enctype' => 'multipart/form-data']) !!}
<div class="centre col-lg-4 col-lg-offset-4 ">
<h2>Product</h2>
{{Form::text('', '', ['class' => 'form-control', 'placeholder' => 'Product'])}}
</div>
<div class="section3 ">
<div class="container centre">
{{Form::submit('Submit', ['class'=>'btn btn-primary'])}}
{!! Form::close() !!}
</div>
</div>
</div>
And don't forget to add name and other parameters in your code to make your form worthy.
{{Form::text('name', null, ['class' => 'form-control', 'placeholder' => 'Product'])}}
first the div tag is not proprly closed ..you have two unwanted tag in first div .
you can style according to your design need .
.
<div class="container">
<div class="well">
{!! Form::open(['action' => 'ProductController#store', 'class' => 'form-inline' , 'method' => 'POST', 'enctype' => 'multipart/form-data']) !!}
<fieldset>
<div class="form-group">
{!! Form::label('product', 'Product:', ['class' => 'col-lg-2 control-
label']) !!}
<div class="col-lg-10">
{!! Form::text('product', $value = null, ['class' => 'form-
control', 'placeholder' => 'product']) !!}
</div>
</div>
<!-- Submit Button -->
<div class="form-group">
<div class="col-lg-10 col-lg-offset-2">
{!! Form::submit('Submit', ['class' => 'btn btn-info '] ) !!}
</div>
</div>
</fieldset>
{!! Form::close() !!}
</div>
</div>
you can use bootsrap html form for easy and better design since they have different form-class for different design .
https://www.w3schools.com/bootstrap/bootstrap_forms.asp
if u find this helpful
The form close tag should be at the same depth as the form open tag:
without them. Is there any way to stylise the form and use it ?
{!! Form::open(['action' => 'ProductController#store', 'method' => 'POST', 'enctype' => 'multipart/form-data']) !!}
<div>
<div>
<div class="centre col-lg-4 col-lg-offset-4 ">
<h2>Product</h2>
{{Form::text('product', $value=null, ['class' => 'form-control', 'placeholder' => 'Product'])}}
</div>
</div>
</div>
<div>
<div class="section3 ">
<div class="container centre">
{{Form::submit('Submit', ['class'=>'btn btn-primary'])}}
</div>
</div>
</div>
{!! Form::close() !!}

Laravel 5 storing empty row in database?

I'm currently working on a simple blog system for my website, I'm using Laravel 5.5 for this, but I'm having a problem. Whenever I create a new article using the form I created, it is storing a empty row in my database instead of the data from the form...
This is my function to store a article:
public function newsStore()
{
$input = Request::all();
Article::create($input);
return $input;
}
According to the tutorial that I'm following should this do the trick.. This is the form that I'm currently using:
{!! Form::open(['url' => 'news']) !!}
<!-- Article Title -->
<div class="form-group row">
{!! Form::label('title', 'Title:', ['class' => 'col-2 col-form-label']) !!}
<div class="col-10">
{!! Form::text('title', null, ['class' => 'form-control']) !!}
</div>
</div>
<!-- Article Description -->
<div class="form-group row">
{!! Form::label('description', 'Description:', ['class' => 'col-2 col-form-label']) !!}
<div class="col-10">
{!! Form::text('description', null, ['class' => 'form-control']) !!}
</div>
</div>
<!-- Article Excerpt -->
<div class="form-group row">
{!! Form::label('excerpt', 'Excerpt:', ['class' => 'col-2 col-form-label']) !!}
<div class="col-10">
{!! Form::text('excerpt', null, ['class' => 'form-control']) !!}
</div>
</div>
<!-- Article Image -->
<div class="form-group row">
{!! Form::label('image', 'Image:', ['class' => 'col-2 col-form-label']) !!}
<div class="col-10">
{!! Form::file('image') !!}
</div>
</div>
<!-- Article Message -->
<div class="form-group row">
{!! Form::label('message', 'Message:', ['class' => 'col-2 col-form-label']) !!}
<div class="col-10">
{!! Form::textarea('message', null, ['class' => 'form-control', 'rows' => '7']) !!}
</div>
</div>
<!-- Article Submit -->
<div class="form-group">
{!! Form::button('Add Article', ['class' => 'btn btn-primary waves-effect waves-light float-right', 'type' => 'submit']); !!}
</div>
{!! Form::close() !!}
This is the route I'm using:
Route::post('news', 'DashboardController#newsStore'); // The articles store route
This is my model:
class Article extends Model
{
protected $fillable = [
'title',
'description',
'excerpt',
'image',
'body',
'published_at'
];
}
I have done this once before and have tried to redo it all but can't seem to figure out what is going wrong.
Thanks in advance!
To make create() work make sure you're using columns with same names. For example, if your column name is title, you need to use title instead of news-title in the form too:
{!! Form::text('title', null, ['class' => 'form-control']) !!}
As you use this Article::create(); it means all your input names should be same as table column name.
Ex:
<input name="title" ... same be same to column call news-title in respective table
If not same
Use Query Builder
DB::table('table_name')->insert(
[
'name' => $input->news-title
.....
]
);
Read More about Hyphens in column names in MySQL DB

Redirect to wrong action when validation fails after form submission in laravel 5.0

I have two form in one view when I leave the text field empty it redirects to wrong action. I am looking forwrad for the solution of this problem. Thanks in hope.
Form one works fine when I enter data in ll the input fields. /inquiry/store also contains http request validation.
View code which contains two forms.
<div class="col-lg-6">
{!! Form::open(['role' => 'form', 'url' => '/inquiry/store', 'class' => 'form-horizontal', 'method'=>'POST']) !!}
<div class='form-group'>
{!! Form::label('name', 'Student Name *', ['class' => 'control-label col-md-4']) !!}
<div class="col-md-8">
{!! Form::text('name', $student->name,['autocomplete'=>'off' , 'placeholder' => 'Student Name', 'class' => 'form-control']) !!}
</div>
</div>
<div class='form-group'>
{!! Form::label('father_name', 'Father Name *', ['class' => 'control-label col-md-4']) !!}
<div class="col-md-8">
{!! Form::text('father_name',$student->father_name, ['autocomplete'=>'off' , 'placeholder' => 'Father Name', 'class' => 'form-control']) !!}
</div>
</div>
<div class="form-group">
{!! Form::label('class', 'Class', ['class' => 'control-label col-md-4']) !!}
<div class="col-md-8">
{!! Form::text('class', $student->admission_class,['autocomplete'=>'off' , 'placeholder' => 'Class', 'class' => 'form-control']) !!}
</div>
</div>
<div class="form-group">
{!! Form::label('roll_no', 'Roll Number', ['class' => 'control-label col-md-4']) !!}
<div class="col-md-8">
{!! Form::text('roll_no', $student->roll_no,['autocomplete'=>'off' , 'placeholder' => 'Roll Number', 'class' => 'form-control']) !!}
</div>
</div>
<div class="form-group">
{!! Form::label('date', 'Date',['class' => 'control-label col-md-4']) !!}
<div class="col-md-8 date">
<div class="input-group input-append date" id="dateRangePicker">
{!! Form::input('date', 'date', null, ['autocomplete'=>'off' , 'placeholder' => 'Date of Birth', 'class'=>'form-control col-height datepicker']) !!}
<span class="input-group-addon add-on"><span class="glyphicon glyphicon-calendar"></span></span>
</div>
</div>
</div>
<div class="form-group">
{!! Form::label('teacher_name', 'Teacher Name', ['class' => 'control-label col-md-4']) !!}
<div class="col-md-8">
{!! Form::select('teacher_name', $teacher, ['autocomplete'=>'off' , 'placeholder' => 'Teacher Name', 'class' => 'form-control']) !!}
</div>
</div>
<div class="form-group">
{!! Form::label('remarks', 'Remarks', ['class' => 'control-label col-md-4']) !!}
<div class="col-md-8">
{!! Form::textarea('remarks', null, ['autocomplete'=>'off' ,
'placeholder' => 'Remarks',
'class' => 'form-control']) !!}
</div>
</div>
<div class="form-group">
<div class="col-xs-offset-3 col-md-12">
{!! Form::submit('Save', ['class' => 'btn btn-primary']) !!}
<input type="reset" class="btn btn-default" value="Reset">
</div>
</div>
{!! Form::close() !!}
second form for searching the student. When I submit the first form with out some empty field it redirect to /inquiry/search_stu. and displays MethodNotAllowedHttpException .
<form role="form" id="search_form" action="/inquiry/search_stu" method="post" class="form-inline">
<div class='form-group'>
<label class="control-label col-md-4">Search Student *</label>
<input class="form-control" type="text" name="search" placeholder="Admission No" required>
</div>
<input type="hidden" name="_token" value="{{ csrf_token() }}">
<div class='form-group'>
<input type="submit" form="search_form" class="btn btn-primary" name="submit">
<input type="reset" class="btn btn-default">
</div>
</form>
Controller function for search student.
public function search_student(Request $request)
{
$teacher = \App\Teacher::lists('name', 'name');
$adminid = $request['search'];
$student = Admission::where('admission_no',$adminid)->first();
return View::make('/inquiry/create', ['student'=> $student,'teacher' => $teacher]);
}
When I submit the store form it redirects to search student.
store function .
public function store(Requests\StoreInquiryRequest $request) {
$input = Input::all();
$inquiry = new Inquiry();
$inquiry->name = $input['name'];
$inquiry->father_name = $input['father_name'];
$inquiry->date = $input['date'];
$inquiry->class = $input['class'];
$inquiry->teacher_name = $input['teacher_name'];
$inquiry->roll_no = $input['roll_no'];
$inquiry->remarks = $input['remarks'];
try {
$inquiry->save();
return redirect()->to('inquiry')->with('message', 'success| Student details have been saved.');
} catch (Exception $ex) {
\Log::error($ex);
return redirect()->to('inquiry')->with('message', 'error| There was an error adding new student, please try again later.');
}
}
Http request for validation when wants to store data
public function rules()
{
$cid = \Route::input('id');
$isEdit = isset($cid) ? true : false;
$rules = array(
'name' => 'required|string|Max:50',
'father_name' => 'required|string|Max:50',
'class' => 'required|string|Max:50',
'date' => 'required|date|Max:50',
'teacher_name' => 'required|string|Max:50',
'remarks' => 'required',
'roll_no' => 'required',
);
return $rules;
}
You can performe as many forms on one page as you wish. As far as I see you should have proper validtion in your controller - different validation for each form.
You need to use proper Laravel Validation. Reading this will make your coding process easier and faster: https://laravel.com/docs/5.0/validation
Example of validation in controller in Laravel 5.0:
public function store(Request $request)
{
$this->validate($request, [
'title' => 'required|unique|max:255',
'body' => 'required',
]);
}
Laravel Valiation also let you perform validations with query to database qithout writing whole SQL query: https://laravel.com/docs/5.0/validation#available-validation-rules
To display errors in view use:
#if($errors->has())
#foreach ($errors->all() as $error)
<div>{{ $error }}</div>
#endforeach
#endif
Good luck!

Categories