Filling selects with appropriate data - laravel

I am having issues trying to figure something out. I pass my view an array of data which takes the following form
array:4 [▼
"data" => array:2 [▼
2015 => array:2 [▼
"english" => array:1 [▼
"chips" => array:1 [▼
0 => "img1.png"
]
]
"french" => array:1 [▼
"mussles" => array:1 [▼
0 => "img1.png"
]
]
]
2016 => array:2 [▼
"indian" => array:1 [▼
"madras" => array:1 [▼
0 => "img1.png"
]
]
"italien" => array:1 [▼
"pasta" => array:1 [▼
0 => "img1.png"
]
]
]
]
]
This stucture is obtained from scanning a directory structure, but it should be pretty consistent. The overall structure is
Year>Country>Dish>Images
I need to somehow display this data within select options. So year is pretty straight forward, I can do this
<select id="yearSelection" class="selectpicker form-control">
#foreach($fileData["data"] as $year => $country)
<option value="{{ $year }}">{{ $year }}</option>
#endforeach
</select>
Here is my problem. So the above select will display 2015 and 2016. I then have a second select input
<select id="countrySelection" class="selectpicker form-control">
<option value=""></option>
</select>
Now depending on what year they select is going to determine what this select should display. So if they select 2015, this select should display english and french.
How can I populate my select options based on previous selections?

You will need to use AJAX to accomplish this in a smooth-looking way.
Or you could refresh when your selected option changes and send the selected year (and other data) using GET parameters.
Then you can just pass your data on to the view depending on your GET parameters.
This should point you to the direction you want to go.

Related

I have a collection of objects in laravel. When I try to access it in blade it shows "Attempt to read property "year_month" on array"

^ Illuminate\Support\Collection {#1471 ▼
#items: array:9 [▼
0 => {#1490 ▼
+"year_month": "2022-01"
}
1 => {#1452 ▼
+"year_month": "2022-02"
}
2 => {#526 ▼
+"year_month": "2022-03"
}
3 => {#1453 ▼
+"year_month": "2022-04"
}
4 => {#1457 ▼
+"year_month": "2022-05"
}
5 => {#1458 ▼
+"year_month": "2022-06"
}
6 => {#1461 ▼
+"year_month": "2022-07"
}
7 => {#1463 ▼
+"year_month": "2022-08"
}
8 => {#1460 ▼
+"year_month": "2022-09"
}
]
#escapeWhenCastingToString: false
}
This is my collection. And I try to access it in Blade as
#if(!empty($monthYears))
<div class="pt-2 pb-2 carousel">
#foreach($monthYears as $monthYear)
<div wire:click="changePeriod('{{$monthYear->year_month}}')" class="text-gray-700 text-base mx-4 cursor-pointer carousel-cell #if($loop->index == count($monthYears)-2) is-clicked #endif">
{{date("m/y", strtotime($monthYear->year_month))}}
</div>
#endforeach
</div>
#endif
On the first load it loads without any issue. But when I click on any events of livewire components it shows "Attempt to read property "year_month" on array".
Here's my livewire component
public Collection $monthYears;
public function mount()
{
$this->monthYears = DB::table('table_name')->orderBy('year_month', 'asc')->groupBy('year_month')->get('year_month');
}
Livewire does not hydrate stdClass properties. They will be converted to array structure.
Note the supported types.

Laravel validate multidimensional array on dynamic fields

I am creating a dynamic form and hence inputs names are being created dynamically, But validation is not working properly.
Input Example:
<input type="text" id="information" name="page[home][Admin]user[]">
<input type="text" name="action[home][Admin]user[]">
Validation:
$validatedData = $request->validate([
'page'=>'required|array',
'page.*.user'=>'required',
]);
But it's not working
try changing input name to
<input type="text" id="information" name="page[home][Admin][user][]">
and validation rules like
[
'page'=>'required|array',
'page.*.*.user.*'=>'required',
]
So Request contain page data like
"page" => array:1 [▼
"home" => array:1 [▼
"Admin" => array:1 [▼
"user" => array:2 [▼
0 => ""
]
]
]
]

Laravel old() function not working --- flash dose not work (stoped working!!)

I'm trying to use old() in Laravel but it doesn't work.
In my controller:
dd(back()->withInput());
this is the session result
#session: Store {#364 ▼
#id: "dyNmtFJVQCQmcCob4SPYEBzWHJ6TJeM9X0mzWWu7"
#name: "laravel_session"
#attributes: array:6 [▼
"_token" => "NVJATvxuHRlGbAC1jUEIjS6gbLQQEWPIIV41fLYd"
"url" => []
"_previous" => array:1 [▼
"url" => "http://localhost:8000/units/23/rents/create"
]
"_flash" => array:2 [▼
"old" => []
"new" => array:1 [▼
0 => "_old_input"
]
]
"login_web_59ba36addc2b2f9401580f014c7f58ea4e30989d" => 1
"_old_input" => array:5 [▼
"_token" => "NVJATvxuHRlGbAC1jUEIjS6gbLQQEWPIIV41fLYd"
"contract_id" => "20"
"unit_id" => "23"
"amount_paid" => "1"
"submit" => "submit"
]
]
#handler: FileSessionHandler {#363 ▶}
#started: true
value="{{ old('amount_paid')}}" I would get empty field
value="{{ old('amount_paid','test') }}" I would get 'test' in the field
I'm using Laravel 5.6
EDIT
to clarify the above code it to debug
this is my original code
controller
return back()->withInput();
view
<div class="form-group">
<label> amount paid </label>
<input type="float" class="form-control" name="amount_paid" value="{{ old('amount_paid')}}" required>
</div>
adding the method that handle the request
public function store(Request $request)
{
// updating the amount paid
$rent = new Rent;
$rent->amount_paid = $request->amount_paid;
try {
$rent->save();
} catch (\Illuminate\Database\QueryException $e) {
//return to the form and populate it
return back()->withInput();
}
return redirect('units/'.$request->unit_id);
** update **
It appear that session->flash is not working.
I tried a new installation of laravel and it is working fine
The problem was related to session it self.
I have 2
\Illuminate\Session\Middleware\StartSession::class,
one in middleware and another in $middlewareGroups[web].
this was a result of following a tutorial in youtube months ago to implement localisation :(

Get id of collection in foreach

I'm trying to get the ID of the share, but, I'm do a foreach in links, look:
#foreach($user->getRelation('shares')->pluck('links') as $link)
{{ $link->first()->title }} //works
{{ //get the ID, of the current share }}
#endforeach
How can I do it?
dd($user->shares);
Collection {#338 ▼
#items: array:3 [▼
0 => Share {#343 ▼
#attributes: array:6 [▼
"id" => 7
"link_id" => 3
"user_id" => 1
"shared_in" => null
"content" => "testetste"
"created_at" => "2018-02-02 15:46:13"
]
#relations: array:1 [▼
"links" => Collection {#349 ▶}
]
}
1 => Share {#344 ▶}
2 => Share {#345 ▶}
]
}
You can specify id as the second parameter for pluck() method:
#foreach($user->getRelation('shares')->pluck('links', 'id') as $id => $link)
{{ $link->first()->title }}
{{ $id }}
#endforeach
In this instance, your shares? are already shared with the view in their entirety and so plucking the links and ids doesn't really save you much performance meaning this loop is unnecessarily complicated.
#foreach($user->getRelation('shares') as $share)
{{ $share->links->first()->title }}
{{ $share->id }}
#endforeach
If however, all you want to use in this view instance is the link and id, then I would recommend modifying your query in the controller to reduce the size of the overall collection being sent to your view instance.

Laravel / Vue: Validating when using v-model

I am using Laravel and Vue js, and I can not figure out how to validate this field using Laravel.
Vue-component:
<div v-for="problem in problems">
<div>
<label :for="problem.id">{{problem.title}}</label>
<input type="text" class="form-control" :id="problem.id"
v-model="problem.rating">
</div>
</div>
This returns an array
array:2 [
"problems" => array:2 [
0 => array:2 [
"id" => 33
"rating" => "12"
]
1 => array:2 [
"id" => 38
"rating" => "3"
]
]
]
Now I do not know how to validate this using Laravels validation. I have tried a lot of things like
$this->validate($request, [
'rating' => 'required|integer',
]);
But this does of course not work here. Can anyone please help me?
EDIT:
I figured it out. Needed to use
$this->validate($request, [
'problems.*.rating' => 'required|integer',
]);

Resources