Old Value/Search Value on search select button in Laravel - html

i make a select button for search specific data on my table.
but there is problem when i click the button, the button doesn't remain or stay from past request value that i click, or it reset back to select all
so this is my form in my blade file
<form action="">
<select class="form-select" name="rumah_id" id="rumah_id">
<option value="">Select All</option>
#foreach($rumahs as $rumah)
#if(old('rumah_id') == $rumah->id)
<option value="{{ $rumah->id }}">{{ $rumah->alamat }}</option>
#else
<option value="{{ $rumah->id }}">{{ $rumah->alamat }}</option>
#endif
#endforeach
</select>
</form>
this is my Controller
public function index(Request $request){
$rumah = Rumah::all();
$mobil = Mobil::with(['rumah'])->orderBy('rumah_id', 'asc')
->when($request->rumah_id != null, function($q) use($request){
return $q->where('rumah_id', 'like', '%'. $request->rumah_id. '%');
})
return view('mobil/index',[
'mobils' => $mobil,
'rumahs' => $rumah,
]);
}
also this is my migration database for rumah look like
Schema::create('rumahs', function (Blueprint $table) {
$table->id();
$table->string('alamat');
$table->timestamp('published_at')->nullable();
$table->timestamps();
});

Try this :
<select class="form-select" name="rumah_id" id="rumah_id">
<option value="">Select All</option>
#foreach($rumahs as $rumah)
<option value="{{ $rumah->id }}" {{ old('rumah_id') == $rumah->id ? 'selected' : '' }}>{{ $rumah->alamat }}</option>
#endforeach
</select>

Related

laravel retrive old() wherein query values

I have 2 multiple value select lists that I pass users between them using jquery.
<select multiple="multiple" id="ListAllUsers" class="form-control">
#foreach ($users as $user)
<option class="content" value="{{ $user->id }}">{{ $user->name }}</option>
#endforeach
</select>
<select name="user_id[]" required multiple="multiple" id="SelectedUsers" class="form-control">
#if (!empty(old('user_id')) && $create_form)
#foreach ($old_value_users as $old_user)
<option class="content" value="{{ $old_user->id }}">{{ $old_user->name }}</option>
#endforeach
#endif
</select>
Now, my issue is that when I go and pass the old('user_id') value in the whereIn query, I only get the first result of the array instead of all of the results that are passed in.
$old_users_id = [old('user_id')];
$old_value_users = User::whereIn('id', $old_users_id)->get();
If I replace $old_users_id = [old('user_id')] with $old_users_id = [1,2,3], it works like a charm. I have dd(old('user_id')) and it is indeed an array, so I do not understand what is the hick-up!
Since old('user_id') is an array, you should not put it in an array. Try this;
You shold provide an empty array if old('user_id') is null.
$old_users_id = old('user_id')?old('user_id'):[];
$old_value_users = User::whereIn('id', $old_users_id)->get();
+ point
Instead of whereIn try with whereIntegerInRaw if you are adding a large array of integer bindings to your query.For more check documentation
Something like this one
<select class="custom-select chosen-select {{ $errors -> has('add_project_owner') ? 'is-invalid' : '' }}" name="add_project_owner[]" id="add_project_owner" multiple>
<option value="">choose</option>
#foreach ($owners as $owner)
<option value="{{ $owner -> emp_name }}" {{ (collect(old('add_project_owner'))->contains($owner -> emp_name)) ? 'selected' : '' }}>{{ $owner -> emp_name }}</option>
#endforeach
</select>

populate data from database into select dropbox

I have a select box which needs to fetch from the database.
view blade:
<select class="form-control" name="service_type" id="service_type" data-parsley-required="true">
#foreach ($service as $sn)
{
<option value="">dd($sn)</option>
}
#endforeach
</select>
controller:
public function index()
{
$service = DB::select('select service_type from services');
return view("service",['service'=>$service]);
}
I am getting an error like
Undefined variable: service (View: /home/devadmin/.config/composer/vendor/laravel/installer/serenseprj/resources/views/package.blade.php)
You can use Eloquent instead of doing this
$service = DB::select('select service_type from services');
Using Eloquent,
$services = ModelName::pluck('name', 'id')->get();
You can pass the variable to the targeted view like below:
return view('view_name', ['variable_name' => $variable]);
And, you can access the variable name like below
<select class="form-control" name="service_type" id="service_type" data-parsley-required="true">
#foreach ($variable_name as $var)
{
<option value="{{$var->id}}">{{$var->name}}</option>
}
#endforeach
</select>
You can use this code as the references. Hope you have service model.
$services= Service::pluck('name', 'id');
return view('services', compact('services'));
then in view file you can use laravel collevtives like.
<div class="form-group col-sm-6">
{!! Form::label('service_id', 'service Id:') !!}
{!! Form::select('service_id', $services, null, ['class' => 'form-control'])!!}
</div>
you can use this too. in you blade file
<select name="service " id="service " class="form-control">
<option value=""> -- Select One --</option>
#foreach ($services as $service )
<option value="{{ $service->id }}">{{ $service->name }}</option>
#endforeach

How can I add a url to a select input's option?

Im trying to create a liste box with each of the item send to a different profile
I have my variable $ru who get all the users it's working .
I tryed this but it's not working:
<select class="chosen form-control" name="User" style="height:28px" >
<option selected="selected"> Rechercher </option>
#foreach($ru as $user)
<option value="/profile/$user->id ">
{{$user->name}}
</option>
#endforeach
</select>
Cant add an href like that just add it to the value and use an event listener:
Add the jquery link to you script tag, replace my url and value with yours.
$(document).ready(function(){
$('#user').change(function(){
window.location = this.value;
});
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<select class="chosen form-control" name="User" id="user">
<option>Select profile</option>
<option value="http://www.google.com"> user name </option>
</select>
source : stackoverflow
href Link is not possible in select / option. You can use jQuery to redirect to the profile selected.
Following is the example:
<select class="chosen form-control" name="User" style="height:28px" >
<option selected="selected">
Rechercher
</option>
#foreach($ru as $user)
<option value="{{ url('/profile/'.$user->id) }}">{{$user->name}}
</option>
#endforeach
</select>
<script>
$(document).ready(function(){
$('[name="User"]').change(function(){
var profile_url = $('[name="User"]').value();
// redirect to user profile
if(profile_url != "")
window.location.href = profile_url;
});
});
</script>
try this
<select required class="form-control" id="user" name="User" onchange="getUser()" required>
<option value="All">--All--</option>
#foreach($ru as $user)
<option value="{{ $user->id }}" {{ (old('user')== $user->id||($user) == $user->id) ? "selected" : "" }}>{{$user->name}}</option>
#endforeach
</select>
//javascript
function getUser()
{
var t= $('#user').val();
if(t!= "" ){
location.href="/profile/"+t;
}
}

Auto filter without using button and ajax in Laravel

I have 3 tables:
Type: id, name
Level: id, name
Question: id, content, type_id, level_id
I've done filter function with "Filter" button. But now, what I want is that when I filter, data will be loaded into table automatically without using button. How can I do?
Here is my code using button
FilterController:
public function filter(Request $request){
$types = Type::all();
$levels = Level::all();
$model = Question::where('id', '>', 0);
if (isset($request->type))
$model = $model->where('type_id', $request->type);
if (isset($request->level))
$model = $model->where('level_id', $request->level);
$data = $model->paginate(999)->appends(request()->query());
return view('filter', compact( 'data', 'request', 'types','levels'));
}
filter.blade.php
<form method="get">
<select name="type">
<option value="">All Types</option>
#foreach ($types as $item)
<option value="{{ $item->id }}" #if ($request->type == $item->id) selected #endif>{{ $item->name }}</option>
#endforeach
</select>
<select name="level">
<option value="">All Levels</option>
#foreach ($levels as $item)
<option value="{{ $item->id }}" #if ($request->level == $item->id) selected #endif>{{ $item->name }}</option>
#endforeach
</select>
<button type="submit" class="btn btn-primary">Filter</button>
</form>
<table border="1">
<tr>
<th>ID</th>
<th>Content</th>
</tr>
#foreach ($data as $q)
<tr>
<td>{{ $q->id }}</td>
<td>{{ $q->content }}</td>
</tr>
#endforeach
</table>
You could simply submit the form when your select values change:
<select onchange="this.form.submit()">
...
</select>
Or if you didn’t want to use a form, assuming your path is /questions and you've set the APP_URL property in your .env file, you could do something similar to the following:
<select name="type" onchange="window.location.href = this.value">
<option value="{{ url('questions') }}">All types</option>
#foreach($types as $type)
<option value="{{ url('questions') }}?type={{ $type->id }}"{{ request('type') == $type->id ? ' selected' : '' }}>
{{ $type->name }}
</option>
#endforeach
</select>
If you want the currently selected level to stay selected when you filter by type, you could change your option value to the following:
<option value="{{ url('questions') . '?' . (request()->filled('level') ? 'level=' . request('level') . '&' : '') . 'type=' . request('type') }}">
{{ $type->name }}
</option>
Answered on my phone so there may be broken syntax.

Select all selected id's in a multiselect dropdown in laravel 5.4 with harvest chosen

I have a dropdown called designation, where a user can add some information against multiple designation. If I add 1 record against 3 designation, then I need to select those during validation and edit time also.
Ex: Choosed id's {5,7,8} from [1 to 10].
<select id="forWhom" name="forWhom[]" multiple class="form-control chosen">
<option value="">--- Select ---</option>
#foreach ($desgInfo as $key => $value)
<option value="{{ $key }}" {{ old('forWhom',$info->forWhom) == $key ? 'selected' : ''}} />{{ $value }}</option>
#endforeach
</select>
After add of those information I store those selected id's in comma(,) separator i.e 5,7,8.
How can I make select this in laravel 5.4
After playing around a bit, I got the result.
Here is the piece of code.
During Add
<select id="forWhom" name="forWhom[]" multiple class="form-control chosen">
<option value="">--- Select ---</option>
#foreach ($desgInfo as $key => $value)
<option value="{{ $key }}"
{{ (collect(old('forWhom'))->contains($key)) ? 'selected':'' }} />
{{ $value }}
</option>
#endforeach
</select>
During Edit
Suppose you got the result of selected ids in
$info->forWhom
<select id="forWhom" name="forWhom[]" multiple class="form-control chosen">
<option value="">--- Select ---</option>
#foreach ($desgInfo as $key => $value)
<option value="{{ $key }}"
{{ (collect(old('forWhom'))->contains($key)) ? 'selected':'' }}
{{ (in_array($key,$info->forWhom)) ? 'selected' : ''}}
/>
{{ $value }}
</option>
#endforeach
</select>
I hope this will help some one else.