Laravel: Let user select MODEL and then get query - php

I am working on a Laravel project, which should be somewhat modular.
I have written a search query and what I want is following:
Models:
Assessment A
Assessment B
Assessment C
View:
{{ Form::open(array('route' => 'search')) }}
<select multiple name="search_by_form[]">
<option selected>Any</option>
<option value="assessment_a">A</option>
<option value="assessment_b">B</option>
<option value="assessment_c">C</option>
</select>
{{ Form::close() }}
Controller:
The controller is much more complex but I made it easier for you to understand what my need is.
Now something like follows:
$user_entry =$request->input('search_by_form', []);
$resultArray = [];
foreach ($user_entry as $entry){
if ($entry == "assessment_a"){
$model_results = AssessmentA::where("approved",1)->get();
} elseif ($entry == "assessment_b"){
$model_results = AssessmentB::where("approved",1)->get();
} elseif($entry == "assessment_c"){
$model_results = AssessmentC::where("approved",1)->get();
}
}
What I want:
SearchViewController:
foreach through Models in specific folder
then:
{{ Form::open(array('route' => 'search')) }}
<select multiple name="search_by_form[]">
<option selected>Any</option>
#foreach($models as $model)
<option value="{{ $model }}">{{ $model->name }}</option>
#endforeach
</select>
{{ Form::close() }}
Instead of having such an ugly approach is it possible to have more something like this:
foreach ($user_entry as $entry){
$entry::where("approved",1)->get(); #Obviously the values on the view would be change to "AssessmentA", etc.
}
I this feasible?
Thank in advance!

I think you will create any array like this:
$model_array = ['assessment_a' => AssessmentA::class];
Then in loop simply pass value
foreach ($user_entry as $entry){
$model_array[$entry]::where("approved",1)->get(); #Obviously the values on the view would be change to "AssessmentA", etc.
}

If you change the select values to the actual table names, you could do it easily
{{ Form::open(array('route' => 'search')) }}
<select multiple name="search_by_form[]">
<option selected>Any</option>
<option value="assessments_a">A</option>
<option value="assessments_b">B</option>
<option value="assessments_c">C</option>
</select>
{{ Form::close() }}
$user_entry = $request->input('search_by_form', []);
if (empty($user_entry)) {
model_results = collect();
} else {
$query = DB::table(array_shift($user_entry))->where('approved', 1);
foreach ($user_entry as $table) {
$query->union(fn($union) => $union->from($table)->where('approved', 1));
}
$model_results = $query->get();
}

Related

Getting data out of array separately in laravel

I'm using shipping system where will calculate shipping price.
this is what i get in my blade currently:
As you see I get id of state and then name of state.
here is my function:
public function index() {
$data = RajaOngkir::Provinsi()->all();
return view('welcome', compact('data'));
}
and this is my blade code:
#foreach($data as $sss)
#foreach($sss as $numbers)
{{ $numbers }} <br>
#endforeach
#endforeach
here is {{$data}} dump info:
what I want is to getting id and name separately so I can use it
in input.
The foreach will loop through anyhow and pull the relevant info. You just then show it within your blade like so:
#foreach ($data as $info)
ID: {{ $info->province_id }} <br />
Name: {{ $info->province }}
#endforeach
Updated:
From my re read you want it as a form drop down select so you can calculate shipping fee's therefore you'd simply do:
<select name="province" id="">
<option value="">Select</option> -> This will be the default select option
#foreach ($data as $info)
<option value="{{ $info->province_id }}">{{ $info->province }}</option>
#endforeach
</select>

Laravel | custom collection for select2 optgroup and option

Using select 2 I want to do something like this (but this syntax is not quite right):
#foreach($courseCategories as $courseCategory)
<select>
#foreach($courseCategory->courseNames as $courseName)
<optgroup label="{{ $courseCategory }}">
<option value="{{ $courseName }}">
{{ $courseName }}
</option>
</optgroup>
</select>
#endforeach
My Model Structure is quite like this:
In my controller:
public function create()
{
$courses = Course::all();
return View::make('college.create', compact('courses'));
}
I need to map courseCategory => optgroup's label and respective course name => option
How can I create a custom collection to faciliate this? and How can I use the collection (I'm guessing using foreach) to output values in my view file?
There a problem in your foreach :
<select>
#foreach($courseCategories as $courseCategory)
#foreach($courseCategory->courseNames as $courseName)
<optgroup label="{{ $courseCategory }}">
<option value="{{ $courseName }}">
{{ $courseName }}
</option>
</optgroup>
#endforeach
#endforeach
</select>

How to keep value of selected option after form is submitted in Laravel form

I want to make when user search by country on the page refresh eg. result page to keep selected country on the option on the form.
This is what I have where I populate the dropdown
#foreach ($countries as $country)
<option value="{!! $country->id !!}">{!! $country->name !!}</option>
#endforeach
And this is what I have tried but seems that is not correct since the value isn't selected
#foreach ($countries as $country)
<option value="{!! $country->id !!}" #if( old('country->id') == $country->id) selected="selected" #endif>{!! $country->name !!}</option>
#endforeach
What's the trick here?
Here is the controller
public function search(Request $request)
{
$searchText = strip_tags($request['q']);
$seachLocation = strip_tags($request['l']);
$columns =['alias','description','address1','address2','address3'];
$query = Item::select('*');
$query->where( 'title', 'like', '%'.$searchText.'%');
foreach( $columns as $column) {
$query->orWhere( $column, 'like', '%'.$searchText.'%', '%'.$seachLocation.'%');
}
$query->orWhereHas('category',function( $query ) use ( $searchText ) {
$query->where('title', 'like', '%'.$searchText.'%' );
});
$query->orWhereHas('country',function( $query ) use ( $searchText ) {
$query->where('name', 'like', '%'.$searchText.'%' );
});
$items = $query->paginate(5);
$searchQuery = $searchText;
$searchQueryLoc = $seachLocation;
return view('search', compact('items','searchQuery','seachLocation'));
}
To use the old() helper, your form inputs need to have a name.
You then use the name of the input as the parameter in the old() helper to get the old value.
So using it like old('country->id') won't work; you need to use old('nameAttributeOfInput').
A full example assuming the name attribute of this input is country_id:
#foreach ($countries as $country)
<option value="{!! $country->id !!}" #if( old('country_id') == $country->id) selected="selected" #endif>{!! $country->name !!}</option>
#endforeach
As a side note, just be aware that {!! !!} does not escape the value being placed on the page which can be potentially dangerous. You would probably be just as fine, and safer, to use {{ }} which escapes the value.
I would suggest that you use session to store the selected country. For example:
session()->put('forms.country', $request->get('country'));
Then your html should become:
<option value="{{ $country->id }}" #if( session('forms.country') == $country->id) selected="selected" #endif>{{ $country->name }}</option>
You can use Laravel inbuilt old() in form's field to hold the provided value.
#if( old('field_name') == $country->id) selected="selected" #endif

Populate dropdown in laravel with more than 2 attributes

So far I know how to populate dropdown using Eloquent (and pluck) is this
public function create()
{
$items = Item::all()->pluck('name', 'id');
return view('admin.item.create', compact('items'));
}
And with this
{!! Form::select('item_id', $items, null, ['class' => 'form-control']) !!}
I can generate this
<select name="items" class="form-control">
<option value="1">Item 1</option>
<option value="2">Item 2</option>
<option value="3">Item 3</option>
</select>
But now I want to create something like this
<select name="items" class="form-control">
<option value="1">Item 1 - Location</option>
<option value="2">Item 2 - Location</option>
<option value="3">Item 3 - Location</option>
</select>
I can only use pluck with 2 attributes. But now I want it to display 3 attributes. How do I do that? I'm using Laravel 5.3 by the way. Thanks for the help.
You have an array like [1=> "something",2=>"else"]. So you task is to either iterate the array and edit the values or select the the locations from the database and the use foreach or array_filter
So something like the following:
$items=[];
foreach(Item::all() as $item){
$items[$item->id] = $item->name . " - " . $item->location->name; // build string to display here
}
Edit:
Your comment said location is at the same table so it would look like...
$items=[];
foreach(Item::all() as $item){
$items[$item->id] = $item->name . " - " . $item->location; // build string to display here
}
Pluck location also
public function create()
{
$items = Item::all()->pluck('name', 'id', 'location');
return view('admin.item.create', compact('items'));
}
Generate like this
<select name="items" class="form-control">
#foreach($items as $item)
<option value="{{ $item->id }}">{{ $item->name }} - {{ $item->location }}</option>
#endforeach
</select>
Best way to create list of array is.
public function create(){
$items = Item::where('id','<>','')->lists('name', 'id');
return view('admin.item.create', compact('items'));}

Repopulate Laravel Input::old() on Form::select

I can't manage to repopulate a Select field input after a query, using Laravel 4:
// Route
Route::get('blog', 'BlogController#getPosts');
// BlogController
public function getPosts()
{
$posts = Post::where('category_id', Input::get('category'))->paginate(25);
$categories = Category::lists('title', 'id');
return View::make('blog', compact('categories', 'posts'));
}
// Blog view
{{ Form::open('method' => 'get', 'id' => 'form-search') }}
{{ Form::select('category', $categories, Input::old('category')) }}
{{ Form::close() }}
I managed to make it work this way, but it's not the best practice
<select name="category" id="category">
<option value="1" {{ (Input::get('category') == 1) ? 'selected="selected"' : null }}>Category 1</option>
<option value="2" {{ (Input::get('category') == 2) ? 'selected="selected"' : null }}>Category 2</option>
<option value="3" {{ (Input::get('category') == 3) ? 'selected="selected"' : null }}>Category 3</option>
</select>
I think the Input::old('category') doesn't work because it is a GET request, am I right? Is there any workarounds?
Update : I finally made it work using Input::get() instead of Input::old() :
{{ Form::select('category', $categories, Input::get('category')) }}
It seems you're not even retrieving the old input, you will need to pass it to the view. You can do that in one of two ways, the easiest and best to understand is to just specify that you want to pass input.
return View::make('blog', compact('categories', 'posts'))->withInput();
Also, you don't need the markup in your HTML. Laravel will do this for you if you just give it the value of the old Input. It works very well.
Perhaps this will work
public function getPosts()
{
$category_id = Input::get('category');
$posts = Post::where('category_id', $category_id)->paginate(25);
$categories = Category::lists('title', 'id');
return View::make('blog', compact('categories', 'posts', 'category_id'));
}
// Blog view
{{ Form::open('method' => 'get', 'id' => 'form-search') }}
{{ Form::select('category', $categories, $category_id) }}
{{ Form::close() }}

Categories