Laravel Dynamic Dependent Dropdown Menu - ajax

I would like to be able to select a vehicle type and use the price associated with that selection to add it to the base price of the services in the cards. I know there are many examples to change the options in a second drop-down menu, the classic country and state example, but I can't seem to find anything that uses the selected menu item in a way where I can add it to the service price. Here is the code I have so far:
<div class="dropdown">
<button class="btn btn-danger dropdown-toggle" type="button" id="dropdownMenuButton" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
Select vehicle type
</button>
<div class="dropdown-menu" aria-labelledby="dropdownMenuButton">
#foreach($vehicles as $vehicle)
<a class="dropdown-item" href="#">{{ $vehicle->type }}</a>
#endforeach
</div>
</div>
</div>
#foreach($services as $service)
<div class="card mt-3">
<div class="card-body">
<div class="d-flex justify-content-between">
<h4 class="card-title">{{ $service->nom }}</h4>
{{--this is where I'd like to use the selected vehicle type to simply add it to
the service price. If i do: {{ $service->price + $vehicle->price }} it chooses
the last one that went through the foreach loop --}}
<h3>{{ $service->price }}$</h3>
</div>
<p class="card-text">{{ $service->description }}</p>
</div>
</div>
#endforeach
I'm aware this is not very fancy (or good) programming but I've been stuck on this for hours. I also know I'll need a script to make it actually dynamic and most probably use form to select the vehicle type.

<script>
function updateServicePrice(vehicle) {
const vehiclePrice = Number(vehicle.dataset.price);
const services = Array.from(document.getElementsByClassName('service-price'));
services.forEach(service => {
service.innerHTML = Number(service.dataset.price) + vehiclePrice;
});
}
</script>
<div class="dropdown">
<button class="btn btn-danger dropdown-toggle" type="button" id="dropdownMenuButton" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
Select vehicle type
</button>
<div class="dropdown-menu" aria-labelledby="dropdownMenuButton">
#foreach($vehicles as $vehicle)
<a onclick="updateServicePrice(this)" data-price="{{ $vehicle->price }}" class="dropdown-item" href="#">{{ $vehicle->type }}</a>
#endforeach
</div>
</div>
</div>
#foreach($services as $service)
<div class="card mt-3">
<div class="card-body">
<div class="d-flex justify-content-between">
<h4 class="card-title">{{ $service->nom }}</h4>
{{--this is where I'd like to use the selected vehicle type to simply add it to
the service price. If i do: {{ $service->price + $vehicle->price }} it chooses
the last one that went through the foreach loop --}}
<h3 class="service-price" data-price="{{ $service->price }}">{{ $service->price }}$</h3>
</div>
<p class="card-text">{{ $service->description }}</p>
</div>
</div>
#endforeach

Related

Laravel issue in room booking

Hi all I have some issue with my code ..
The idea is that I have multi room's which I retrieve from database every room have multi booking date my idea is to show when open room page all page if the room have current reservation mark it red or future reservation marked blue I can't know how to make this happen my room.blade is:
#section('content')
<div class="row text-right ">
#foreach ($room_typ as $a )
<div class="col-12 col-lg-2">
<div class="card radius-15">
<div class="card-body text-center radius-15">
<h4 title="{{ $a->r_det }}" class="mb-0 font-weight-bold mt-0 text-white">{{ $a->r_name }}
</h4>-<h6>{{ $a->f_name }}</h6>
<span class="mt-1 mb-1 acupp"></span>
<i class="bx bx-plus"></i>
<i class="fadeIn animated bx bx-trash-alt mr-md-3"></i>
<i class="bx bx-info-square mr-md-3"></i>
{{ $rr }} // should show the count for reservation with room number
</div>
</div>
</div>
#endforeach
</div>
#endsection
the controller is :
public function allrom()
{
$room_typ = addroom::all();
foreach ($room_typ as $dd) {
$rom = $dd->r_name;
$rr = res_c::where('rno', '=', $rom)->get();
}
echo view('room', compact('room_typ', 'rr'));
}
Try this please change the checkin and checkout fields with the one in your database let me know if it work
#section('content')
<div class="row text-right ">
#foreach ($room_typ as $a )
<div class="col-12 col-lg-2">
<div class="card radius-15" style="{{ date("Y-m-d") >= date("Y-m-d",strtotime($a->r_date_checkin)) && date("Y-m-d") < date("Y-m-d",strtotime($a->r_date_checkout)) ? "background-color:red" : ( date("Y-m-d") > date("Y-m-d",strtotime($a->r_date_checkin) ) ) ? "background-color:blue" : "" }}">
<div class="card-body text-center radius-15">
<h4 title="{{ $a->r_det }}" class="mb-0 font-weight-bold mt-0 text-white">{{ $a->r_name }}
</h4>-<h6>{{ $a->f_name }}</h6>
<span class="mt-1 mb-1 acupp"></span>
<i class="bx bx-plus"></i>
<i class="fadeIn animated bx bx-trash-alt mr-md-3"></i>
<i class="bx bx-info-square mr-md-3"></i>
{{ $rr }} // should show the count for reservation with room number
</div>
</div>
</div>
#endforeach
</div>

Laravel Error: Too few arguments to function, 0 passed and exactly 1 expected

Using Laravel-5.8, I have this code:
public function manager_employee_list(Request $request)
{
$employees = HrEmployee::paginate(6);
return view('appraisal.appraisal_goals.manager_employee_list')->with('employees', $employees);
}
And it render this view: manager_employee_list
<div class="row d-flex align-items-stretch">
#if (count($employees))
#foreach($employees as $key => $employee)
<div class="col-12 col-sm-6 col-md-4 d-flex align-items-stretch">
<div class="card bg-light">
<div class="card-header text-muted border-bottom-0">
{{isset($employee->designation) ? $employee->designation->designation_name : ''}}
</div>
<div class="card-body pt-0">
<div class="row">
<div class="col-7">
<h2 class="lead"><b>Staff ID: {{$employee->employee_code}}</b></h2>
<h2 class="lead"><b>{{$employee->first_name}} {{$employee->last_name}}</b></h2>
<h6 class="lead"><b>Employee Department: </b>{{isset($employee->department) ? $employee->department->dept_name : ''}}</h6>
</div>
<div class="col-5 text-center">
#if($employee->emp_image != '')
<img src="{{ URL::to('/') }}/public/storage/employees/image/{{ $employee->emp_image }}" class="img-circle img-fluid" />
#else
<img class="profile-user-img img-fluid img-circle" src="{{asset('theme/adminlte3/dist/img/default.png')}}" alt="" class="img-circle img-fluid">
#endif
</div>
</div>
</div>
<div class="card-footer">
<div class="text-right">
<a href="{{ route('appraisal.appraisal_goals.manager_employee_goal', ['id'=>$employee->id]) }}" class="btn btn-sm btn-primary">
<i class="fas fa-user"></i> View Goal
</a>
</div>
</div>
</div>
</div>
#endforeach
#else
<h4 style="text-align:center;">No matching records found</h4>
#endif
</div>
I want to pass the parameter
['id'=>$employee->id]
from above to another controller action:
public function manager_employee_goal($id)
{
$goals = AppraisalGoal::where('employee_id', $id)->get();
return view('appraisal.appraisal_goals.manager_employee_goal')->with('goals', $goals);
}
It utilizes it here:
$goals = AppraisalGoal::where('employee_id', $id)->get();
When I clicked on
<a href="{{ route('appraisal.appraisal_goals.manager_employee_goal', ['id'=>$employee->id]) }}" class="btn btn-sm btn-primary">
<i class="fas fa-user"></i> View Goal
</a>
I got this error:
Too few arguments to function App\Http\Controllers\Appraisal\AppraisalGoalsController::manager_employee_goal(), 0 passed and exactly 1 expected
and this is underlined:
public function manager_employee_goal($id)
route/web.php
Route::get('appraisal_goals/manager_employee_list', 'Appraisal\AppraisalGoalsController#manager_employee_list')->name('appraisal.appraisal_goals.manager_employee_list');
Route::get('appraisal_goals/manager_employee_goal', 'Appraisal\AppraisalGoalsController#manager_employee_goal')->name('appraisal.appraisal_goals.manager_employee_goal');
How do I resolve it?
Thank you.
You need to change in your web.php file. add {id} in URL.
otherwise, all are fine as now your get method will be like 'appraisal_goals/manager_employee_goal/{id}'
Route::get('appraisal_goals/manager_employee_goal/{id}','Appraisal\AppraisalGoalsController#manager_employee_goal')->name('appraisal.appraisal_goals.manager_employee_goal');
I believe your route need to be updated to
Route::get('appraisal_goals/manager_employee_goal/{id}','Appraisal\AppraisalGoalsController#manager_employee_goal')->name('appraisal.appraisal_goals.manager_employee_goal');

Laravel validation on array of radio inputs

I'm developing a module that have multiple radio inputs and has numeric index,
I have 15 radio input groups and each group has 7 radio input members with the same index.
I want to check each input if empty.
HTML output
I have tried dynamic requests validation on laravel it failed to validate.
I tried to hard code the validation of 15 inputs = failed,
Then I tried to test it individually and adding next index on success and it only validates up to 12th item/index.
my valdation code
$rules = [];
foreach($this->request->get('ratingsd') as $key => $val)
{
$rules["ratings[$val]"] = 'required';
}
<div class="card mb-3 bg-info">
<ul class="list-group list-group-flush bg-light">
#foreach($items as $index=>$group)
<li class="list-inline">
<h5 class="card-header text-white bg-info">
{{$group->group_name}}
</h5>
</li>
#foreach($group->items as $items=>$item)
<li class="list-group-item">
<span class="font-weight-bold">
{{ $item->number }} {{ $item->entry }}
</span>
<div class="float-md-right form-inline mt-2">
#foreach($ratings as $rate_value=>$rate_text)
{!!Form::hidden("ratingsd[".$item->item_id."]", $item->item_id)!!}
{!!Form::radio("ratings[$item->item_id]", $rate_text, "$group->group_id*$item->item_id*$rate_value")->inline()!!}
#endforeach
{!!Form::textarea("remarks[".$item->item_id."]", '')->placeholder('Remarks')->inline()!!}
</div>
</li>
#endforeach
#endforeach
<li class="list-inline bg-info text-white">
<h5 class="card-header">
Overall Remarks
</h5>
</li>
<li class="list-inline-item p-1">
{!!Form::textarea("overall_remark", '')->placeholder('Remarks')->inline()!!}
</li>
<li class="list-inline-item text-center p-2">
<button class="btn btn-primary btn-block" type="submit">
Submit
</button>
</li>
</ul>
</div>
I expect to validate properly each "ratings" radio inputs

Laravel - Why my code isnt displaying all the collapses ?

This code need to display all "subcategory" for each "subcategories"
But the view result is the first subcategory only.
Where i missed please ?
View 1 :
#foreach($treeView as $category)
<div class="collapse multi-collapse collapse in " id="8" >
<div class="col-md-2"> {{ $category->account_name}}</div>
<div class=" float-right"><button class="btn btn-success" data-toggle="collapse" data-target="#{{ $category->id}}8" aria-expanded="false" aria-controls="{{ $category->id}}8" #click="getSubaccount(); getNumberrawaccount();">
<span class="glyphicon glyphicon-eye-open"></span></button></div>
<br>
<div class="list-group">
#if(count($category->subcategories))
#include('tree2',['subcategories'=>$category->subcategories])
#endif
</div>
#endforeach
View 2 : tree2
<div class='list-group'>
<br>
#foreach($subcategories as $subcategory)
<div class="list-group col-md-12">
<div class="collapse multi-collapse" id="{{ $category->id}}8" >
<div class="col-md-4"> {{ $subcategory->subaccount_name }}</div>
<div class=" float-right"><button class="btn btn-success" data-toggle="collapse" data-target="#{{ $subcategory->id}}" aria-expanded="false" aria-controls="collapseExample2">
<span class="glyphicon glyphicon-eye-open"></span></button></div>
</div>
</div>
#endforeach
</div>
Thank you

How do I get the second last record of a table in laravel

I want the second last record from my table and search already and
I tried this:
$news2 = News::orderBy('created_at', 'desc')->skip(1)->take(1)->get();
but nothing works.
I get this error:
Property [datum] does not exist on this collection instance. (View: C:\xampp\htdocs\j4ylara\resources\views\user\start.blade.php)
If I want the last one everything works it perfectly.
$news1 = News::all()->last();
and my view is the same
<div class="col-12 col-md-6 news">
<div class="news-content">
<div class="date"><i class="far fa-calendar-alt"></i> {{ $news1->datum }}</div>
<h4 class="news-title">{{ $news1->newstitel }}</h4>
<p class="news-text">{{ $news1->newsbeschreibung }}</p>
<a href="#" class="btn btn-primary">
mehr lesen »
</a>
</div>
</div>
</div>
<div class="row no-gutters news-wrapper">
<div class="col-12 col-md-6 news-image">
<img class="img-fluid" src="https://picsum.photos/385/370/?random">
</div>
<div class="col-12 col-md-6 news">
<div class="news-content">
<div class="date"><i class="far fa-calendar-alt"></i>{{ $news2->datum }}</div>
<h4 class="news-title">{{ $news2->newstitel }}</h4>
<p class="news-text">{{ $news2->newsbeschreibung }}</p>
<a href="#" class="btn btn-primary">
mehr lesen »
</a>
</div>
</div>
</div>
try this
$news2 = News::orderBy('created_at', 'desc')->skip(1)->take(1)->first();
this return only one result if you need more you need used foreach inside blade
The error you are receiving is from the view, you are probably calling to a "datum" property for an object and one of them does not exist:
(View: C:\xampp\htdocs\j4ylara\resources\views\user\start.blade.php)
The query looks good to me
try to use getModel() instead of get() to get query result
Try it:
$news2 = News::orderBy('created_at', 'desc')->offset(1)->take(1)->first();
get() returns collection of News. You must use first()
I GET IIIIIIIIIIIT AHHHHHHHHHH :)
$row = News::count();
$newsid = $row -2;
$news1 = News::all()->last();
$news2 = News::orderBy('created_at', 'desc')->skip($newsid)->take($newsid)->first();
return view('user/start', compact('news1', 'news2', 'newsid'));

Resources