#can directive issue - laravel - laravel

jobs.edit.blade.php can be accessed by registered users
out of them the accountant can access some checkboxes(only visible to him)
The database values are changed(Boolean) according to checkboxes submitted by the accountant.
#can ('isAccountant')
//some code with checkboxes
#endcan
The problem is, when a registered user access jobs.edit.blade.php
and submits(Checkboxes are not visible), all the Boolean values in the database becomes Zero
How can i avoid this situation
jobs.edit.blade.php
#can ('isAccountant')
<div class="row">
<div id="status">
#if ($job->entered_status == '0')
<input type="checkbox" name="entered_status" #if($job->entered_status) checked #endif> Entered <br><br>
#elseif ($job->entered_status == '1')
<input type="checkbox" name="entered_status" checked value="1" disabled> Entered <br><br>
<input type='hidden' name='entered_status' value="1">
#endif
#if ($job->sales_status == '1')
<input type="checkbox" name="despatch_status" checked value="1" disabled> All Despatches Completed <br><br>
<input type="checkbox" name="invoice_status" checked value="1" disabled> All Invoices Completed
<input type='hidden' name='despatch_status' value="1">
<input type='hidden' name='invoice_status' value="1">
#endif
#if ($job->sales_status == '0' && $job->job_status == 'Completed' && $job->despatch_status == '0')
<input type="checkbox" name="despatch_status" #if($job->despatch_status) checked #endif value="1"> All Despatches Completed <br><br>
#endif
#if ($job->sales_status == '0' && $job->job_status == 'Completed' && $job->despatch_status == '1')
<input type="checkbox" name="despatch_status" checked value="1"> All Despatches Completed <br><br>
<input type="checkbox" name="invoice_status" #if($job->invoice_status) checked #endif value="1"> All Invoices Completed <br><br>
#endif
</div>
</div>
#endcan
JobController.php
public function update(Request $request, $id)
{
$job = Job::find($id);
$job->customer_name = $request->customer_name;
$job->company_name = $request->company_name;
$job->job_type = $request->job_type;
$job->job_owner = $request->job_owner;
$job->job_status = $request->job_status;
$job->despatch_status = $request->has('despatch_status');
$job->invoice_status = $request->has('invoice_status');
if ($job->despatch_status == 1 && $job->invoice_status == 1) {
$job->sales_status = 1;
}
$job->entered_status = $request->has('entered_status');
AuthServiceProvider.php
public function boot()
{
$this->registerPolicies();
Gate::define('isAdmin', function ($user) {
return $user->user_type == 'Admin';
});
Gate::define('isDirector', function ($user) {
return $user->user_type == 'Director';
});
Gate::define('isProduction', function ($user) {
return $user->user_type == 'Production';
});
Gate::define('isAccountant', function ($user) {
return $user->user_type == 'Accountant';
});
}
Extended Error
_token
"nzY5y8EW5yupkL3d6aL9bcpZRYmlc5YTN94ee1uI"
_method
"PATCH"
id
"10725"
customer_name
""
company_name
""
job_type
"""
Item:\r\n
Qty:\r\n
Size:\r\n
Paper:\r\n
Print:\r\n
Each:\r\n
Total:
"""
job_owner
"Sam"
job_status
"Pending"
job_delivery_date
""
job_delivery_time
""
pk_pkl
"PKL"
entered_status
"on"

The reason for this is that you are using:
$job->despatch_status = $request->has('despatch_status')
$request->has('despatch_status') is true if the request has the field, and false if the request does not have the field.
The code i can recommend:
This will get the despatch_status from the request, if its true or false (or 1/0) it will get passed to $job->despatch_status.
But if despatch_status is not present in the request, the ->get() will return null, so the ?? operator will then read the value already in the database, so then it wont change.
$job->despatch_status = $request->get('despatch_status') ?? $job->despatch_status;

Based on the comments of the other answer, add this to your RouteServiceProvider:
public function boot()
{
//
parent::boot();
if (!$this->app->runningInConsole()) {
foreach (request()->all() as $key => $value) {
if (is_string($value) && $value === 'on') {
request()->offsetSet($key, true);
}
}
}
}
What this will do is scan all incoming fields, and if they are 'on' they get cast to true.
(the reason we add this to a service provider and not middleware is based on order of execution)

Related

Getting only 1 radio button to be saved instead of 2

In my page I can have multiple addresses, but the user can only select one address.
The problem I'm having is that both my radio buttons are being saved instead of just one.
So if the user has 2 addresses then only one should have selected = 1 and the other address should be selected = 0, but at the moment both address = 1.
I haven't been able to only have 1 address selected and saved as such in the database. I know that I'm passing the ID of the selected radio button through, but I was hoping to have this happen.
E.G: When the user saves their first address that is then saved as their selected address (selected = 1) and any other addresses they add after that will be (selected = 0).
If they change their mind and want their second address to be the one they use then the select it and that will then become like this, address 1 (selected = 0) and address 2 will become (selected = 1).
I hope this made sense. If it didn't please let me know.
My form
#foreach($addresses as $address)
<div class="col-lg-4">
<form id="address-radio" action="{{ route('account.post.addresses.radio', $address->id) }}" method="post">
#csrf
<div class="form-check">
<input type="radio" class="form-check-input" name="address_option" id="address_{{ $address->id }}" {!! $address->selected == '1' ? 'checked' : '' !!}>
<label for="address_{{ $address->id }}" class="form-check->label">
#if(!empty($address->complex))
{{ $address->complex }} <br>
#endif
{{ $address->address }} <br>
{{ $address->suburb }} <br>
{{ $address->city }} <br>
{{ $address->province }} <br>
{{ $address->postal_code }} <br>
</label>
</div>
</form>
</div>
#endforeach
<script>
$(document).ready(function(){
$('input[type="radio"]').on('change', function(){
$(this).closest("form").submit();
})
})
</script>
and this is my function
public function postAddressesRadio(Request $request, $id)
{
$selected = Address::findOrFail($id);
$user_id = Auth::user()->id;
$not_selected = $selected->where('id', '!=', $id)
->where('user_id', $user_id)->get();
foreach($not_selected as $selected)
{
$selected->selected = "0";
}
if($request->address_option == 'on'){
$selected->selected = '1';
}
$selected->save();
return redirect()->back()->with('success', 'Address was updated');
}
So I've managed to do it. I'm not sure if there is a better way but this works.
In my function I did
public function postAddressesRadio(Request $request, $id)
{
$address = Address::findOrFail($id);
$user_id = Auth::user()->id;
$addresses = Address::where('user_id', $user_id)->get();
foreach($addresses as $address)
{
if($address->id == $id)
{
$address->selected = "1";
}else{
$address->selected = "0";
}
$address->save();
}

Laravel-MYSQL Search through for values in database and then display them

I have a textbox that I'm trying to use to search through the database, What I'm trying to achieve is when I type in a date of birth(e.g 14/06/1996) into the textbox(searchdob) it will display the users that have this date of birth value in the database. I've tried use my AppointmentController to do the where conditions so the foreach loop is kept tidier.
Error is: Non-static method Symfony\Component\HttpFoundation\Request::get() should not be called statically, assuming $this from incompatible context
AppointmentController
function addAppointment()
{
$doctors = Doctor::all();
$search = Request::get('searchdob');
$users = User::where('role', '=', 1)
->where('dateofbirth', 'LIKE', '%'.$search.'%')
->get();
return view('appointment/addappointmentform',['users'=>$users],['doctors'=>$doctors]);
}
addappointment.blade
<form>
Insert Patients date of birth
<input
type="text"
name="searchdob"
id="searchdob"
placeholder="dd/mm/yyyy"
onkeyup="
var v = this.value;
if (v.match(/^\d{2}$/) !== null) {
this.value = v + '/';
} else if (v.match(/^\d{2}\/\d{2}$/) !== null) {
this.value = v + '/';
}"
maxlength="10"
></form>
<fieldset>
<legend>Select the Patient</legend>
#foreach($users as $user)
<div>
<label for="dirBtn{{$user->id}}">
<input id="dirBtn{{$user->id}}" type="radio" name="user" value="{{$user->id}}">
{{$user->firstname}}
</label>
</div>
#endforeach
</fieldset>
Change it to:
$search = request('searchdob');

Laravel 5: Check Image Type Validation Only If Uploaded

I'm creating a form in Laravel 5 which has a product name, product main image and secondary image. Both the image field and secondary field are optional.
<div class="form-group {{ ($errors->has('title')) ? 'has-error' : '' }}">
<label>Title *</label>
<input class="form-control validate[required]" name="title" type="text" value="{{ Input::old('title') }}">
{{ ($errors->has('title') ? $errors->first('title') : '') }}
</div>
<div class="form-group">
<label for="featured_image">Cover Image
<small>(optional)</small>
</label>
<input type="file" placeholder="" id="featured_image" name="featured_image">
<small class="description"> Maximum file size: 2 MB.</small>
{{ ($errors->has('title') ? $errors->first('featured_image') : '') }}
</div>
<div class="form-group">
<label for="gallery_images">Gallery Images
<small>(optional)</small>
</label>
<input type="file" placeholder="" id="gallery_images" name="gallery_images[]" multiple="">
<small class="description">Maximum file size: 2 MB.</small>
{{ ($errors->has('title') ? $errors->first('gallery_images') : '') }}
</div>
My validation in the request file is:
public function rules()
{
return [
'title' => 'required|min:5',
'featured_image' => 'mimes:jpeg,png,jpg|max:2048',
'gallery_images' => 'mimes:jpeg,png,jpg|max:2048'
];
}
But it always checks for the image upload whether it is uploaded or not. Which is incorrect, the image type was checked only when images were upload else not.
Thank you.
You should do these checks before upload to get best result. Below example uses the ajax form jquery plugin
$(document).ready(function() {
var options = {
target: '#output',
beforeSubmit: beforeSubmit,
uploadProgress: OnProgress,
success: afterSuccess,
resetForm: true
};
$('#my-upload-form').submit(function() {
$(this).ajaxSubmit(options);
return false;
});
});
function OnProgress(event, position, total, percentComplete) {
// Some in progress thingy
}
function afterSuccess() {
// OK msg
}
function beforeSubmit() {
if (window.File && window.FileReader && window.FileList && window.Blob) {
if( !$('#image').val()) {
return false
}
var fsize = $('#image')[0].files[0].size;
var ftype = $('#image')[0].files[0].type;
switch(ftype) {
case 'image/jpeg': case 'image/pjpeg':
break;
default:
// Input not supported
return false
}
if(fsize>10485760) {
// Input too large
return false
}
} else {
// Upgrade browser msg
return false;
}
}
function bytesToSize(bytes) {
var sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB'];
if (bytes == 0) return '0 Bytes';
var i = parseInt(Math.floor(Math.log(bytes) / Math.log(1024)));
return Math.round(bytes / Math.pow(1024, i), 2) + ' ' + sizes[i];
}

Using the Remember me feature with Sentry in Laravel 4

I'm trying to get a login form to 'remember' the user logging in and I just can't work out how to do it.
Here's my controller
public function getLogin()
{
// Return the view with the data
return View::make('users.login');
}
public function postLogin()
{
// Gather Sanitized Input
$input = array(
'email' => Binput::get('email'),
'password' => Binput::get('password'),
'rememberMe' => Binput::get('rememberMe')
);
// Set Validation Rules
$rules = array (
'email' => 'required|min:4|max:64|email',
'password' => 'required|min:6'
);
//Run input validation
$v = Validator::make($input, $rules);
if ($v->fails())
{
// Validation has failed
return Redirect::to('users/login')->withErrors($v)->withInput();
}
else
{
try
{
//Check for suspension or banned status
$user = Sentry::getUserProvider()->findByLogin($input['email']);
$throttle = Sentry::getThrottleProvider()->findByUserId($user->id);
$throttle->check();
// Set login credentials
$credentials = array(
'email' => $input['email'],
'password' => $input['password']
);
// Try to authenticate the user
$user = Sentry::authenticate($credentials, $input['rememberMe']);
Sentry::loginAndRemember($user);
}
catch (Cartalyst\Sentry\Users\UserNotFoundException $e)
{
// Sometimes a user is found, however hashed credentials do
// not match. Therefore a user technically doesn't exist
// by those credentials. Check the error message returned
// for more information.
Session::flash('error', 'Invalid username or password.' );
return Redirect::to('users/login')->withErrors($v)->withInput();
}
catch (Cartalyst\Sentry\Users\UserNotActivatedException $e)
{
echo 'User not activated.';
Session::flash('error', 'You have not yet activated this account.');
return Redirect::to('users/login')->withErrors($v)->withInput();
}
// The following is only required if throttle is enabled
catch (Cartalyst\Sentry\Throttling\UserSuspendedException $e)
{
$time = $throttle->getSuspensionTime();
Session::flash('error', "Your account has been suspended for $time minutes.");
return Redirect::to('users/login')->withErrors($v)->withInput();
}
catch (Cartalyst\Sentry\Throttling\UserBannedException $e)
{
Session::flash('error', 'You have been banned.');
return Redirect::to('users/login')->withErrors($v)->withInput();
}
return Redirect::to('/');
}
}
/**
* Logout
*/
public function getLogout()
{
Session::flush();
Sentry::logout();
return Redirect::to('/');
}
And here's my View
#extends('layouts/master')
{{-- Web site Title --}}
#section('title')
#stop
{{-- Content --}}
#section('content')
<div class="tck-well span6 offset3">
<h1>Login</h1>
<form class="" action="{{ URL::to('users/login') }}" method="post">
{{ Form::token(); }}
<div class="control-group {{ ($errors->has('email')) ? 'error' : '' }}" for="email">
<label class="control-label" for="email">E-mail</label>
<div class="controls">
<input name="email" id="email" value="{{ Request::old('email') }}" type="text" class="input-xlarge" placeholder="E-mail">
{{ ($errors->has('email') ? $errors->first('email') : '') }}
</div>
</div>
<div class="control-group {{ $errors->has('password') ? 'error' : '' }}" for="password">
<label class="control-label" for="password">Password</label>
<div class="controls">
<input name="password" value="" type="password" class="input-xlarge" placeholder="New Password">
{{ ($errors->has('password') ? $errors->first('password') : '') }}
</div>
</div>
<div class="control-group" for"rememberme">
<div class="controls">
<label class="checkbox inline">
<input type="checkbox" name="rememberMe" value="1"> Remember Me
</label>
</div>
</div>
<div class="form-actions">
<input class="button button-large button-secondary" type="submit" value="Log In">
Forgot Password?
</div>
</form>
</div>
#stop
Can someone help point me in the right direction please?
You could also use the helper method:
if( Input::get('rememberMe') ) {
$user = Sentry::authenticateAndRemember($credentials)
} else {
$user = Sentry::authenticate($credentials, false);
}
Similar to Devo's
// Try to log the user in
Sentry::authenticate(Input::only('email', 'password'), Input::get('remember-me', 0));
// For the view page
<input type="checkbox" name="remember-me" id="remember-me" value="1" /> Remember me;
Instead of,
$user = Sentry::authenticate($credentials, $input['rememberMe']);
Use,
if(!empty($input['rememberMe'])) {
$user = Sentry::authenticate($credentials, true);
} else {
$user = Sentry::authenticate($credentials, false);
}
And make sure you are getting some value in $input['rememberMe'].
From GitHub it seems setting gc_maxlifetime in php.ini (or .htaccess) is sometimes necessary as well..
session.gc_maxlifetime = 2592000
In app/config/session.php add this lines:
'lifetime' => 999999,
'expire_on_close' => false,

Laravel 4 - Input::old for radio buttons

I was wondering if some Laravel guys can help out.
I have a form in which i have 2 radio buttons, when the form submits it goes through the validator, if the validator fails it comes back to the form, populates the fields with the input and displays error messages.
I cant seem to do this for radio buttons, if one is clicked when the form is submitted and there was an error, it comes back to the form with everything filled out EXCEPT the radio button that was checked is now empty.
My radio buttons are as follows:
<input type="radio" name="genre" value="M" class="radio" id="male" />
<input type="radio" name="genre" value="F" class="radio" id="female" />
<span class="error">{{ $errors->first('genre') }}</span>
Any help would be greatly appreciated.
You can try this using Laravel's out of the box HTML radio...
Laravel Docs Form checkboxes and Radio
Using blade,
{{ Form::radio('genre', 'M', (Input::old('genre') == 'M'), array('id'=>'male', 'class'=>'radio')) }}
{{ Form::radio('genre', 'F', (Input::old('genre') == 'F'), array('id'=>'female', 'class'=>'radio')) }}
Or just php,
echo Form::radio('genre', 'M', (Input::old('genre') == 'M'), array('id'=>'male', 'class'=>'radio'));
echo Form::radio('genre', 'F', (Input::old('genre') == 'F'), array('id'=>'female', 'class'=>'radio'));
You could do this:
<input type="radio" name="genre" value="M" class="radio" id="male" <?php if(Input::old('genre')== "M") { echo 'checked="checked"'; } ?> >
<input type="radio" name="genre" value="F" class="radio" id="female" <?php if(Input::old('genre')== "F") { echo 'checked="checked"; } ?> >
The bug is known :
- https://github.com/laravel/laravel/issues/2069
- https://github.com/laravel/framework/issues/1564
You have a temporary solution in the second link.
I've just stumbled into this and I don't want to keep repeating such conditions on every form, so I've created a function on my Helper class.
Helper.php:
class Helper {
// other functions
public static function oldRadio($name, $value, $default = false) {
if(empty($name) || empty($value) || !is_bool($default))
return '';
if(null !== Input::old($name)) {
if(Input::old($name) == $value) {
return 'checked';
} else {
return '';
}
} else {
if($default) {
return 'checked';
} else {
return '';
}
}
// Or, short version:
return null !== Input::old($name) ? (Input::old($name) == $value ? 'checked' : '') : ($default ? 'checked' : '');
}
}
So, now on my forms, I just use it like this:
<label>Please select whatever you want</label>
<div class="radio-inline"><label><input type="radio" name="whatever" value="1" required {{ Helper::oldRadio('whatever', '1', true) }}> One</label></div>
<div class="radio-inline"><label><input type="radio" name="whatever" value="2" {{ Helper::oldRadio('whatever', '2') }}> Two</label></div>
<div class="radio-inline"><label><input type="radio" name="whatever" value="3" {{ Helper::oldRadio('whatever', '3') }}> Three</label></div>
Each option passes its name and value to the helper function and the previously selected one will print 'checked'. Additionally, an option can pass 'true' as the third parameter so it gets selected if there was no old input.
Using with the Bootstrap & Automatic checking
Add this code at end of file: app/start/global.php
//...
Form::macro('radio2', function($group ='group-name', $value_model = 'value-model', $label ='label-radio', $value_radio = 'value-radio', $attrs = array())
{
$item = "<div class='radio'>";
$item .= "<label>";
$item .= Form::radio($group, $value_radio, ($value_model == $value_radio) ? true : false, $attrs);
$item .= $label;
$item .= "</label>";
$item .= "</div>";
return $item;
});
In your view.php
{{ Form::radio2('status', Input::old('status'), 'Online', '1', array()) }}
{{ Form::radio2('status', Input::old('status'), 'Offline', '0', array()) }}
Final result:
I tried simply using Input::get() instead of Input::old().... and it's worked!!{{Form::radio('estado','V',(Input::get('estado')=="V"))}}
My approach is a nested shorthand if/else statement with blade syntax. This solution considers also the initial value that is set in the database.
<div class="form-check form-check-inline">
<input class="form-check-input" type="radio" name="sex" id="male" value="male"{!! ((old('sex') == 'male') ? ' checked="checked"' : ((empty(old('sex')) && $user->sex == 'male') ? ' checked="checked"' : '')) !!}/>
<label class="form-check-label" for="male">Männlich</label>
</div>
<div class="form-check form-check-inline">
<input class="form-check-input" type="radio" name="sex" id="female" value="female"{!! ((old('sex') == 'female') ? ' checked="checked"' : ((empty(old('sex')) && $user->sex == 'female') ? ' checked="checked"' : '')) !!}/>
<label class="form-check-label" for="female">Weiblich</label>
</div>
Tested with Laravel 5.7.

Resources