Creating a dependent drop down using laravel livewire - php

I am trying to use laravel livewire to create a dynamic dependent drop down list.
So I have created a livewire component named assignment.
assignment.blade.php
<div>
<div class="row mb-4">
<label for="category" class="col-md-4 col-form-label text-md-end">Category</label>
<div class="col-md-6">
<select name="category" id="category" class="form-select col-md-6">
<option value="null">Select category</option>
#foreach ($categories as $item)
<option value="{{$item->id}}">{{$item->name}}</option>
#endforeach
</select>
</div>
</div>
<div class="row pt-4">
<button class="btn btn-warning text-white" wire:loading.attr="disable">Add New Product</button>
<div wire:loading>
Hold on...
</div>
</div>
</div>
assignment.php
<?php
namespace App\Http\Livewire;
use Livewire\Component;
use App\Models\Category;
class Assignment extends Component
{
public function render()
{
return view('livewire.assignment', [
'Category' => Category::all()
]);
}
}
The livewire component is embedded on the create.blade.php. Here is a snippet
</div>
<livewire:assignment />
<div class="row mb-4">
<label for="subcategory" class="col-md-4 col-form-label text-md-end">Sub Category</label>
<div class="col-md-6">
<select name="subcategory" id="subcategory" class="form-select col-md-6">
<option value="null">Select subcategory</option>
<option value="wine">Wine</option>
<option value="whisky">Whisky</option>
Here is my productscontroller function that renders the view
public function create()
{
return view('products.create');
}
routes
Route::get('/explore', [App\Http\Controllers\ConnectsController::class, 'index']);
Route::get('/p/create', [App\Http\Controllers\ProductsController::class, 'create']);
Route::post('/p', [App\Http\Controllers\ProductsController::class, 'store']);
I am getting the following error: syntax error, unexpected end of file.
What could be the issue?

There is a typo in assignment.blade.php. #enforeach should be #endforeach. I had to post this as answer as could not post this as comment.

Related

Laravel 8 throws undefined offset how to handle it?

here I have form to retrieve data from database using date and time issue is iam able to retrive data succesfuly most times but some time i get this error
here is the code
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use App\PricingDetail;
use App\AccessoryCharge;
use App\Discount;
use App\MonthlyCourtCharge;
class PricingDetailController extends Controller
{
public function index(Request $request){
$op_id=$request->session()->get('op_id');
$inf_id=$request->session()->get('inf_id');
if(is_null($inf_id)){
return redirect('/infrastructure_details');
}else if(is_null($op_id)){
return redirect('/operational_details');
}else{
$timings=DB::table('operational_details')
->select('opening_time','closing_time')
->where('infrastructure_details_id',$inf_id)
->first();
$pricing_details=[];
$pricing_details=DB::table('pricing_details')
->where('infrastructure_details_id','=',$inf_id)
->get();
$accessory_charges=[];
$accessory_charges=DB::table('accessory_charges')
->where('infrastructure_details_id','=',$inf_id)
->get();
$discounts=[];
$discounts=DB::table('discounts')
->where('infrastructure_details_id','=',$inf_id)
->get();
$monthly_charges=[];
$monthly_charges=DB::table('monthly_court_charges')
->where('infrastructure_details_id','=',$inf_id)
->get();
if(($pricing_details->count())>0){
$request->session()->put('price_id',$pricing_details[0]->id);
}
return view('pricing_details',['inf_id'=>$inf_id,'op_id'=>$op_id,'timings'=>$timings,'pricing_details'=>$pricing_details,'acc_charges'=>$accessory_charges,'discounts'=>$discounts,'monthly_charges'=>$monthly_charges]);
}
//return view('pricing_details',['timings'=>$timings]);
//return view('home',['court_types'=>$court_types]);
}
public function create(Request $request){
$diff=$request->input('diff');
$no_accessories=$request->input('no_accessories');
$no_discounts=$request->input('no_discounts');
$no_monthly_charges=$request->input('no_monthly_charges');
for($i=0;$i<$diff;$i++){
$pricing_details=new PricingDetail;
$pricing_details->timings=$request->input('time'.$i);
$pricing_details->price=$request->input('price'.$i);
$pricing_details->discount=$request->input('discount'.$i);
$pricing_details->discounted_price=$request->input('discounted_price'.$i);
$pricing_details->disc_from_date=$request->input('disc_from_date'.$i);
$pricing_details->disc_to_date=$request->input('disc_to_date'.$i);
$pricing_details->infrastructure_details_id=$request->input('inf_id');
$pricing_details->operational_details_id=$request->input('op_id');
$pricing_details->day_types_id=$request->input('day_type'.$i);
$pricing_details->save();
}
for($i=1;$i<=$no_accessories;$i++){
$accessory_charge=new AccessoryCharge;
$accessory_charge->item=ucfirst($request->input('item'.$i));
$accessory_charge->brand=ucfirst($request->input('brand'.$i));
$accessory_charge->price=$request->input('acc_price'.$i);
$accessory_charge->quantity=$request->input('quantity'.$i);
$accessory_charge->return_type=$request->input('return_type'.$i);
$accessory_charge->infrastructure_details_id=$request->input('inf_id');
$accessory_charge->operational_details_id=$request->input('op_id');
$accessory_charge->save();
}
for($i=1;$i<=$no_monthly_charges;$i++){
$monthly_charge=new MonthlyCourtCharge;
$monthly_charge->infrastructure_details_id=$request->input('inf_id');
$no_days=0;
$scheme_type=$request->input('scheme_type'.$i);
if($scheme_type=="scheme1"){
$no_days=22;
}else{
$no_days=31;
}
$monthly_charge->scheme_type=$scheme_type;
$monthly_charge->no_days=$no_days;
$monthly_charge->hour_type=$request->input('hour_type'.$i);
$monthly_charge->from_time=date('H:i',strtotime($request->input('from_time'.$i)));
$monthly_charge->to_time=date('H:i',strtotime($request->input('to_time'.$i)));
$monthly_charge->price=$request->input('price'.$i);
$monthly_charge->save();
}
$id=$pricing_details->id;
$request->session()->put('price_id', $id);
return redirect('/facility_details');
}
public function update(Request $request){
$diff=$request->input('diff');
$no_accessories=$request->input('no_accessories');
//$no_discounts=$request->input('no_discounts');
$no_monthly_charges=$request->input('no_monthly_charges');
for($i=0;$i<$diff;$i++){
$pr_id=$request->input('pricing_id'.$i);
$pricing_details=PricingDetail::find($pr_id);
$pricing_details->timings=$request->input('time'.$i);
$pricing_details->price=$request->input('price'.$i);
$pricing_details->discount=$request->input('discount'.$i);
$pricing_details->discounted_price=$request->input('discounted_price'.$i);
$pricing_details->disc_from_date=$request->input('disc_from_date'.$i);
$pricing_details->disc_to_date=$request->input('disc_to_date'.$i);
$pricing_details->infrastructure_details_id=$request->input('inf_id');
$pricing_details->operational_details_id=$request->input('op_id');
$pricing_details->day_types_id=$request->input('day_type'.$i);
$pricing_details->save();
}
for($i=1;$i<=$no_accessories;$i++){
$acc_id=$request->input('acc_id'.$i);
$accessory_charge=AccessoryCharge::find($acc_id);
$accessory_charge->item=ucfirst($request->input('item'.$i));
$accessory_charge->brand=ucfirst($request->input('brand'.$i));
$accessory_charge->price=$request->input('acc_price'.$i);
$accessory_charge->quantity=$request->input('quantity'.$i);
$accessory_charge->return_type=$request->input('return_type'.$i);
$accessory_charge->infrastructure_details_id=$request->input('inf_id');
$accessory_charge->operational_details_id=$request->input('op_id');
$accessory_charge->save();
}
for($i=1;$i<=$no_monthly_charges;$i++){
$monthly_id=$request->input('monthly_id'.$i);
$monthly_charge=MonthlyCourtCharge::find($monthly_id);
//$monthly_charge->infrastructure_details_id=$request->input('inf_id');
$no_days=0;
$scheme_type=$request->input('scheme_type'.$i);
if($scheme_type=="scheme1"){
$no_days=22;
}else{
$no_days=31;
}
$monthly_charge->scheme_type=$scheme_type;
$monthly_charge->no_days=$no_days;
$monthly_charge->hour_type=$request->input('hour_type'.$i);
$monthly_charge->from_time=date('H:i',strtotime($request->input('from_time'.$i)));
$monthly_charge->to_time=date('H:i',strtotime($request->input('to_time'.$i)));
$monthly_charge->price=$request->input('price'.$i);
$monthly_charge->save();
}
$id=$pricing_details->id;
$request->session()->put('price_id', $id);
return redirect('/facility_details');
}
}
here is the layout code
<div class="dynamic_row2">
<div class="row">
<div class="col-md-12 bg-info text-white">
<h4 calss="h4-responsive">4. Monthly Charges</h4>
</div>
</div>
<div class="row">
<div class="col-md-2">
<h4 class="h4-responsive">Scheme Type</h4>
</div>
<div class="col-md-2">
<h4 class="h4-responsive">Hour Type</h4>
</div>
<div class="col-md-3">
<h4 class="h4-responsive">From Time</h4>
</div>
<div class="col-md-3">
<h4 class="h4-responsive">To Time</h4>
</div>
<div class="col-md-2">
<h4 class="h4-responsive">Price / hour</h4>
</div>
</div>
#php
$i=1;
#endphp
#foreach($monthly_charges as $monthly)
{{Form::hidden('monthly_id'.$i,$monthly->id)}}
<div class="row" id="discount_row1">
<div class="col-md-2">
<div class="form-group">
<select name="scheme_type<?php echo $i; ?>" class="form-control" required>
<option value="<?php echo $monthly->scheme_type; ?>" selected><?php echo $monthly->scheme_type; ?></option>
<option value="" disabled>Select Scheme</option>
<option value="scheme1">Scheme1 - 22 days</option>
<option value="scheme2">Scheme2 - 30/31 days</option>
</select>
</div>
</div>
<div class="col-md-2">
<div class="form-group">
<select name="hour_type<?php echo $i; ?>" class="form-control" required>
<option value="<?php echo $monthly->hour_type; ?>" selected><?php echo $monthly->hour_type; ?></option>
<option value="" disabled>Select hour type</option>
<option value="Peak hour">Peak hour</option>
<option value="NonPeak hour">NonPeak hour</option>
</select>
</div>
</div>
<div class="col-md-3">
<div class="form-group">
{{ Form::time('from_time'.$i,$monthly->from_time,['class'=>'form-control','placeholder'=>'From Time','required']) }}
</div>
</div>
<div class="col-md-3">
<div class="form-group">
{{ Form::time('to_time'.$i,$monthly->to_time,['class'=>'form-control','placeholder'=>'From Time','required']) }}
</div>
</div>
<div class="col-md-2">
<div class="form-group">
{{ Form::number('price'.$i++,$monthly->price,['class'=>'form-control','placeholder'=>'From Time','required']) }}
</div>
</div>
</div>
#endforeach
</div>
{{Form::submit('Update',['class'=>'btn btn-primary'])}}
{!! Form::close() !!}
#endif
i have checked database datatypes since i works most of the time no problem there. i don't what iam missing

Laravel Livewire Select2 Multiple sellect issue

Product variation Color first time is disable after enable can select color.
without wire:ignore after select color its disappear from select option.
If I use wire:ignore
{{ $colors_active== 0 ?'disabled':'' }}
disable is not removed, it's still disabled.
product.blade.php
<div class="row g-3 align-center">
<div class="col-lg-3">
<div class="form-group">
<input class="form-control" type="text" value="Colors" readonly>
</div>
</div>
<div class="col-lg-8">
<div class="form-group">
<select class="form-control color-select2" multiple style="width: 100%" {{ $colors_active== 0 ?'disabled':'' }} >
#foreach (App\Models\Admin\Color::orderBy('name', 'asc')->get() as $key => $color)
<option value="{{$color->code}}">
{{$color->name}}
</option>
#endforeach
</select>
</div>
</div>
<div class="col-lg-1">
<div class="form-group">
<div class="form-control-wrap">
<label class="c-switch c-switch-pill c-switch-opposite-success">
<input wire:model="colors_active" class="c-switch-input" type="checkbox" value="1" ><span class="c-switch-slider"></span>
</label>
</div>
</div>
</div>
</div>
Product.php Component
{
public $colors_active;
public $choice_attributes = [];
public $colors = [];
public function mount(){
}
public function render()
{
return view('livewire.admin.product.product')->layout('admin.layouts.master');
}
}
Livewire will ignore any changes to anything marked with wire:ignore, so naturally it will not re-render anything when its ignored.
You can solve this by using Apline.js, and entangle the $colors_active property to an attribute in Alpine.js. Entangle means that when Livewire updates the variable in its backend, Alpine updates its variable too - and vice-versa (Alpine.js updates Livewire when the variable is changed). Basically, its kept in sync between the two.
Then, you can make Alpine.js dynamically bind the disabled property of your select-element based on that variable.
<div x-data="{ 'colorsActive': #entangle('colors_active') }">
<select
class="form-control color-select2"
multiple
style="width: 100%"
wire:ignore
x-bind:disabled="colorsActive == 0"
>
#foreach (App\Models\Admin\Color::orderBy('name', 'asc')->get() as $key => $color)
<option value="{{ $color->code }}">
{{ $color->name }}
</option>
#endforeach
</select>
</div>
This is the way I use to approach this kind of solutions with Livewire, without Alpine because unfortunately I haven't learn it.
In the blade component:
<div class="col d-flex display-inline-block">
<label>Device</label>
<select {{ $customer ? '' : 'disabled' }} wire:model="selectedItem" class="form-control contact_devices_multiple" multiple="multiple" data-placeholder="Select" style="width: 100%;">
#foreach($devices as $device)
<option value="{{ $device->id }}">{{ $device->alias }}</option>
#endforeach
</select>
</div>
<script>
window.loadContactDeviceSelect2 = () => {
$('.contact_devices_multiple').select2().on('change',function () {
livewire.emitTo('contact-component','selectedItemChange',$(this).val());
});
}
loadContactDeviceSelect2();
window.livewire.on('loadContactDeviceSelect2',()=>{
loadContactDeviceSelect2();
});
</script>
in component
public $customer = null;
public $selectedItem = [];
public function hydrate()
{
$this->emit('loadContactDeviceSelect2');
}
public $listeners = [
'selectedItemChange',
];
public function selectedItemChange($value)
{
dd($value);
}

Laravel Multiple Filters Table with Multiple Collections

I am trying to create a multiple filter function to filter my table. I have a model called Product and it has collections named categories, colors and sizes. I have the following in my view:
<div class="col-6">
<div class="form-group mt-3">
<label>{{ __('Categories') }}</label>
<select class="form-control" id="select-categories">
<option value="0">All</option>
#foreach($categories as $category)
<option value="{{ $category->id }}">{{ $category->name }}</option>
#endforeach
</select>
</div>
</div>
<div class="col-6">
<div class="form-group mt-3">
<label>{{ __('Colors') }}</label>
<select class="form-control" id="select-colors">
<option value="0">All</option>
#foreach($colors as $color)
<option value="{{ $color->id }}">{{ $color->name }}</option>
#endforeach
</select>
</div>
</div>
<div class="col-6">
<div class="form-group mt-3">
<label>{{ __('Sizes') }}</label>
<select class="form-control" id="select-sizes">
<option value="0">All</option>
#foreach($sizes as $size)
<option value="{{ $size->id }}">{{ $size->name }}</option>
#endforeach
</select>
</div>
</div>
And then I use JQuery to send a AJAX request to my controller with the current selected option values of all three dropdowns.
Example data:
Categories: All
Colors: Red
Sizes: Small
Now I wish to show the products with the color red and size small but the category doesn't matter.
I get the collections with the use of one to many relationships and link tables.
$product->categories
$product->colors
$product->sizes
I am not sure how to approach this.
You can do something similar to below:
public function index(ProductRequest $request)
{
$query = Product::where($request->validated());
return ProductResource::collection($query);
}
and in your ProductRequest you can define filters that you want to allow
class ProductRequest extends FormRequest
{
...
public function rules()
{
return [
'categories' => 'exists:categories,id'
....
];
}

How to redirect to the page which shows only the data inserted to the database from that id

For example if there is a form which creates customers, and when you filled the form and submit the page should redirect and show the data which was inserted to that form by a specific individual id. in my case it redirects to a page and views all the data in the database.
Here's my Web.php
Route::get('customers','CustomersController#index');
Route::get('customers/create','CustomersController#create');
Route::post('customers','CustomersController#store');
Route::get('customers/{customer}','CustomersController#show');
Here's my Controller
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Customer;
use App\Company;
class CustomersController extends Controller
{
public function index(){
$customers = Customer::all();
return view('customers.index',compact('customers'));
}
public function create(){
$companies = Company::all();
return view ('customers.create',compact('companies'));
}
public function store()
{
$data = request()->validate([
'name'=>'required | min:3',
'email'=>'required | email',
'active'=>'required ',
'company_id'=>'required',
]);
Customer::create($data);
return redirect('customers');
}
// * Route Model binding
public function show(Customer $customer){
return view('customers.show',compact('customer'));
}
}
Here's my Customer Model
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Customer extends Model
{
protected $guarded = [];
public function getActiveAttribute($attribute){
return [
0 => 'Inactive',
1 => 'Active',
] [$attribute];
}
public function scopeActive($query){
return $query->where('active',1);
}
public function scopeInactive($query){
return $query->where('active',0);
}
public function company()
{
return $this->belongsTo(Company::class);
}
}
Here's my Create Blade
#extends('layouts')
#section('title','Add New Customer')
#section('content')
<div class="row">
<div class="col-12">
<h1>Add New Customer</h1>
</div>
</div>
<div class="row">
<div class="col-12">
<form action="/customers" method="POST" >
<div class="form-group">
<label for="name">Name</label>
<input type="text" class="form-control" name="name" value="{{old('name')}}">
<div>{{$errors->first('name')}}</div>
</div>
<div class="form-group">
<label for="email">Email</label>
<input type="text" class="form-control" name="email" value="{{old('email')}}">
<div>{{$errors->first('email')}}</div>
</div>
<div class="form-group">
<label for="">Customer Status</label>
<select name="active" id="active" class=" form-control">
<option value="" disabled>Select Customer Status</option>
<option value="1">Active</option>
<option value="0">Inactive</option>
</select>
</div>
<div class="form-group">
<label for="company_id">Company</label>
<select name="company_id" id="company_id" class=" form-control">
#foreach ($companies as $company)
<option value="{{ $company->id }}">{{ $company->name }}</option>
#endforeach
</select>
</div>
<button type="submit" class=" btn btn-dark">Add Customer</button>
#csrf
</form>
</div>
</div>
#endsection
Here's my show blade
#extends('layouts')
#section('title','Details for ' . $customer->name)
#section('content')
<div class="row">
<div class="col-12">
<h1>Details for {{ $customer->name }}</h1>
</div>
</div>
<div class="row">
<div class="col-12">
<p><strong>Name : </strong> {{ $customer->name }}</p>
<p><strong>Email : </strong> {{ $customer->email }}</p>
<p><strong>Company : </strong> {{ $customer->company->name }}</p>
<p><strong>Status : </strong> {{ $customer->active }}</p>
</div>
</div>
#endsection
Redirect to the show route rather than the customers index route:
$customer = Customer::create($data);
return redirect('customers/' . $customer->id);

Laravel - Form Action to Controller Function

I'm trying to create a situation where a user can load a page, select a timesheet number, and then have the system delete all records associated with that timesheet number. This function loads the form page:
public function deletetimesheetLoad()
{
$timesheets = collect(DB::select("SELECT dbo.TIME.Source AS Source
FROM dbo.TIME
GROUP BY dbo.TIME.Source
ORDER BY dbo.TIME.Source ASC"));
return view('utilities/deletetimesheet',['timesheets' => $timesheets]);
}
This is the actual blade template:
<form method="post"
action="{{url('/time/deletetimesheet/process')}}"
enctype="multipart/form-data"
<div class="form-group">
<label class="col-md-12 control-label" for="TIMESHEETNUMBER">Timesheet Number</label>
<div class="col-md-12">
<select required id="TIMESHEETNUMBER" name="TIMESHEETNUMBER" class="form-control select2_field">
<option value=""></option>
#foreach ($timesheets as $row)
<option value="{{ $row->Source }}">{{ $row->Source }}</option>
#endforeach
</select>
</div>
</div>
<div id="saveActions" class="form-group">
<input type="hidden" name="save_action" value="Submit">
<div class="btn-group">
<button type="submit" class="btn btn-success">
<span class="fa fa-save"></span>
<span data-value="Submit">Submit</span>
</button>
</div>
<span class="fa fa-ban"></span> Cancel
</div>
This is the function that the form action is pointing to:
public function deletetimesheetProcess(TimesheetRequest $request)
{
DB::table('TIME')->where('Source', $request->get('TIMESHEETNUMBER'))->delete();
\Alert::success(trans('yay'))->flash();
return view('details/customershow',[]);
}
Here are the defined routes for the two functions:
Route::get('/time/deletetimesheet', 'Admin\TimeCrudController#deletetimesheetLoad');
Route::post('/time/deletetimesheet/process', 'Admin\TimeCrudController#deletetimesheetProcess');
Currently the blade template loads correctly, and does not throw an error on submit - just reloads the current page. What am I doing wrong?

Categories