Adminlte multi select searching display in laravel - laravel

I am using Adminlte 3 Multiple (.select2-purple) in laravel 6, and I got a error like this
I don't know why searching... is showing
Here's my code:
VIEW
<div class="col-md">
<div class="form-group">
<label>Technician</label>
<div class="select2-purple">
<select class="select2" multiple="multiple" data-placeholder="Select a Technician" data-
dropdown-css-class="select2-purple" style="width: 100%;">
#foreach($stations as $station)
<option id="{{ $station->id }}">{{ $station->name }}</option>
#endforeach
</select>
</div>
</div>
</div>
<script>
$('.select2').select2()
</script>
Controller
public function totaltestperform(){
$stations = \DB::table('stations')
->select('id','station')
->orderby('id')
->get();
return view("analytic/total_test_perform",compact('stations'));
}
Here's the output of station variable when I use dd($stations);

Wrong: <option **id="{{ $station->id }}"**>{{ $station->name }}</option>
Use: <option value="{{ $station->id }}">{{ $station->name }}</option>

Related

Show phone code of a country selected in dropdown Laravel

I'm trying to show the phone code based on the country selected. Like when you try to register and while choosing a country the phone code changes at the same time.
here's my code.
<select class="form-control" name="country_id" required>
<option value selected disabled>Select Country</option>
#foreach ($countries as $country)
<option value="{{ $country->id }}" id="shop-country">{{ $country->name }}</option>
#endforeach
</select>
<div class="form-group">
<label>Phone Number</label>
#foreach ($countries as $country)
<span id="phonecode">{{ $country->phonecode }}</span>
#endforeach
</div>
First, you need to populate the select options with a custom attribute, for example, phonecode
<select id="countryList" class="form-control" name="country_id" required>
#foreach ($countries as $country)
<option phonecode="{{ $country->phonecode }}"
value="{{ $country->id }}"
id="shop-country">{{ $country->name }}
</option>
#endforeach
</select>
Remove foreach loop from pone code text.
<div class="form-group">
<label>Phone Number</label>
<span id="phonecode"></span>
</div>
Now, use Javascript to listen to change events on the select list.
<script>
let countryList = document.getElementById("countryList") //select list with id countryList
let phoneCode = document.getElementById('phonecode') //span with id phonecode
countryList.addEventListener('change', function(){
phoneCode.textContent = this.options[this.selectedIndex].getAttribute("phonecode");
});
</script>

topic.blade.php cannot fetch value->id, but it works with value->name

I got the code snippet,
<div class="form-group">
<label for="category-id-field">Category</label>
<select class="form-control" name="category_id" id="category-id-field">
<option value="" hidden disabled {{ $topic->id ? '' : 'selected' }}>Choose your category</option>
#foreach ($categories as $value)
#php
$selected = $topic->category_id === $value->id ? 'selected' : '';
#endphp
<option value="{{ $value->id }}" {{ $selected }}>{{ $value->name }}</option>
#endforeach
</select>
</div>
And it shows like below, as you can see $value->name works but $value->id just shows up zero.
<div class="form-group"><label for="category-id-field">Category</label>
<select name="category_id" id="category-id-field" class="form-control">
<option value="" selected="selected">Choose your category</option>
<option value="0">statement</option>
<option value="0">life</option>
<option value="0">work</option>
<option value="0">study</option>
</select>
</div>
Versions:
laravel/framework: "^7.0"
Platform: macos catalina
mysql: 8.0.19
Some more background:
Things I did, the id in the categories table used to be integer, I changed it to VARCHAR(30) and added it as the foreign key to table topics->category_id.
If anyone happens to have some experience on this one, please point out the error. Many thanks.

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();

Get input value and pass it to route in same blade file Laravel 5.7

I want to pass selected value from dropdown to be passed throw routing from the same blade file
Here is my blade code :
<div class="form-group col-md-2">
<label>#lang('site.son')</label>
#if(Auth::user()->shortsign == '--')
<select class="select2-size-lg form-control border-primary" style="width: 100%;" name="sign">
#foreach ($users as $user)
<option value="{{ $user->shortsign }}">{{ $user->shortsign }}</option>
#endforeach
</select>
#else
<select class="select2-size-lg form-control border-primary" style="width: 100%;" name="sign" readonly>
<option value="{{ Auth::user()->shortsign }}">{{ Auth::user()->shortsign }}</option>
</select>
#endif
</div>
<div class="form-actions right">
<i class="la la-search" aria-hidden="true"></i> #lang('site.search')
</div>
Now the code iam using pass the last value from database not selected value
you can try this by giving id to your anchor tag
<script type="text/javascript">
$('.select2-size-lg').change(function () {
var id = $(this).val();
document.getElementById("abc").href="{{ url('/salereports/report/' . id) }}";
});
</script>

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();
}
});

Resources