Dropdown filter doesn't work in Laravel 5.8 - laravel

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!

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

How to use select2 multiselect with livewire

What I am trying to achieve is, there are two models A and B with a relationship of One to many, where A has many B. So if the records for A and B have already been created, A can be assigned to many B. So I started this current project with livewire, and now I am at a loss. Normal select with multiple works just fine and populates the array in the backend of livewire. But when I use select2 nothing is stored in the array, and when I dd it, it shows an empty array.
Even though livewire provides wonderful set of tools, I am starting to realize that there is a lot of gray areas where things are missing or lacks external support, like select2 here.
Here is what I have done so far:
<div>
<div class="tab-pane fade show active" id="card1" role="tabpanel" aria-labelledby="card1-tab">
<div class="pt-3"></div>
<div class="row" >
<div class="form-group col">
<label for="cards">Enter Cards</label>
<select class="form-control select2" id="cards" multiple="multiple" onchange="this.dispatchEvent(new InputEvent('input'))>
<option disabled="">Select Cards</option>
#foreach($cardsUnassigned as $card)
<option value="{{ $card->id }}">{{ $card->imei_number }}</option>
#endforeach
</select>
</div>
<div class="form-group col">
<label for="technicians" class="">Technicians</label>
<select class="form-control select2" id="technicians" wire:model='techies.'>
<option disabled="">Select Technicians</option>
#foreach($technicians as $tech)
<option value="{{ $tech->id }}">{{ $tech->first_name }}</option>
#endforeach
</select>
</div>
</div>
#foreach($cardSelect as $key => $value)
{{$value}}
#endforeach
<button wire:click="assignCardToTech()" class="btn btn-primary">Submit</button>
</div>
</div>
#push('scripts')
<script>
$('#cards').select2({
placeholder: 'Select Cards to assign'
});
//send data to livewire
$('#technicians').select2({
placeholder: 'Select Technicians'
});
</script>
#endpush
And the backend:
<?php
namespace App\Http\Livewire\Stock\Assigns;
use App\Models\Card;
use App\Models\Sim;
use App\Models\Technician;
use Livewire\Component;
use Livewire\WithPagination;
use Illuminate\Support\Facades\Auth;
class Assigns extends Component
{
public $sortBy_card = 'imei_number';
public $sortBy_sim = 'ip';
public $sortDirection = 'asc';
public $search = '';
public $perPage = 10;
public $cardSelect = [];
public $techies;
public function render()
{
$simsUnassigned = Sim::query()
->whereNull('technician_id')
->searchUnassigned($this->search)
->get();
$cardsUnassigned = Card::query()
->whereNull('technician_id')
->searchUnassignedCard($this->search)
->get();
// dd($cardsUnassigned);
$simsAssigned = Sim::query()
->searchAssigned($this->search)
->orderBy($this->sortBy_sim, $this->sortDirection)
->paginate($this->perPage);
$cardsAssigned = Card::query()
->searchAssignedCard($this->search)
->orderBy($this->sortBy_card, $this->sortDirection)
->paginate($this->perPage);
$technicians = Technician::query()->get();
return view('livewire.stock.assigns.assigns',compact('simsUnassigned','cardsUnassigned','simsAssigned','cardsAssigned','technicians'))
->extends('layouts.base')
->section('content');
}
public function assignCardToTech(){
dd($this->cardSelect);
if($this->cards){
foreach($this->cards as $card){
}
}
}
}
Hopefully this helps.
######## INSIDE LIVEWIRE COMPONENT
public array $locationUsers = [];
protected $listeners = ['locationUsersSelected'];
public function locationUsersSelected($locationUsersValues)
{
$this->locationUsers = $locationUsersValues;
}
######## INSIDE LIVEWIRE BLADE
<div class="col-md-12 mb-3" wire:ignore>
<label for="locationUsers">Select Users</label>
<select id="locationUsers" class="form-control select2" multiple="multiple">
<option value="">--select--</option>
#foreach($this->users as $id => $name)
<option value="{{ $id }}">{{ $name }}</option>
#endforeach
</select>
</div>
######## INSIDE LIVEWIRE SCRIPTS
document.addEventListener('livewire:load', function () {
$('#locationUsers').on('select2:close', (e) => {
#this.emit('locationUsersSelected', $('#locationUsers').select2('val'));
});
$('#locationUsers').val(#this.get('locationUsers')).trigger('change');
});
Your could try adding this to your html
<div class="form-group col-md-6" wire:ignore>
<label for="manager" class="mb-0 h5">Assign Managers:</label>
<select wire:model="reporting_managers" id="manager" class="select-2 multiple='multiple' data-placeholder="Assign Managers">
#foreach ($managers as $manager)
<option value="{{ $manager->id }}" data-name="{{ $manager->name }}">{{ $manager->name }}</option>
#endforeach
</select>
</div>
and then in your javaScript section
$('#manager').on('select2:select', function (e) {
#this.set('reporting_managers', $('#manager').select2('val'));
});
$('#manager').on('select2:unselect', function (e) {
#this.set('reporting_managers', $('#manager').select2('val'));
});
This will directly store your selection in variable in livewire and won't get deselected on render.

Laravel search from multi select dropdown

I am trying to create a search filter using dropdown selections. The code only shows results when all dropdowns (make and model) are selected. But it should work even single dropdown (either make or model)is selected. At the moment, if I select a single dropdown, the result page shows "No Matching Data found". this is what I have done -
Controller
class CarFrontController extends Controller
{
public function index_classiccars(Request $request)
{
$filterMakes = DB::table('cars')->select('make')->distinct()->get()->pluck('make');
$filterModels = DB::table('cars')->select('model')->distinct()->get()->pluck('model');
$classiccar = Car::query();
if ($request->has('make')) {
$classiccar->where('make', $request->make);
}
if ($request->has('model')) {
$classiccar->where('model', $request->model);
}
return view(
'layouts.frontend.cars.classiccars_index',
[
'filterMakes' => $filterMakes, 'filterModels' => $filterModels,
'classiccars' => $classiccar->get()
]
);
}
public function store(Request $request)
{
return $request->all();
return view('layouts.frontend.cars.classiccars_index');
}
}
data blade
#forelse ($classiccars as $car)
<div class="row">
<div class="col-lg-5 col-md-5 col-pad">
<div class="car-thumbnail">
<a href="car-details.html" class="car-img">
<div class="price-box">
<span>£{{ $car->price }}</span>
</div>
#php $i=1; #endphp
#foreach ($car->join_caralbum as $image)
#if ($i>0)
<img class="d-block w-100" src="{{asset($image->image_location)}}" alt="car" height="225px" >
#endif
#php $i--; #endphp
#endforeach
</a>
<div class="carbox-overlap-wrapper">
<div class="overlap-box">
<div class="overlap-btns-area">
<a class="overlap-btn" href="{{ url('car_show/'.$car->id) }}">
<i class="fa fa-eye-slash"></i>
</a>
</div>
</div>
</div>
</div>
</div>
<div class="col-lg-7 col-md-7 col-pad align-self-center">
<div class="detail">
<h3 class="title">
{{ $car->make }}
</h3>
<ul class="custom-list">
<li>
{{ $car->model }} /
</li>
......
</ul>
<ul class="facilities-list clearfix">
.....
.....
....
</ul>
</div>
</div>
</div>
</div>
#empty
No matching data found
#endforelse
filter blade
{!! Form::open(['action'=>'CarFrontController#index_classiccars','method'=>'GET']) !!}
<select class="form-control" name="make" id="make">
<option> Make(All)</option>
#foreach ($filterMakes as $make)
<option value="{{ $make }}">{{ $make }}</option>
#endforeach
</select><br>
<select class="form-control" name="model" id="model">
<option> Model(All)</option>
#foreach ($filterModels as $model)
<option value="{{ $model }}">{{ $model }}</option>
#endforeach
</select><br>
{{ Form::submit('Update Search',['class'=>'btn btn-primary']) }}
Your query results empty because even if you don't select any of your options, you actually do. If options don't have a value, the value is the text you give it to the option.
In your case, if you select just a model, actually you are selected 'Make(All)' option too. You can see this simply by putting dd($request->all()) on your index_classiccars method.
For checking selects, you can give empty value to first(default) option and control it with request()->filled('input').

Laravel: Array to string conversion Error message

I'm trying to make search function and having problem. I have Array to string conversion error message.
Could you teach me right code please?
Here is this program usage
1.User select value then click [search] button (This is search.blade.php)
2.Search result will display at result.blade.php
my Laravel Framework is 6.18.8
search.blade.php
<form action="{{ route('search') }}" class="form-image-upload" method="POST" enctype="multipart/form-data">
{!! csrf_field() !!}
<div class="col-md-5">
<strong>TYPE</strong>
<select name="type" class="form-control">
<option value="-" selected>-</option>
<option value="j">j</option>
<option value="w">w</option>
</select>
</div>
<div class="col-md-5">
<strong>wc</strong>
<select name="wc" class="form-control">
<option value="N0" selected>0</option>
<option value="N1">1</option>
<option value="N2">2</option>
<option value="N3">3</option>
</select>
</div>
<div class="col-md-5">
<strong>FC</strong>
<select name="fc" class="form-control">
<option value="0" selected>0</option>
<option value="f01">f01</option>
<option value="f02">f02</option>
<option value="f03">f03</option>
</select>
</div>
<div class="col-md-5">
<strong>YC</strong>
<select name="yc" class="form-control">
<option value="0" selected>0</option>
<option value="yc1">yc1</option>
<option value="yc2">yc2</option>
</select>
</div>
<div class="col-md-5">
<strong>SC</strong>
<select name="sc" class="form-control">
<option value="Z01" selected>Z01</option>
<option value="Z02" selected>Z02</option>
<option value="Z03" selected>Z03</option>
</select>
</div>
<div class="col-md-2">
<br/>
<button type="submit" class="btn btn-success">Search</button>
</div>
</div>
</form>
result.blade.php
{!! csrf_field() !!}
<div class='list-group gallery'>
#if($images->count())
#foreach($images as $image)
<div class='col-sm-4 col-xs-6 col-md-3 col-lg-3'>
<a class="thumbnail fancybox" rel="ligthbox" href="/images/{{ $image->image }}">
<img class="img-responsive" alt="" src="/images/{{ $image->image }}" />
<div class='text-center'>
<small class='text-muted'></small>
</div> <!-- text-center / end -->
</a>
</div> <!-- col-6 / end -->
#endforeach
#endif
</div> <!-- list-group / end -->
ImageGalleryController.php
public function search()
{
$images = ImageGallery::get();
return view('search',compact('images'));
}
public function order(Request $request)
{
$data = $request->all();
$images = ImageGallery::where(['type',$request->$data['type']],
['wc',$request->$data['type']],
['fc',$request->$data['fc']],
['yc',$request->$data['yc']],
['sc',$request->$data['sc']])->get();
return view('result',compact('images'));
}
Web.php
// search section
Route::post('search', 'ImageGalleryController#order');
Route::get ('search', 'ImageGalleryController#search');
UPDATE
Curent my controller
public function search()
{
$images = ImageGallery::get();
return view('search',compact('images'));
}
public function order(Request $request) {
$data = $request->all();
$images = ImageGallery::where([
['type', $data['type']],
['wc',$data['type']],
['fc',$data['fc']],
['yc',$data['yc']],
['sc',$data['sc']]
])->get();
dd($images);
return view('search', compact('images'));
}
Check this code:
search.blade.php
<form action="{{ route('search') }}" class="form-image-upload" method="POST" enctype="multipart/form-data">
{!! csrf_field() !!}
<div class="col-md-5">
<strong>TYPE</strong>
<select name="type" class="form-control">
<option value="" selected>Please Select</option>
<option value="j">j</option>
<option value="w">w</option>
</select>
</div>
<div class="col-md-5">
<strong>wc</strong>
<select name="wc" class="form-control">
<option value="" selected>Please Select</option>
<option value="N0">0</option>
<option value="N1">1</option>
<option value="N2">2</option>
<option value="N3">3</option>
</select>
</div>
<div class="col-md-5">
<strong>FC</strong>
<select name="fc" class="form-control">
<option value="" selected>Please Select</option>
<option value="f01">f01</option>
<option value="f02">f02</option>
<option value="f03">f03</option>
</select>
</div>
<div class="col-md-5">
<strong>YC</strong>
<select name="yc" class="form-control">
<option value="" selected>Please Select</option>
<option value="yc1">yc1</option>
<option value="yc2">yc2</option>
</select>
</div>
<div class="col-md-5">
<strong>SC</strong>
<select name="sc" class="form-control">
<option value="" selected>Please Select</option>
<option value="Z01">Z01</option>
<option value="Z02">Z02</option>
<option value="Z03">Z03</option>
</select>
</div>
<div class="col-md-2">
<br/>
<button type="submit" class="btn btn-success">Search</button>
</div>
</form>
result.blade.php
<div class='list-group gallery'>
#if($images->count())
#foreach($images as $image)
<div class='col-sm-4 col-xs-6 col-md-3 col-lg-3'>
<a class="thumbnail fancybox" rel="ligthbox" href="/images/{{ $image->image }}">
<img class="img-responsive" alt="" src="/images/{{ $image->image }}" />
<div class='text-center'>
<small class='text-muted'></small>
</div> <!-- text-center / end -->
</a>
</div> <!-- col-6 / end -->
#endforeach
#endif
</div> <!-- list-group / end -->
ImageGalleryController.php
public function search()
{
$images = \App\ImageGallery::get();
return view('search', compact('images'));
}
public function order(Request $request)
{
$data = $request->all();
$images = \App\ImageGallery::when($data['type'], function ($query, $type) {
return $query->where('type', $type);
})->
when($data['fc'], function ($query, $fc) {
return $query->orWhere('fc', $fc);
})->
when($data['yc'], function ($query, $yc) {
return $query->orWhere('yc', $yc);
})->
when($data['wc'], function ($query, $wc) {
return $query->orWhere('wc', $wc);
})->
when($data['sc'], function ($query, $sc) {
return $query->orWhere('sc', $sc);
})
->get();
return view('result', compact('images'));
}
This is the database structure:
This is the full working code on my local system.
This is web.php file
Route::get ('search', 'ImageGalleryController#search');
Route::post('search', 'ImageGalleryController#order')->name('search');
Add csrf token in your form.
#csrf
Change order function as:
public function order(Request $request) {
$data = $request->all();
$images = ImageGallery::where([
['type', $data['type']],
['wc',$data['type']],
['fc',$data['fc']],
['yc',$data['yc']],
['sc',$data['sc']]
])->get();
return view('result', compact('images'));
}
Give name method to route:
Route::post('search', 'ImageGalleryController#order')->name('search');
You have to change something. Because your $data is an array. But you have declare as an object. For this reason you get this error.
$data = $request->all();
$images = ImageGallery::where(['type'=>$data['type']],
['wc'=>$data['type']],
['fc'=>$data['fc']],
['yc'=>$data['yc']],
['sc'=>$data['sc']])->get();

how to convert select box html to form html collactive

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-->

Resources