We know laravel has an update() method that update records through "put" http method. Everything works cool except when I try validating the new input values against a static $rules method in my User model.
Please have a look at my model
protected $guarded = ['id', 'admin'];
public static $rules = [
'name' => 'required|min:4',
'username' => 'required|min:3|alpha_num|unique:users',
'email' => 'required|email|unique:users',
'password' => 'required|alpha_num|between:5,12|confirmed',
'password_confirmation' => 'required|alpha_num|between:5,12',
'phone' => 'required|min:5|alpha_num',
'city' => 'required|min:2|alpha',
'town' => 'required|min:2|alpha_num',
'address' => 'required|min:5|alpha_num',
'blood' => 'required|min:2',
'img' => 'image|mimes:jpeg,jpg,bmp,png,gif',
'active' => 'integer',
'admin' => 'integer'
];
You see I have made email as unique to the user table. So, when I try to update the records, which are about the logged in user's profile information, if I update all the records along with the email address it works. But if I leave the email field as is, it throws the validation error saying the email address is already taken.
But I want laravel to validate only the updated fields so that users can update whatever they want or leave one or two as before.
Is there any way to accomplish that?
Please now check out the controller method that is responsible for updating the newly inputted data
//update the changes now
public function update($id) {
$user = User::find($id);
$validator = Validator::make(Input::all(), User::$rules);
$input = array_except(Input::all(), ['_method', 'password_confirmation']);
if( $validator->passes() ) {
$user->save($input);
return Redirect::to('donors/'.$user->username.'/profile')
->withMessage('Information Updated');
}
return Redirect::to('donors/'.$user->username.'/profile/edit')
->withMessage('Something went wrong! Please fix the errors.')
->withErrors($validator)
->withInput();
}
For understanding the scenario more clearly, I am adding the form from view and the route file snippet too.
Route
Route::put('donors/{id}', ['uses'=>'DonorsController#update']);
Views form
{{ Form::open(['url'=>'donors/'.$donor->id, 'method'=>'put', 'role'=>'form']) }}
<table class="table table-bordered donors-table">
<colgroup>
<col class="col-xs-1">
<col class="col-xs-7">
</colgroup>
<thead>
<tr>
<th>Type</th>
<th>Description</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td>
<span class="text-green">Name</span>
</td>
<td>
{{ Form::text('name', $donor->name) }}
<span class="text-red input-error">{{ $errors->first('name') }}</span>
</td>
</tr>
<tr>
<tr>
<td>
<span class="text-green">Email</span>
</td>
<td>
{{ Form::email('email', $donor->email) }}
<span class="text-red input-error">{{ $errors->first('email') }}</span>
</td>
</tr>
<tr class="odd">
<td>
<span class="text-green">New Password</span>
</td>
<td>
{{ Form::password('password', null) }}
<span class="text-red input-error">{{ $errors->first('password') }}</span>
</td>
</tr>
<tr class="odd">
<td>
<span class="text-green">Confirm New Password</span>
</td>
<td>
{{ Form::password('password_confirmation', null) }}
<span class="text-red input-error">{{ $errors->first('password_confirmation') }}</span>
</td>
</tr>
<tr class="odd">
<td>
<span class="text-green">Blood Group</span>
</td>
<td>
{{ Form::text('blood', $donor->blood) }}
<span class="text-red input-error">{{ $errors->first('blood') }}</span>
</td>
</tr>
<tr>
<tr class="odd">
<td>
<span class="text-green">Address</span>
</td>
<td>
{{ Form::text('address', $donor->address) }}
<span class="text-red input-error">{{ $errors->first('address') }}</span>
</td>
</tr>
<tr class="odd">
<td>
<span class="text-green">City</span>
</td>
<td>
{{ Form::text('city', $donor->city) }}
<span class="text-red input-error">{{ $errors->first('city') }}</span>
</td>
</tr>
<tr class="odd">
<td>
<span class="text-green">Town</span>
</td>
<td>
{{ Form::text('town', $donor->town) }}
<span class="text-red input-error">{{ $errors->first('town') }}</span>
</td>
</tr>
<tr class="odd">
<td>
<span class="text-green">Phone</span>
</td>
<td>
{{ Form::text('phone', $donor->phone) }}
<span class="text-red input-error">{{ $errors->first('phone') }}</span>
</td>
</tr>
<tr>
</tbody>
</table>
<p class="submit clearfix">
{{ Form::submit('Update', ['class'=>'btn btn-success pull-left']) }}
{{ link_to('donors/'.$donor->username.'/profile', 'Cancel', ['class'=>'pull-right btn btn-danger']) }}
</p>
{{ Form::close() }}
Did I miss anything please?
Let me know if I should show any other snippet of my codes.
For updating you need to change the email rule ever so slightly. If as part of the Unique rule you pass the field to check and then a value it won't complain that the value is already taken.
In your case update your controller by placing the following just before you do the validation:
User::$rules['email'] = 'required|email|unique:users,email,' . Input::get['email'];
Your validation should now pass as you have told laravel that the email should be unique but that you will accept the current email address.
Either do
$user->update($input);
or
// this is exactly what `update()` method does.
$user->fill($input)->save();
And adjust validation rules as well
'email' => 'unique:users,email_address,10'
where 10 is ID of current model instance. To achieve this, you need to parse rules in some way.
Related
Here is the page where I am getting the error. Can someone help me ? I do not understand why I am having this issue
#foreach($allData as $key => $item)
<tr>
<td> {{ $key+1}} </td>
<td>{{ $item['category']['name'] }}</td>
<td> #{{ $item['invoice']['invoice_no'] }} </td>
<td> {{ date('d-m-Y',strtotime($item['invoice']['date'])) }} </td>
<td> {{ $item->due_amount }} </td>
<td>
<i class="fas fa-edit"></i>
<i class="fa fa-eye"></i>
</td>
</tr>
#endforeach
I am trying to print the customer invoice with the category of the product he bought, the name of the product, the invoice number and the date.
Into my laravel application while I am fetching data by eager loading order by showing me the correct format of data in dd i.e. the data in DESC format, but while i am compacting data and trying to show it in blade it shows the data in ASC format...
My controller
public function index(Request $request)
{
$data = Rate::with('hospital')->orderBy('id','DESC')->get();
// dd($data );
return view('admin.rates.index',compact('data'));
}
My blade file
<table id="example1" class="table table-bordered table-striped">
<thead>
<tr>
{{-- <th>No</th> --}}
<th>Hospital</th>
<th>Contract Date</th>
<th class="text-center">Contract Length (weeks)</th>
<th>Weekly</th>
<th>Hourly</th>
<th>Others</th>
{{-- <th>Status</th> --}}
<th>Action</th>
</tr>
</thead>
<tbody>
<?php $i=0; ?>
#foreach ($data as $key => $rate)
<tr>
{{-- <td>{{++$i}}</td> --}}
<td>
{{ ucwords($rate->hospital->short_name) }}
</td>
<td>
{{date('m/d/Y',strtotime($rate->contract_date))}}
</td>
<td class="text-center">
{{ $rate->contract_duration}}
</td>
<td class="text-right">
{{ ($rate->weekly_rate != '') ? $rate->weekly_rate : '' }}
</td>
<td class="text-right">
{{ ($rate->hourly_rate != '') ? $rate->hourly_rate : '' }}
</td>
<td class="text-right">
{{ ($rate->others_rate != '') ? $rate->others_rate : '' }}
</td>
{{-- <td>
</td> --}}
<td>
#if ($rate->status=='inactive')
<i class="fas fa-times"></i>
#else
<i class="fas fa-check"></i>
#endif
<a class="btn btn-sm btn-info" href="{{ route('rate.edit',$rate->id) }}"><i class="fas fa-edit"></i></a>
<a class="btn btn-sm btn-danger" onclick="return confirm('Are you sure you want to delete')" href="{{ route('rate.destroy',$rate->id) }}"><i class="fas fa-trash"></i></a>
</td>
</tr>
#endforeach
</tbody>
</table>
There is nothing much more in it but why the data are not showing in DESC format is my question??? I am also pasting my dd image below where you can see the first data has the id 8 and the second one id 7 which is totally correct but while fetching id 7 comes first why??
When you are using datatable it tries to filter or sort the data in nearest alphabetical order if serial number is not in use I saw that you have closed tag for serial no try to open it and check the result. I am quite sure it is due to that... Just try to use "ordering": false into your datatable function..
Thank you for all the help in the past.
On my app, when i try to view a certain blade, it returns the error
"Trying to get property of non-object".
It has pointed me to the blade and the section of code where the error comes from:
#extends('layouts.app')
#section('content')
#component('dashboard.frame')
<div class="panel panel-default">
<div class="panel-heading">
Create
Activation Pins - {{ $item->name }}
</div>
{{--<div class="panel-body">--}}
{{--#if($pin_item && !$pin_item->pivot->is_paid )--}}
{{--<form action="/activation-pins/{{ $pin_item->id }}" method="post">--}}
{{--<input type="hidden" name="_method" value="PUT">--}}
{{--<input type="hidden" name="vendor_id" value="{{ $item->vendor->id }}">--}}
{{--{!! csrf_field() !!}--}}
{{--<button type="submit" class="btn btn-success">Mark As Paid</button>--}}
{{--</form>--}}
{{--#endif--}}
{{--</div>--}}
<table class="table">
<thead>
<tr>
<th>S/N</th>
<th>Name</th>
<th>Is Active</th>
<th>User</th>
</tr>
</thead>
<tbody>
#forelse($item->activation_pins as $itm)
<tr>
<td>{{ $loop->index + 1 }}</td>
<td>{{ $itm->pin }}</td>
<td>
<span class="label label-{{ $itm->is_active ? ( $itm->is_valid ? 'info' : 'danger' ) : 'warning' }}">
{{ $itm->is_active ? ( $itm->is_valid ? 'active' : 'expired' ) : 'inactive' }}
</span>
</td>
<td>
{{ $itm->user_id ? $itm->user->email : '-' }}
</td>
</tr>
#empty
<tr>
<td colspan="6">
<p class="text-center">No Activation Pins</p>
</td>
</tr>
#endforelse
</tbody>
</table>
</div>
#endcomponent
#endsection
With emphasis on:
<td>
{{ $itm->user_id ? $itm->user->email : '-' }}
</td>
Could I be missing something? Thanks.
This is what it displays now:
I'm trying to call the timeStamp but it doesn't appear in my table. Here is my code:
#foreach($cevents as $cevent)
<tr >
<td class="col-lg-2">
{{ $cevent->name_client }}
</a>
</td>
<td class="col-lg-2">
{{ $cevent->name_event }}
</td>
<td class="col-lg-4">
{{ $cevent->details }}
</td>
<td class="col-lg-3">
{{ $cevent->created_at }}
</td>
</tr>
#endforeach
Using dd() in a controller I can find it, so the value is present.
How can I fix this?
I'm having a table with input fields in every row but i want to submit all of the inputs at once, not one by one. Where should I put the submit button? Like that I'm submitting only the last row in the table.
<table class="table table-responsive table-striped" id="admin-table">
<thead>
<tr>
<th>Клас:</th>
<th>N:</th>
<th>Име:</th>
<th>Предмет:</th>
<th>Оценка:</th>
<th>Тип на оценката:</th>
</tr>
</thead>
<tbody>
#foreach($students as $student)
#foreach($class as $classes)
#foreach($sub as $subject)
<tr>
{!! Form::open(['action' => 'Educator\AccountController#markStudent', $subject, 'class' => 'form-horizontal']) !!}
<td>
{{$classes->name}}
</td>
<td>
{!! Form::text('student_id', $student->id) !!}
</td>
<td>
{{$student->full_name}}
</td>
<td>
{!! Form::text('subject_id', $subject->id) !!}
</td>
<td>
{!! Form::text('mark',null, ['class'=>'form-control col-md-2']) !!}
</td>
<td>
{!! Form::select('markType', $markType, null, ['class'=>'form-control']) !!}
</td>
</tr>
#endforeach
#endforeach
#endforeach
<div align="center">
<button type="button" class="btn btn-default">Назад</button>
{!! Form::submit('Запиши', ['class' => 'btn btn-default']) !!}
</div>
{!! Form::close() !!}
</tbody>
</table>
First off all move Form::open outside of all foreaches.
Then You have to change all you inputs for array (with $i itteration), so your loops will looks like:
{!! Form::open(['action' => 'Educator\AccountController#markStudent', $subject, 'class' => 'form-horizontal']) !!}
<table class="table table-responsive table-striped" id="admin-table">
<thead>
<tr>
<th>Клас:</th>
<th>N:</th>
<th>Име:</th>
<th>Предмет:</th>
<th>Оценка:</th>
<th>Тип на оценката:</th>
</tr>
</thead>
<tbody>
<?php $i = 0; ?>
#foreach($students as $student)
#foreach($class as $classes)
#foreach($sub as $subject)
<td>
{{$classes->name}}
</td>
<td>
{!! Form::text('entry[][student_id]', $student->id) !!}
</td>
<td>
{{$student->full_name}}
</td>
<td>
{!! Form::text('entry[][subject_id]', $subject->id) !!}
</td>
<td>
{!! Form::text('entry[][mark]',null, ['class'=>'form-control col-md-2']) !!}
</td>
<td>
{!! Form::select('entry[][markType]', $markType, null, ['class'=>'form-control']) !!}
</td>
<?php $i++; ?>
#endforeach
#endforeach
#endforeach
</tbody>
</table>
<div align="center">
<button type="button" class="btn btn-default">Назад</button>
{!! Form::submit('Запиши', ['class' => 'btn btn-default']) !!}
</div>
{!! Form::close() !!}
Move your {{ Form::open }} outside foreach just after tbody tag. It should work.