Laravel do check selected items from array on html select as multiple feature - laravel

After get all categories and user selected categories for selected post with this result:
$content_categories = ContentCategories::all()->pluck('title', 'id');
Collection {#209 ▼
#items: array:3 [▼
1 => "laravel"
2 => "nodejs"
3 => "php"
]
}
$selected_categories = $manage_content->categories()->get()->pluck('title', 'id');
Collection {#223 ▼
#items: array:2 [▼
1 => "laravel"
2 => "php"
]
}
I'm trying to implement this result on html select and do check selected items when we have in $selected_categories, this code only show $content_categories and can not be check items with $selected_categories by default
{{
Form::select('categories[]',
$content_categories,
$selected_categories,
array('multiple'=>'multiple'))
}}
result:
<select class="multiselect-success" multiple="multiple" name="categories[]">
<option value="1">laravel</option>
<option value="2">nodejs</option>
<option value="3">php</option>
</select>

Get IDs of selected categories without titles:
$selected_categories = $manage_content->categories()->pluck('id');
Or use keys() to get keys only:
Form::select('categories[]', $content_categories, $selected_categories->keys(), array('multiple'=>'multiple'))

Related

Laravel: How can I use Group By in laravel blade?

I am trying to get all slots, then group them by room. Within my view, I want to have a row of the room:
<tr>
<td>Room ID 1</td>
<td>Slot ID 1</td>
<td>Slot ID 2</td>
<td>Slot ID 3</td>
.................
</tr>
<tr>
<td>Room ID 2</td>
<td>Slot ID 1</td>
<td>Slot ID 2</td>
<td>Slot ID 3</td>
.................
</tr>
I believe I am close, but not sure of the "Laravel way". The data looks good - as in, the query is getting/grouping correctly, I think my struggle is understanding how to loop through the collection.
MyController.php
public function routineViewDayWise()
{
$sunday_routines = Routine::where('day_id', 1)->orderBy('room_id')->get()->groupBy('room_id');
return view('day_wise_routine', compact('sunday_routines'));
}
In my view I can see I am getting this:
MyView.blade.php
#items: array:4 [▼
2 => Illuminate\Database\Eloquent\Collection {#1323 ▼
#items: array:2 [▼
0 => App\Models\Routine {#1339 ▶}
1 => App\Models\Routine {#1340 ▶}
]
}
4 => Illuminate\Database\Eloquent\Collection {#1322 ▶}
6 => Illuminate\Database\Eloquent\Collection {#1336 ▶}
8 => Illuminate\Database\Eloquent\Collection {#1335 ▶}]
SOLUTION
Here is what my view looks like to get the output I wanted.
MyView.blade.php
#foreach($sunday_routines as $room_id => $routines)
<tr>
<td>{{ $room_id }}</td>
#foreach($routines as $routine)
<td>{{ $routine->slot_id }}</td>
#endforeach
</tr>#endforeach
Hi friend please update the following as put groupBy() before get()
public function routineViewDayWise()
{
$sunday_routines = Routine::where('day_id', 1)->orderBy('room_id')->groupBy('room_id')->get();
return view('day_wise_routine', compact('sunday_routines'));
}

Array to string conversion error when multiple file upload in Laravel 5.8.38

I have an Array to string conversion error, when trying to realize multiple upload files function in Laravel 5.8.38
Cant find any decision about it
In blade form I have simple thing:
<form class="form-horizontal" action="{{route('admin.estates.store')}}" method="post" enctype="multipart/form-data">
{{ csrf_field() }}
<label for="estate_image" class="mt-4">Images</label>
<input type="file" name="estate_image[]" multiple>
<input class="btn btn-primary" type="submit" value="Сохранить">
<input type="hidden" name="created_by" value="{{Auth::id()}}">
</form>
In store function I have:
The function creates an estate(one property). If user adds some images for it, we adds this images in local path and adds them in database
if I comment $estate = Estate::create($request->all()); it works fine
but in this case estate doesnt adds in database
public function store(Request $request)
{
$estate = Estate::create($request->all());
if($request->hasFile('estate_image')) {
foreach ($request->file('estate_image') as $image) {
// do some image resize and store it on local path
$filename = time() . '.' . $image->getClientOriginalExtension();
$location = public_path('images\\' . $filename);
Image::make($image)->resize(800, 400)->save($location);
// add image info in database
$estateimage = new EstateImages();
$estateimage->image_path = $location;
$estateimage->image_alt = 'testalt';
$estateimage->save();
}
}
}
Am array I have from input
array:5 [▼
"name" => array:2 [▼
0 => "image1.jpg"
1 => "image2.jpg"
]
"type" => array:2 [▼
0 => "image/jpeg"
1 => "image/jpeg"
]
"tmp_name" => array:2 [▼
0 => "C:\OSPanel\userdata\php_upload\phpCB51.tmp"
1 => "C:\OSPanel\userdata\php_upload\phpCB52.tmp"
]
"error" => array:2 [▼
0 => 0
1 => 0
]
"size" => array:2 [▼
0 => 164808
1 => 58217
]
]
As understand, foreach doesnt start, but dont understand why (tried to delete all code in foreach, and leave there just simple echo 'Hello!'; , have the same error.
Saw the same problems in StackOverflow, but any of it helped me...
The problem was in first line
$estate = Estate::create($request->all());
So, from blade I receive a name="estate_image[] data which is an array, and Laravel tried to adds an array in database cell. In database cell cold be added just string value and through that, I had that error.
Solve it, by deleting this column from database and deleting this from $fillable variable in main model.
Quite stupid and easy thing from me, but hope, this answer helps someone. :-)

Laravel Validating Values in Array in List of Values

This must be a simple question, but unable to find what I am doing wrong. I have a form (shown below) with a multi-select input. It gets sent to the validator as an array. I want to make sure the values selected in the client-side are in the pre-defined list of values.
<form>
<select multiple name="names[]">
<option value="Jerry">Jerry</option>
<option value="Susan">Susan</option>
<option value="Tim">Tim</option>
</select>
</form>
public function postNames(Request $request)
{
$this->validate($request, [
'names.*' => 'in:Jerry,Susan,Tim',
]);
}
When I open the dev tools in the browser and change the value of them and then submit the form, the validator lets it pass even though the name in the request is not in the list as defined in the validator.
Thank you.
UPDATE:
When I dump the request it looks like this.
+request: Symfony\Component\HttpFoundation\ParameterBag {#52 ▼
#parameters: array:1 [▼
"names" => array:1 [▼
0 => "Robert"
]
]
}

Select input with 2 empty option values

In my laravel 5.7/mysql 5 app I have boolean is_quiz field in my vote Table and in model I define:
protected $casts = [
'is_quiz' => 'boolean',
];
...
And array with possible values/keys for using of this fields
private static $voteIsQuizLabelValueArray = Array(1 => 'Is Quiz', 0 => 'Is Not Quiz');
in control I add empty value for empty selector:
$viewParamsArray['voteIsQuizValueArray'] = $this->SetArrayHeader(['' => ' -Select Is Quiz- '], Vote::getVoteIsQuizValueArray(false));
and this array has values:
$viewParamsArray['voteIsQuizValueArray']::Array
(
[] => -Select Is Quiz-
[1] => Is Quiz
[0] => Is Not Quiz
)
In my form this array as :
{{ Form::select('is_quiz', $voteIsQuizValueArray, isset($vote->is_quiz) ? $vote->is_quiz : '', [ "id"=>"is_quiz", "class"=>"form-control editable_field select_input " ] ) }}
and in rendered html-source I see 2 options selected :
<select id="is_quiz" class="form-control editable_field select_input valid" name="is_quiz" aria-invalid="false" aria-describedby="is_quiz-error"><option value="" selected="selected"> -Select Is Quiz- </option><option value="1">Is Quiz</option><option value="0" selected="">Is Not Quiz</option></select>
and validator.w3.org raised error here.
I see what is the reson of the syntax error, but I do not know is there is easy way to fix it ?
Thanks!
Form builder is deprecated since Laravel 5
Why are Form and HTML helpers deprecated in Laravel 5.x?
As one of options you can try use Laravel Collective
https://packagist.org/packages/laravelcollective/html
But IMO the best decision is to build select via blade:
<select id="is_quiz"
class="form-control editable_field select_input valid"
name="is_quiz"
aria-invalid="false"
aria-describedby="is_quiz-error"
>
#foreach ($voteIsQuizValueArray as $k => $v)
<option
value="{{ $k }}"
#if( $k === old('is_quiz', '') ) selected="selected" #endif
>{{ $v }}</option>
#endforeach
</select>

Laravel date before_equal is allowing all dates through

$DOB = $request->input('DOB');
$allowed = Carbon::now()->subYears(26)->format('Y-m-d');
$validatedData = $request->validate(
['DOB' => 'required|date|before_or_equal:'.$allowed],
);
<input type="date" name="DOB" class="form-control" required>
I am having a hard time validating dates in Laravel.
If I enter 01/01/2018 in the form, the validation does not catch it. Below it what is returned if I dd() the variables.
dd([$DOB, $allowed]);
array:2 [▼
0 => "2018-01-01"
1 => "1992-04-26"
]
No idea where I am going wrong.

Resources