The element I want to pass to view:
<option value="UK">UK</option>
<option value="Mexico">Mexico</option>
//Something like this...
Part of my code:
foreach($data as $row)
{
$output .= '<option value='."$row->Country".'>'.$row->Country.'</option>';
}
It will return:
{"table_data":"German<\/option>Mexico<\/option>Mexico<\/option>UK<\/option>Brazil<\/option>UK<\/option>","total_data":6}
So, It can only print the name between the , but can not print out the value inside the open tag.
How can I solve this?
why not you just do it like this:
in the controller
public function country(){
$countries=Country::all();
return view('country',compact('countries'));
}
in the blade view
<select>
#foreach($contries as $country)
<option value="{{$country->name}}">{{$country->name}}</option>
#endforeach
</select>
Please Check This In your Controller passing the countries
public function country(){
$countries=Country::all();
return view('your_blade_file',compact('countries'));
}
In your blade option value is $country->id option show it $country->name
<select>
#foreach($contries as $country)
<option value="{{$country->id}}">{{$country->name}}</option>
#endforeach
</select>
Related
i have this situation.
Undefined variable: city (View: \newjoblist\resources\views\components\hero.blade.php)
in my controller i have
<?php
namespace App\Http\Controllers;
use App\Models\City;
use Illuminate\Http\Request;
class CitiesController extends Controller
{
public function index(Request $request){
$city = City::orderBy('name') // variable displayed in view
->get();
// return view('components.hero')->with('city',$city);
$this->view('<components.hero')->with('city', $this->city);
}
}
and in components/hero.blade.php
<select class="form-group" id="s" name="s">
<option value=""></option>
#foreach ($city as $cities)
<option value="{{$cities}}">{{$cities->name}}</option>
#endforeach
</select>
i don't know if i need to do something in web.php
you need to update your index method to
public function index(Request $request){
$cities = City::orderBy('name') // variable displayed in view
->get();
return view('components.hero')->with('cities',$cities);
}
and at the view
<select class="form-group" id="s" name="s">
<option value=""></option>
#foreach ($cities as $city)
<option value="{{$city->id}}">{{$city->name}}</option>
#endforeach
</select>
You can also do this
public function index(Request $request)
{
$cities = City::orderBy('name') // variable displayed in view
->get();
return view('components.hero', compact('cities'));
}
You pass in your code city as class membervariable but previously you assign only to $city.
Change this line: $this->view('<components.hero')->with('city', $this->city); to view('components.hero')->with('city',$city); . What you already had a line above ;-)
You can also pass Vars to your blade like that:
view('components.hero', ['city' => $city];
I read the comment i find out that you are trying another way to get the data.
So you can do it like this.
<?php
namespace App\Http\Controllers;
use App\Models\City;
use Illuminate\Http\Request;
class CitiesController extends Controller
{
private $city;
public function __construct() {
$this->city = new City();
}
public function index(Request $request) {
$cities = $this->city->query()->orderBy('name')->get();
return view('<components.hero')->with('cities', $cities);
}
}
And in the view:
<select class="form-group" id="s" name="s">
<option value=""></option>
#foreach ($cities as $city)
<option value="{{ $city->id }}">{{ $city->name }}</option>
#endforeach
</select>
is it possible to continue the error because I want to display in components.hero where is another controller? this is my web.php....
Route::get('/', [Controllers\ListingController::class, 'index']) ->name('listings.index');
and i have others views in this controller
UPDATE*
if I use diff. the route in web.php it's works
Route::get('/search', [Controllers\CitiesController::class, 'index']);
-> displaying cities from DB
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();
}
I need help with my dropdown. Currently the dropdown display all options from database. However, what I want it to display option based on the user id only. For example, this form is for Urban Seafood, suppose the dropdown will only display Urban Seafood option instead of showing all options. Below is the controller and view blade. Thank you.
Adjustment Controller:
public function create() {
$restorants = Restorant::where(['active' => 1])->get();
return view('ingredient::adjustments.create', compact('restorants'));
}
View:
<select class="form-control select2" name="restorant_id">
<option disabled selected value> -- {{ __('Select an option') }} -- </option>
#foreach ($restorants as $restorant)
<option <?php if(isset($_GET['restorant_id'])&&$_GET['restorant_id'].""==$restorant->id.""){echo "selected";} ?> value="{{ $restorant->id }}">{{$restorant->name}}</option>
#endforeach
</select>
You need the Auth facade
use Illuminate\Support\Facades\Auth;
// ...
public function create() {
$restorants = Restorant::where('active', 1)->where('user_id', Auth::id())->get();
return view('ingredient::adjustments.create', compact('restorants'));
}
<select name="sort-posts" id="sortbox" name="sortbox">
<option value="" disabled>Sort by</option>
<?php
$categories = get_categories(['exclude' => 1]);
foreach ($categories as $category) {
echo "<option>$category->name </option>";
}
?>
<option>Newest</option>
<option>Oldest</option>
<option>Title Asc</option>
<option>Title Desc</option>
</select>
And I want to get the value of the selected option without redirecting or using form, for me to use it in function.
How can I do that?
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'));}