Laravel 5.7 - Undefined offset: 1 - php

I'm new in Laravel and it is my first question.
I have 3 tables:
categories: id, name (at the moment 2 items)
variants: id, name
category_variant: id, category_id, variant_id; <--
Every variant has 1 or 2 categories
In the VariantController I have following code:
public function edit($id)
{
$variant = Variant::where('id', $id)->with('categories')->first();
$categories = Category::all();
return view('admin.variant.edit', compact('variant', 'categories'));
}
In the edit.blade.php I have following html:
#foreach ($categories as $key=>$category)
<div class="form-group form-float">
#if (isset($variant->categories[$key]->pivot->category_id)) <-- I think here is the problem
<input type="checkbox" id="wb" class="filled-in" name="wb" value="{{$category->id}}" {{ $category->id == $variant->categories[$key]->pivot->category_id ? 'checked' : ''}} >
<label for="wb">{{ $category->name}}</label>
#else
<input type="checkbox" id="wb" class="filled-in" name="wb" value="{{$category->id}}">
<label for="wb">{{ $category->name}}</label>
#endif
</div>
#endforeach
I want to know which category was checked in the checkbox. If the variant has all 2 categories everthing is ok but if the user has chosen only one category I get an error
Undefined offset: 1 (View: /shui/resources/views/admin/variant/edit.blade.php)
How can I solve this problem?
Thanks in advance
Dimi

You can use the power of collection:
#if($variant->categories->contains($category))
{{-- You does not need to test a second time to know if you need to "check" --}}
<input type="checkbox" id="wb" class="filled-in" name="wb" value="{{$category->id}}" checked >
<label for="wb">{{ $category->name}}</label>
#else
{{-- Do stuff --}}
#endif
Or simpler. Remove the first #if
<input type="checkbox" id="wb" class="filled-in" name="wb" value="{{$category->id}}" $variant->categories->contains($category)? 'checked' : '' >
<label for="wb">{{ $category->name}}</label>
Also in you Controller, you can simplify
$variant = Variant::where('id', $id)->with('categories')->first();
with
$variant = Variant::with('categories')->find($id);
// Or better, Laravel throws a 404 error when the id doesn't exists
$variant = Variant::with('categories')->findOrFail($id);

Related

laravel 9 form validation error has space between field name

There are space comes after every character of field name while displaying errors in blade template in laravel 9 as mentioned in uploaded image
my code is mentioned as below
CODE IN HTML IS :
<form action="{{route('saveAsset')}}" method="post">
{{#csrf_field()}}
<div class="form-group">
<label for="assetTitle" class="mt-2 mb-2">{{$assetCategory->category}} TITLE :</label>
<input type="text" class="form-control" name="assetTitle" id="assetTitle" placeholder="Enter Asset Title">
</div>
<div class="inputError">#error('assetTitle') Error: {{ $message }} #enderror </div>
<input type="hidden" name="assetCateId" id="assetCateId" value="{{$assetCategory->id}}">
#if(count($attributes) > 0)
#foreach($attributes as $attribute)
<label for="assetType-{{$attribute->attr_id}}" class="mt-4 mb-2">{{$attribute->attr_title}} : </label>
<div class="form-group">
<select class="form-select" name="{{$attribute->attr_title}}" id="attribute-{{$attribute->attr_id}}" aria-label="Default select example">
<option value="">Select {{$attribute->attr_title}}</option>
#foreach($attrValues as $attrValue)
#if ($attrValue->attr_id == $attribute->attr_id && strlen($attrValue->value) > 0 )
<option value="{{$attribute->attr_id}}-{{$attrValue->value_id}}" > {{$attrValue->value}}</option>
#endif
#endforeach
</select>
</div>
<div class="inputError"> #error("{$attribute->attr_title}") Error: {{ $message }} #enderror </div>
#endforeach
#endif
<div>
<button type="submit" class="btn btn-primary mt-3 px-4">Save</button>
</div>
</form>
CODE IN CONTROLLER IS :
$fields = $req->input();
$ValidationRules=[];
foreach($fields as $fieldName=>$fieldvalue){
if($fieldName == "assetTitle"){
$ValidationRules[$fieldName] = 'required|unique:assets,title';
}else{
$ValidationRules[$fieldName] = 'required';
}
}
$req->validate($ValidationRules);
I guess you have made some changes in resources/lang/en/validation.php file.
in this or any language you have set , there is a attribute named attributes
that you can change the the attribute name displaying in error message.
because the error message of asset field is ok I do not think it's a general error . check this file and attributes or messages key in it.
Don't know if you still need help but I just had the same issue and I think it's because you might have the input names with text uppercase, laravel automaticly adds a space for each uppercase character in the name attribute when showing the errors.
For example:
name="thisModel"
Will display as:
... this Model ...
If you have:
name="MODEL"
Will display as:
... M O D E L ...
Hope this helps someone in the future x)

How to display in the view edit-add.blade.php only some tables in Voyager?

It's my first time using blade and I'm a little confused with this. I need to show a form like what it comes with the default edit-add view in Voyager, but I need to display some inputs based on roles. I am filtering by the current user that is logged in and that is working fine, but I need to hide/display some inputs of the form based on that.
How can I accomplish this in the blade view?
Here is how Voyager take the data in the blade view:
#php
$dataTypeRows = $dataType->{(isset($dataTypeContent->id) ? 'editRows' :'addRows' )};
#endphp
#foreach($dataTypeRows as $row)
<!-- GET THE DISPLAY OPTIONS -->
#php
$options = json_decode($row->details);
$display_options = isset($options->display) ? $options->display : NULL;
#endphp
#if ($options && isset($options->formfields_custom))
#include('voyager::formfields.custom.' . $options->formfields_custom)
#else
<div class="form-group #if($row->type == 'hidden') hidden #endif #if(isset($display_options->width)){{ 'col-md-' . $display_options->width }}#else{{ '' }}#endif" #if(isset($display_options->id)){{ "id=$display_options->id" }}#endif>
{{ $row->slugify }}
<label for="name">{{ $row->display_name }}</label>
#include('voyager::multilingual.input-hidden-bread-edit-add')
#if($row->type == 'relationship')
#include('voyager::formfields.relationship')
#else
{!! app('voyager')->formField($row, $dataType, $dataTypeContent) !!}
#endif
#foreach (app('voyager')->afterFormFields($row, $dataType, $dataTypeContent) as $after)
{!! $after->handle($row, $dataType, $dataTypeContent) !!}
#endforeach
</div>
#endif
#endforeach
If my table 'example' contains: id, name, address and phone. How do I display in this view form only inputs for name and address?
Thanks in advance.
You should try this:
public function edit(Request $request, $id)
{
$user = Example::find($id);
return view(‘viewname’,compact(‘user’));
}
edit-add.blade.php
<input type = “text” name=”name” value=”#if(isset($user->name)){{ old('name', $user->name) }}#else{{old('name')}}#endif”>
<input type = “text” name=”name” value=”#if(isset($user->address)){{ old('address', $user->address) }}#else{{old('address')}}#endif”>
i cant understand what do you mean but
for showing a value in input in a view first you should send the value to view for example in controller you have :
public function test()
{
.....
return view('myView',compact('user'));
}
then in your view :
<input type="text" value"{{$user->address}}">
<input type="text" value"{{$user->first_name}}">
You can edit it from ADMIN interface without using any code
Just go to the settings menu and click BREAD

Show checkbox checked from database in laravel

I'm using Laratrust package for ACL in my application. I'm trying to edit roles (assign permissions to role) using checkbox. And want get already assigned permission checkbox state checked from database.
Code in RoleController.php
public function edit($id)
{
$role = Role::where('id', $id)->with('permissions')->first();
$permissions = Permission::all();
return view('admin.manage.roles.edit')->withRole($role)->withPermissions($permissions);
}
Below is the code what I have tried:
#foreach($permissions as $permission)
<div class="checkbox checkbox-styled">
<label>
<input type="checkbox" name="permissions[]" value="{{$permission->id}}"
{{ $role->permissions->pluck('id') == $permission->id ? 'checked' : '' }}
>
<span>{{$permission->display_name}} <em>({{$permission->description}})</em></span>
</label>
</div>
#endforeach
The code is throwing error
Object of class Illuminate\Support\Collection could not be converted to int
I had tried:
{{ $role->permissions->id == $permission->id ? 'checked' : '' }}
This throws error:
Property [id] does not exist on this collection instance
When I do {{dd($role->permissions)}}: The following output was given:
I would be very thankful if anyone could point-out mistake I'm doing here.
Your code will not work because you are trying to compare array with a string, which is impossible. you can use php in_array function to check whether your permission exist for the current permission or not
I think you are trying to check all the permission which already exist for the specific roles. correct me if i am wrong.
Try this
<input type="checkbox" name="permissions[]" value="{{$permission->id}}"
#if($role->permissions) #if(in_array($permission->id, $role->permissions->pluck('id')) checked #endif #endif>
Hope this will help :)
Easiest way to do is to check in collection using contain
#if($role->permissions->contains($permission->id)) checked=checked #endif
<input type="checkbox" name="permissions[]" value="{{$permission->id}}"
{{ #if($role->permissions->contains($permission->id)) checked=checked #endif }}
>
Another short method.
<input type="checkbox" name="permissions[]" value="{{ $permission->id }}"
{{ $role->permissions->contains($permission->id) ? 'checked' : '' }}>

How to show checkboxes as checked when values are set in the database in Laravel

I'm setting up entrust and trying to manage roles via an edit users page. I have pulled the list of available roles as well as the roles that the user is already assigned to and have them passed to the view as $roles and $assignedRoles.
The problem I'm having is getting the form to show the already assigned roles ($assignedRoles) as checked and the un-assigned roles (remainder of $roles) as not checked.
$roles looks like
[{"id":1,"name":"Admin","created_at":"2014-09-15 14:26:24","updated_at":"2014-09-15 14:26:24"},{"id":2,"name":"Pastor","created_at":"2014-09-15 14:26:34","updated_at":"2014-09-15 14:26:34"},{"id":3,"name":"Elder","created_at":"2014-09-15 14:26:43","updated_at":"2014-09-15 14:26:43"},{"id":4,"name":"Ministry Leader","created_at":"2014-09-15 14:26:55","updated_at":"2014-09-15 14:26:55"}]
and $userRoles looks like
[{"id":1,"name":"Admin","created_at":"2014-09-15 14:26:24","updated_at":"2014-09-15 14:26:24","pivot":{"user_id":1,"role_id":1}}]
I am iterating through the available roles in the view using
#foreach ($roles as $role)
{{ Form::checkbox('role[]', $role->id) }}
{{ Form::label('role', $role->name) }}<br>
#endforeach
My problem is that I do not know how I can make it so that when viewing the form it will show which roles are already set as per $userRoles (above).
First you make an array with all data
<?php
$all_data = array();
foreach($roles as $role){
$all_data[] = $role->id;
}
?>
Now you create checkbox
#foreach ($roles as $role)
{{ Form::checkbox('role[]', $role->id, in_array($role->id, $all_data)) }}
{{ Form::label('role', $role->name) }}<br>
#endforeach
In Controller
$user = User::find($id);
$allRoles = Role::all();
$assignedRoles = $user->roles->pluck('id')->toArray();
In View
<div class="form-group row" id="assign_hostel_fees">
<label for="inputEmail3" class="col-sm-3 control-label">Select Roles</label>
<div class="col-sm-9">
#foreach($allRoles as $role)
<div class="form-check">
<label class="form-check-label">
<input class="form-check-input" type="checkbox" name="roles[]" value="{{$role->id}}" {{in_array($role->id,$assignedRoles)?'checked':''}}>
{{$role->name}}
</label>
</div>
#endforeach
</div>
</div>
this might help you -
{{ Form::checkbox('agree', 1, true) }}
will generate -
<input checked="checked" name="agree" type="checkbox" value="1">
the third parameter defines if it will be checked or not. depending on the data(check the role id of $userRoles with $roles) generate a variable with true or false value.

Laravel selectbox selected attribute with pivot column

I have a bit of a problem what i am stuck with.
I have a ManyToMany relation storing body measurements in my database and these are serialized arrays,
My measurements table
id | name | value
1 | Height | a:61:{i:0;s:6:"150 cm";i:1;s:6:"151 cm";i:2;s:6:"152 cm";i:3;s:6:"153 cm";i:4;s:6:"154 cm";i:5;s:6:"155 cm";i:6;s:6:"156 cm";i:7;s:6:"157 cm";i:8;s:6:"158 cm";i:9;s:6:"159 cm";i:10;s:6:"160 cm";i:11;s:6:"161 cm";i:12;s:6:"162 cm";i:13;s:6:"163 cm";i:14;s:6:"164 cm";i:15;s:6:"165 cm";i:16;s:6:"166 cm";i:17;s:6:"167 cm";i:18;s:6:"168 cm";i:19;s:6:"169 cm";i:20;s:6:"170 cm";i:21;s:6:"171 cm";i:22;s:6:"172 cm";i:23;s:6:"173 cm";i:24;s:6:"174 cm";i:25;s:6:"175 cm";i:26;s:6:"176 cm";i:27;s:6:"177 cm";i:28;s:6:"178 cm";i:29;s:6:"179 cm";i:30;s:6:"180 cm";i:31;s:6:"181 cm";i:32;s:6:"182 cm";i:33;s:6:"183 cm";i:34;s:6:"184 cm";i:35;s:6:"185 cm";i:36;s:6:"186 cm";i:37;s:6:"187 cm";i:38;s:6:"188 cm";i:39;s:6:"189 cm";i:40;s:6:"190 cm";i:41;s:6:"191 cm";i:42;s:6:"192 cm";i:43;s:6:"193 cm";i:44;s:6:"194 cm";i:45;s:6:"195 cm";i:46;s:6:"196 cm";i:47;s:6:"197 cm";i:48;s:6:"198 cm";i:49;s:6:"199 cm";i:50;s:6:"200 cm";i:51;s:6:"201 cm";i:52;s:6:"202 cm";i:53;s:6:"203 cm";i:54;s:6:"204 cm";i:55;s:6:"205 cm";i:56;s:6:"206 cm";i:57;s:6:"207 cm";i:58;s:6:"208 cm";i:59;s:6:"209 cm";i:60;s:6:"210 cm";}
2 | Weight | a:76:{i:0;s:5:"35 kg";i:1;s:5:"36 kg";i:2;s:5:"37 kg";i:3;s:5:"38 kg";i:4;s:5:"39 kg";i:5;s:5:"40 kg";i:6;s:5:"41 kg";i:7;s:5:"42 kg";i:8;s:5:"43 kg";i:9;s:5:"44 kg";i:10;s:5:"45 kg";i:11;s:5:"46 kg";i:12;s:5:"47 kg";i:13;s:5:"48 kg";i:14;s:5:"49 kg";i:15;s:5:"50 kg";i:16;s:5:"51 kg";i:17;s:5:"52 kg";i:18;s:5:"53 kg";i:19;s:5:"54 kg";i:20;s:5:"55 kg";i:21;s:5:"56 kg";i:22;s:5:"57 kg";i:23;s:5:"58 kg";i:24;s:5:"59 kg";i:25;s:5:"60 kg";i:26;s:5:"61 kg";i:27;s:5:"62 kg";i:28;s:5:"63 kg";i:29;s:5:"64 kg";i:30;s:5:"65 kg";i:31;s:5:"66 kg";i:32;s:5:"67 kg";i:33;s:5:"68 kg";i:34;s:5:"69 kg";i:35;s:5:"70 kg";i:36;s:5:"71 kg";i:37;s:5:"72 kg";i:38;s:5:"73 kg";i:39;s:5:"74 kg";i:40;s:5:"75 kg";i:41;s:5:"76 kg";i:42;s:5:"77 kg";i:43;s:5:"78 kg";i:44;s:5:"79 kg";i:45;s:5:"80 kg";i:46;s:5:"81 kg";i:47;s:5:"82 kg";i:48;s:5:"83 kg";i:49;s:5:"84 kg";i:50;s:5:"85 kg";i:51;s:5:"86 kg";i:52;s:5:"87 kg";i:53;s:5:"88 kg";i:54;s:5:"89 kg";i:55;s:5:"90 kg";i:56;s:5:"91 kg";i:57;s:5:"92 kg";i:58;s:5:"93 kg";i:59;s:5:"94 kg";i:60;s:5:"95 kg";i:61;s:5:"96 kg";i:62;s:5:"97 kg";i:63;s:5:"98 kg";i:64;s:5:"99 kg";i:65;s:6:"100 kg";i:66;s:6:"101 kg";i:67;s:6:"102 kg";i:68;s:6:"103 kg";i:69;s:6:"104 kg";i:70;s:6:"105 kg";i:71;s:6:"106 kg";i:72;s:6:"107 kg";i:73;s:6:"108 kg";i:74;s:6:"109 kg";i:75;s:6:"110 kg";}
Relation table users_measurements
user_id | measurement_id | value
1 | 1 | 160 cm
1 | 2 | 50 kg
measurements controller
public function measurements()
{
$user = Auth::user();
$measurements = Measurement::all();
$this->layout->title = "Measurements";
$this->layout->content = View::make('user::settings/measurements')
->with('user', $user)
->with('measurements', $measurements);
}
And my view, i loop through the results to generate a form
View
{{ Form::open(array('id' => 'ajax-', 'class' => 'ui fluid form segment')) }}
<div class="two fields">
#foreach($measurements as $measurement)
<div class="field">
<label for="{{ $measurement->id }}">{{ $measurement->name }}</label>
<select name="{{ $measurement->id }}" id="{{ $measurement->id }}" class="form-select">
<option value="">{{ $measurement->name }} kiválasztása</option>
#foreach(unserialize($measurement->value) as $value)
<option value="{{ $value }}" >{{ $value }}</option>
#endforeach
</select>
</div>
#endforeach
</div>
{{ Form::submit('Módosítások mentése', array('class' => 'ui tiny orange button')) }}
{{ Form::close() }}
So i am really stuck how to solve to add the actual selected attribute to my selectbox
I tried a few things.
In my controller i store the logged in user in the $user variable.
I tried
$user-measurement->lists('value');
This gave back the following result
array(2) { [0]=> string(1095) "a:61:{i:0;s:6:"150 cm";i:1;s:6:"151 cm";i:2;s:6:"152 cm";i:3;s:6:"153 cm";i:4;s:6:"154 cm";i:5;s:6:"155 cm";i:6;s:6:"156 cm";i:7;s:6:"157 cm";i:8;s:6:"158 cm";i:9;s:6:"159 cm";i:10;s:6:"160 cm";i:11;s:6:"161 cm";i:12;s:6:"162 cm";i:13;s:6:"163 cm";i:14;s:6:"164 cm";i:15;s:6:"165 cm";i:16;s:6:"166 cm";i:17;s:6:"167 cm";i:18;s:6:"168 cm";i:19;s:6:"169 cm";i:20;s:6:"170 cm";i:21;s:6:"171 cm";i:22;s:6:"172 cm";i:23;s:6:"173 cm";i:24;s:6:"174 cm";i:25;s:6:"175 cm";i:26;s:6:"176 cm";i:27;s:6:"177 cm";i:28;s:6:"178 cm";i:29;s:6:"179 cm";i:30;s:6:"180 cm";i:31;s:6:"181 cm";i:32;s:6:"182 cm";i:33;s:6:"183 cm";i:34;s:6:"184 cm";i:35;s:6:"185 cm";i:36;s:6:"186 cm";i:37;s:6:"187 cm";i:38;s:6:"188 cm";i:39;s:6:"189 cm";i:40;s:6:"190 cm";i:41;s:6:"191 cm";i:42;s:6:"192 cm";i:43;s:6:"193 cm";i:44;s:6:"194 cm";i:45;s:6:"195 cm";i:46;s:6:"196 cm";i:47;s:6:"197 cm";i:48;s:6:"198 cm";i:49;s:6:"199 cm";i:50;s:6:"200 cm";i:51;s:6:"201 cm";i:52;s:6:"202 cm";i:53;s:6:"203 cm";i:54;s:6:"204 cm";i:55;s:6:"205 cm";i:56;s:6:"206 cm";i:57;s:6:"207 cm";i:58;s:6:"208 cm";i:59;s:6:"209 cm";i:60;s:6:"210 cm";}" [1]=> string(1300) "a:76:{i:0;s:5:"35 kg";i:1;s:5:"36 kg";i:2;s:5:"37 kg";i:3;s:5:"38 kg";i:4;s:5:"39 kg";i:5;s:5:"40 kg";i:6;s:5:"41 kg";i:7;s:5:"42 kg";i:8;s:5:"43 kg";i:9;s:5:"44 kg";i:10;s:5:"45 kg";i:11;s:5:"46 kg";i:12;s:5:"47 kg";i:13;s:5:"48 kg";i:14;s:5:"49 kg";i:15;s:5:"50 kg";i:16;s:5:"51 kg";i:17;s:5:"52 kg";i:18;s:5:"53 kg";i:19;s:5:"54 kg";i:20;s:5:"55 kg";i:21;s:5:"56 kg";i:22;s:5:"57 kg";i:23;s:5:"58 kg";i:24;s:5:"59 kg";i:25;s:5:"60 kg";i:26;s:5:"61 kg";i:27;s:5:"62 kg";i:28;s:5:"63 kg";i:29;s:5:"64 kg";i:30;s:5:"65 kg";i:31;s:5:"66 kg";i:32;s:5:"67 kg";i:33;s:5:"68 kg";i:34;s:5:"69 kg";i:35;s:5:"70 kg";i:36;s:5:"71 kg";i:37;s:5:"72 kg";i:38;s:5:"73 kg";i:39;s:5:"74 kg";i:40;s:5:"75 kg";i:41;s:5:"76 kg";i:42;s:5:"77 kg";i:43;s:5:"78 kg";i:44;s:5:"79 kg";i:45;s:5:"80 kg";i:46;s:5:"81 kg";i:47;s:5:"82 kg";i:48;s:5:"83 kg";i:49;s:5:"84 kg";i:50;s:5:"85 kg";i:51;s:5:"86 kg";i:52;s:5:"87 kg";i:53;s:5:"88 kg";i:54;s:5:"89 kg";i:55;s:5:"90 kg";i:56;s:5:"91 kg";i:57;s:5:"92 kg";i:58;s:5:"93 kg";i:59;s:5:"94 kg";i:60;s:5:"95 kg";i:61;s:5:"96 kg";i:62;s:5:"97 kg";i:63;s:5:"98 kg";i:64;s:5:"99 kg";i:65;s:6:"100 kg";i:66;s:6:"101 kg";i:67;s:6:"102 kg";i:68;s:6:"103 kg";i:69;s:6:"104 kg";i:70;s:6:"105 kg";i:71;s:6:"106 kg";i:72;s:6:"107 kg";i:73;s:6:"108 kg";i:74;s:6:"109 kg";i:75;s:6:"110 kg";}" }
I tried so many methods to process this data to return the actual users pivot column values, but no luck
If anybody could help me out would really make my day, or any hint please
Thank you
{{ Form::select($measurement->id, unserialize($measurement->value), $user->value, array("id" => $measurement->id, "class" => "form-select") ) }}
Try this.
Okay if anybody else needs help with this i wrote a function in my user model
public function selectedMeasurement()
{
foreach ($this->measurement as $measurement)
{
$selected[] = $measurement->pivot->value;
}
if (!empty($selected))
{
return $selected;
}
else
{
return array();
}
}
And in my view
{{ Form::open(array('id' => 'ajax-', 'class' => 'ui fluid form segment')) }}
<div class="two fields">
#foreach($measurements as $measurement)
<div class="field">
<label for="{{ $measurement->id }}">{{ $measurement->name }}</label>
<select name="{{ $measurement->id }}" id="{{ $measurement->id }}" class="form-select">
<option value="">{{ $measurement->name }} kiválasztása</option>
#foreach(unserialize($measurement->value) as $value)
<option value="{{ $value }}" {{ in_array($value, $user->selectedMeasurement()) ? 'selected="selected"' : "" }} >{{ $value }}</option>
#endforeach
</select>
</div>
#endforeach
</div>
{{ Form::submit('Módosítások mentése', array('class' => 'ui tiny orange button')) }}
{{ Form::close() }}
I had the same problem and that is how I solved it.
I added this in my controller, in the edit method and passed it into the view:
$selectedTags = $product->tags()->pluck('tag_id')->toArray();
And in the view file I added it like this:
<option value="{{$row->id}}"{!! in_array($row->id, $selectedTags) ? ' selected="selected"' : '' !!}>{{$row->name}}</option>

Categories