how to convert select box html to form html collactive - laravel

Hi i have this in my controller
$listPatient = Patient::get();
return view('backend.consultations.create')->with([
'patient' => $this->patient,
'patient_id' =>$this->patient_id,
'listPatient' => $listPatient,
]);
and in my view i have
<div class="form-group">
<div class="col-md-2">
List clients
</div>
<div class="col-lg-10">
<select name="patient_id">
<option value="0">Veillier séléctionner un patien </option>
#foreach($listPatient as $key)
<option value="{{$key->id}}">{{$key->nom_patient}} {{$key->prenom_patient}}</option>
#endforeach
</select>
</div><!--col-lg-10-->
</div><!--form control-->
and it work fine,i want to use Form::select but it doesn't work can anyone help me please

In your controller you would want the following to fetch $listPatient
$listPatient = Patient::lists("nom_patient","id")->toArray();
and in your view you would want
{!! Form::select('patient_id', [0 => 'Veillier séléctionner un patien'] + $listPatient, null) !!}

a simple solution
enter code here : <div class ="form-group">
{{ Form::label('list_patient', trans('validation.attributes.backend.consultations.nom_patient'), ['class' => 'col-lg-2 control-label required']) }}
<div class="col-lg-10">
<select id="patient_id" name="patient_id" class="form-control select2 box-size" required="required" placeholder=" Non définie" >
<option value="">Non définie</option>
#foreach($listPatient as $key)
<option value ="{{$key->id}}">{{$key->nom_patient}} {{$key->prenom_patient}}</option>
#endforeach
</select>
</div><!--col-lg-3-->
</div><!--form control-->

Related

How to show query search results without reloading on button click in laravel

I am trying to show query results without reloading in laravel
This is the form in which I get the category and country
<form class="new_is" id="lets_search" action="{{route('advanced-search.show','searchInfluencer')}} method="GET">
<div class="row">
<div class="col-md-6">
<label>Category</label>
<select name="filter_category" id="filter_category" class="influencer-category-picker form-control form-select" >
<option value="">Click to select the category</option>
#foreach($Categories as $Category)
<option id="category" name="{{$Category->id}}" value="{{$Category->id}}">{{$Category->name}}</option>
#endforeach
</select>
</div>
<div class="col-md-6">
<label>Location</label>
<select name="filter_country" id="filter_country" class="influencer-category-picker form-control form-select">
<option value="" >Click to select the Country</option>
#foreach($Countries as $Country)
<option id="country" value="{{$Country->id}}">{{$Country->name}}</option>
#endforeach
</select>
</div>
<div id="advanced-search" class="col-md-12" style="margin-top: 20px">
<p>
<button type="button" name="filter" id="filter" class="btn btn-lg btn-primary track-event">Search</button>
</p>
</div>
</div>
</form>
This is the controller including the query and returning the result and everything works well but on another page
public function searchInfluencer(Request $request)
{
$category = $request->get('category');
$country = $request->get('country');
if(empty($category) && !empty($country)){
$influencers =Influencer::where('country_id', $country)->get();
return response()->json($influencers);
}
elseif (!empty($category) && empty($country)){
$influencers =Influencer::where('category_id', $category)->get();
return response()->json($influencers);
}
elseif (!empty($category) && !empty($country)){
$influencers =Influencer::where('category_id', $category)->where('country_id', $country)->get();
return response()->json($influencers);
}
Here is the index function:
public function index(){
return view('businesses.advanced-search-business', [
'Genders' => Gender::all(),
'Countries' => Country::all(),
'Categories' => Category::all(),
]);
}
The issue is that on clicking the search button, I want to show the results on the same page without reloading. Thank you in advance for your help

Dropdown filter doesn't work in Laravel 5.8

I tried to do the input dropdown filter using this code:
<div class="col-md-2">
<div class="text-justify" >
<select class="itemName form-control form-filter" style="width:90px;" name="itemName" id="itemName">
#foreach($data as $category){
<option value="{{$category->Umur}}">{{$category->Umur}}</option>
#endforeach
</select>
</div>
</div>
However, my code does not run as I expected (in picture below) - the code only call the first row in database. What should I edit with the code?
<div class="col-md-2">
<div class="text-justify" >
<select class="itemName form-control form-filter" style="width:90px;" name="itemName" id="itemName">
#foreach($data as $category){
<option value="{{$category->Umur}}">{{$category->Umur}}</option>
#endforeach
</select>
</div>
</div>
remove the { after #foreach & change the value like
<div class="col-md-2">
<div class="text-justify" >
<select class="itemName form-control form-filter" style="width:90px;" name="itemName" id="itemName">
#foreach($data as $category)
<option value="{{$category->id}}">{{$category->Umur}}</option>
#endforeach
</select>
</div>
</div>
I too had this issue myself, this is what i used:
<div class="form-group">
<div class="btn-group">
<input list="brow" class="form-control"name="instagram_account_id" id="instagram_account_id" placeholder="Search..">
<datalist id="brow">
#if($users->count() > 0)
#foreach($users as $user)
<option value="{{$user->id}}">{{ $user->username }}</option>
#endforeach
#else
No User(s) Found
#endif
</datalist>
</div>
</div>
As for my controller:
public function index()
{
$comments = Comment::paginate(10);
$users = InstagramAccount::all();
return view('instagramComments', [
'comments' => $comments,
'users' => $users,
]);
}
And (if you're going to use it an a form) my insert in my controller:
public function insert(Request $req)
{
$instagram_comment = $req->input('instagram_comment');
$instagram_account_id = $req->input('instagram_account_id');
$data = array('comment'=>$instagram_comment, 'account_id'=>$instagram_account_id);
DB::table('comments')->insert($data);
return redirect('/comments');
}
(if using the insert above, use this in your web route):
Route::post('/locationsInsert', 'InstagramLocationsController#insert')->middleware('auth');
Hope any of this helps!

show data of nested foreach loop in two different Select Tag in laravel

I have two Model(Category,Brand) are relate with one to many relation(hasMany) in laravel, Like category hasMany brands. now i want to display brand data in one select tag and category model data in another select tag.
Here is my Controller code
$category=Category::with('brands')->get();
return view('product.add')->with('category',$category);
Here is my view blade.
<div class="form-group">
<select name="category_id" class="input">
<option value="">Select Main Category</option>
#foreach($category as $category)
<option value="{{$category->id}}">{{$category->name}}</option>
#endforeach
</select>
</div>
<div class="form-group">
<select name="brand_id" class="input">
#foreach($brands->brands as $brand)
<option value="{{$brand->id}}">{{$brand->name}}</option>
#endforeach
</select>
</div>
its not working
You can do something simple like this:
<div class="form-group">
<select name="category_id" class="input">
<option value="">Select Main Category</option>
#foreach($category as $cat)
<option value="{{$cat>id}}">{{$cat->name}}</option>
#endforeach
</select>
</div>
<div class="form-group">
<select name="brand_id" class="input">
#foreach($category as $cat)
#foreach($cat->brands as $brand)
<option value="{{$brand->id}}" category="{{$cat->id}}">{{$brand->name}}</option>
#endforeach
#endforeach
</select>
</div>
and you need to show only the brands for the selected option using javascript(jquery)
$('[name="category_id"]').on('change',function() {
if($(this).val() != '') {
$('[name="brand_id"] option').hide();
$('[name="brand_id"] option[category="'+$(this).val()+'"]').show();
} else {
$('[name="brand_id"] option').show();
}
});

when click submit error is occur

when click on submit data,data successfully save and show in database but got error in index page, i think don't get it user-id
Trying to get property 'id' of non-object (View:
C:\xampp\htdocs\ytl\resources\views\profile\index.blade.php)
This is index.blade.php file
<form method="post", id="form", action="{{action('Profile\UserProfileController#index')}}" accept-charset="UTF-8">
{{ csrf_field() }}
<div class="row">
<div class="col-md-6 mb-3 form-group">
Exchange:<select name="exchange_id" id="exchange" class="form-control " onchange="myfunc()">
<option value="">Select</option>
#foreach($exchanges as $key=>$val )
<option value="{{ $val->id }}">{{ $val->exchange }}</option>
#endforeach
</select>
{{--{!! Form::label('exchange_id', 'Exchanges: ') !!}--}}
{{--{!! Form::select('exchange_id', ['' => 'Choose Options'] + $exchanges, null, ['class' => 'form-control', 'id' => 'exchange', 'name' => 'exchange_id'])!!}--}}
</div>
<div class="col-md-6 mb-3 form-group">
Market<select name="market_id" id="market" class="form-control bindselect" >
<option value="">Select</option>
{{--#foreach($markets as $key=>$val )--}}
{{--<option value="{{ $val->id }}">{{ $val->market }}</option>--}}
{{--#endforeach--}}
</select>
</div>
<div class="col-md-6 mb-3 form-group">
Country:<select name="country_id" id="country" class="form-control " >
<option value="">Select</option>
#foreach($countries as $key=>$val )
<option value="{{ $val->id }}">{{ $val->country }}</option>
#endforeach
</select>
</div>
<div class="col-md-6 mb-3 form-group">
Company:<select name="brokerage_company_id" id="brokerage_company_id" class="form-control " >
<option value="">Select</option>
#foreach($brokerage_company as $key=>$val )
<option value="{{ $val->id }}">{{ $val->brokerage_company }}</option>
#endforeach
</select>
</div>
<div class="col-md-6 mb-3 form-group">
{{--{!! Form::label('Intraday_Charge', 'Intraday Charge:') !!}--}}
{{--{!! Form::text('Intraday_Charge', null, ['required' => 'required', 'class'=>'form-control number_only'])!!}--}}
Intraday_charge: <input type="text" name="charge_intraday" class="form-control"><br>
</div>
<div class="col-md-6 mb-3 form-group">
Delivery_charge: <input type="text" name="charge_delivery" class="form-control"><br>
</div>
<div class="col-md-6 mb-3 form-group">
Delivery_charge: <input type="text" name="charge_per_lot" class="form-control"><br>
</div>
<div class="col-md-6 mb-3 form-group">
Delivery_charge: <input type="text" name="charge_per_order" class="form-control"><br>
</div>
<div class="mb-3 form-group">
{{--{!! Form::submit('Add trade', ['class'=>'btn btn-success btn-lg']) !!}--}}
<input type="submit" value="Submit">
</div>
</div>
</form>
This is controller in which 2 method. 1st index and 2nd store method
public function index(Request $request){
$exchanges = Exchange::select('exchange','id')->get();
$markets = Market::select('market','id')->get();
$countries = Country::select('country','id')->get();
$brokerage_company = BrokerageCompany::select('brokerage_company','id')->get();
return view('profile.index', compact( 'exchanges','markets','countries','brokerage_company'));
}
public function store(Request $request){
$Input = $request->all();
$user = Auth::user();
$user->userprofile()->create($Input);
$user_id = Auth::user()->id;
$exchanges = Exchange::pluck('exchange','id')->all();
$markets = Market::pluck('market','id')->all();
$countries = Country::pluck('country','id')->all();
$brokerage = BrokerageCompany::pluck('brokerage_company','id')->all();
$user_profile = UserProfile::pluck('charge_intraday','charge_delivery','charge_per_lot','charge_per_order');
return view('profile.index', compact( 'exchanges','markets','countries','brokerage','user_profile'));
}
Since Laravel 5.2 pluck() method of Eloquent builder returns a collection of values from given column. When you call all() on this collection, you simply get an array of values from that first column. For example, when you call
$exchanges = Exchange::pluck('exchange','id')->all();
$exchanges will be an array that contains all values of exchange column in your Exchanges table. Hence the error, as you're trying to access id attribute of this scalar value.
I guess you're trying to limit number of columns fetched from the database. Call select() method instead of pluck():
$exchanges = Exchange::select('exchange','id')->get();

Laravel chosen-select retrieving old value from the form

I am trying to retrieve old value from my form on the edit form, but it seems impossible to find an answer, because everyone in the world decided to use Form::select from laravelcollective/html library. I am trying to use the regular HTML to work this, and it's not able to retrieve old value from the form.
This is my HTML Code.
<div class="col-xs-12 form-group">
<label class="control-label">Role*</label>
<select name="roles[]" data-placeholder="Choose a Role for this User..." class="chosen-select" multiple tabindex="4">
<option value="">Select</option>
#foreach ($roles as $key => $value)
<option value="{{ $key }}" {{ in_array($key, old("roles")) ? "selected":"") }} >{{ $value }}</option>
#endforeach
</select>
</div>
This is my controller code.
public function edit($id)
{
$roles = Role::get()->pluck('name', 'id');
$user = User::findOrFail($id);
return view('admin.users.edit', compact('user', 'roles'));
}
I am trying to convert the following into regular html
<div class="row">
<div class="col-xs-12 form-group">
{!! Form::label('role', trans('global.users.fields.role').'*', ['class' => 'control-label']) !!}
{!! Form::select('role[]', $roles, old('role') ? old('role') : $user->role->pluck('id')->toArray(), ['class' => 'form-control select2', 'multiple' => 'multiple', 'required' => '']) !!}
<p class="help-block"></p>
#if($errors->has('role'))
<p class="help-block">
{{ $errors->first('role') }}
</p>
#endif
</div>
</div>
Try this:
<div class="col-xs-12 form-group {{ $errors->has('role') ? 'has-error' : '' }}">
<label for="role" class="control-label">Role *</label>
<select class="form-control select2" name="role[]" id="role" required multiple>
#foreach ($roles as $id => $label)
<option value="{{ $id }}" #if (collect(old('role', $user->role->pluck('id') ?? []))->contains($id)) selected="selected" #endif>{{ $label }}</option>
#endforeach
</select>
<span class="help-block">
#if ($errors->has('role'))
{{ $errors->first('role') }}
#endif
</span>
</div>
In your controller's update/store function, make sure you either use the built-in validation methods, which will populate the session on any errors.
or
If you have other checks, make sure you return the input when redirecting back.
return redirect()->back()
->withInput() // <- return input with redirect, will populate session
->with('error', 'Persist failed'); // <- optional

Resources