using Laravel 5.6.28 my route on web.php file looks like this:
Route::get('kitysoftware/besttrades', 'SectorsController#besttradesview');
My controller with bestrasdeview function:
class SectorsController extends Controller
{
public function besttradesview()
{
$sectors = DB::table('Sectors')->get();
foreach ($sectors as $sector) {
echo $sector->SectorName;
}
//passing variable sectors1 to my view (is = $sector)
return view('besttradesview', ['sectors1' => $sector]);
}}
My view Bestradesview.blade.php is:
<form method="GET">
<div class="selectsector">
<Select class="selectsector" name = "sectors">
<option value="{{ sectors1 }}"></option>
<!-- test#2: <option value="{{ $sectors1->sector[] }}"></option> -->
<!-- test#3: <option value="{{ $sectors1->sector }}"></option> -->
</form>
</select>
And i get this error: Use of undefined constant sectors1 - assumed
'sectors1' (this will throw an Error in a future version of PHP)
When testing #2 commented line instead i get another error: Symfony \ Component \ Debug \ Exception \ FatalErrorException (E_UNKNOWN)
Cannot use [] for reading
When testing #3 commented line without [] i get another error: Use of undefined constant sectors1 - assumed 'sectors1' (this will throw an Error in a future version of PHP)
Probably it's pretty simple but it's driving me nuts because i can't see why the variable is not passing to the view.
I know it's reading my table because if i remove the last return view call line on my controller, it echoes out all the values from my SectorName column, but i want it on a select dropdown menu.
I have been reading the docs, forums and watching laracast videos without luck. Any insight or just pointing me out to where to learn the proper sintax solution will be appreciatted.
Thanks in advance
It looks like you're trying to populate the select options from the query. If that's the case, your loop is in the wrong place.
Take the loop out of the controller method, and pass the $sectors collection directly to the view.
public function besttradesview()
{
$sectors = DB::table('Sectors')->get();
return view('besttradesview', ['sectors1' => $sectors]);
}
Then loop in the view to output the options.
<select class="selectsector" name="sectors">
#foreach($sectors1 as $sector)
<option>{{ $sector->SectorName }}</option>
#endforeach
</select>
I assumed you wanted some text in the option. If you use the sector name as the option text it will also be used as the option value by default. If you want to use something else for the value, it would be like this, for example:
<option value="{{ $sector->id }}">{{ $sector->SectorName }}</option>
Change,
return view('besttradesview', ['sectors1' => $sector]);
To,
return view('besttradesview', ['sectors1' => $sectors]);
Notice the s in $sectors ($sectors = DB::table('Sectors')->get();) and in the actual view, it should be $sectors1 not just sectors1.
You also need to loop through actual sector:
#foreach ($sectors1 as $sector)
<option>{{ $sector->SectorName }}</option>
#endforeach
Also, I would definitely suggest creating a model for your Sectors table rather than using the DB:: facade. You should make use of the M in MVC.
Related
So, I have an assistant and enfant tables. I made a dropdown list to affect an assistant from DB while the registration of an enfant. I don't know whats wrong I keep getting this error "Type error: Too few arguments to function App\Http\Controllers\EnfantsController::create(), 0 passed and exactly 1 expected". So this is my Enfants Controller :
public function create($nom)
{
$assistants =Assistant::all();
return view('enfants.create' ,['ass'=>$assistants, 'nom'=>$nom]);
}
And this is my create.blade.php :
<select class="form-control select2" id="ass_name" name="ass_name" required>
#foreach($ass as $value)
<option value="{{ $value->nom }}">{{ $value->nom }}</option>
#endforeach
</select>
And, in the option values , I want to have the name of the assistant and then get the id of the selected name and add it to the foreign key "assistant_id" in my enfant table. I hope you get what i want to do, i'm new to Laravel. Any help will be appreciated.
The issue is in your EnfantsController#create method. As you can see its expecting an argument which is a query param that you mention in your routes file. You need to pass value for $nom parameter when you make call to the create method.
My dynamic drop down list is not working when I am trying to navigate to update page by using url id and then its showing errors.The result suppose to be a name of city that is associated with id in the database.
controller class
public function updateAddress($edit_id)
{
$data=[];
$data['edit_id']=$edit_id;
$address_data = $this->address->find($edit_id);
$data['first_name']=$address_data->first_name;
$data['last_name']=$address_data->last_name;
$data['street']=$address_data->street;
$data['zip_code']=$address_data->zip_code;
$data['city']=$address_data->city;
return view('update/edit',$data);
}
Here is my blade file. The rest of the fields are working properly except drop down list. I am using foreach loop to display data into the drop down list.
<select id="city" name="city" >
#foreach($data as $cities)
<option id="city" value="{{$cities->edit_id}}" selected="selected">{{$cities->city}}</option>
#endforeach
</select>
Update the code in the controller so that:
return view('update/edit',$data);
Is changed to:
return view('update/edit', ['data' => $data]);
change your controller code to
return view('update/edit',compact('data'));
and change your view page like
{{ $data['edit_id'] }}. you dont want to use #foreach.
Here is part of my controller;
$agreements = Agreement::all();
print_r($agreements) //this works and displays an object/array with the details!
return View::make('individual_agreements.create')
->with('individual', $individual)
->with('agreements', $agreements)
->with('content_title', 'Create new individual agreement');
Here is part of my view;
<div class="form-group">
{{ Form::label('agreement_name', 'Agreement name') }}
{{ Form::select('agreement_name', $agreements->agreement_name) }}
</div>
I have a field called agreement_name in the table.. All I want to do is turn that in to a dropdown in the most efficient Laravel/Eloquent way as possible.
I keep getting the standard "agreement_name not defined" error and I cannot find a suitable example anywhere online having looked for over an hour now.
If you need the agreements for something else on the same page try
<div class="form-group">
{{ Form::label('agreement_name', 'Agreement name') }}
{{ Form::select('agreement_name', $agreements->lists('agreement_name', 'agreement_name')) }}
</div>
This will get all the agreements names within your agreements collection and allow them to be used within the select drop down. First parameter is the column and second is the key.
Else just change your query to;
$agreements = Agreement::lists('agreement_name', 'id');
You should do this
$agreements = Agreement::lists('agreement_name', 'id');
all return you all columns in table, so that's why your code wont work
The way you have this set up allows for the use of a foreach loop to list through and display results:
<select name="agreement_name" class="">
<option value="">- Select -</option>
#foreach($agreements AS $agreement)
<option value="{{ $agreement->id }}">{{ $agreement->agreement_name }}</option>
#endforeach
</select>
Since your $agreements is an array of elements, calling $agreements->agreement_name wouldn't work, while $agreements[0]->agreement_name probably would. If you want to use the Form::select() option, you'll need to use a different method of getting your agreements. Check the other answers for that solution.
Hope this provided some insight!
Edit
I should note that this isn't the Laravel Way of doing things, but it is certainly viable.
controller::
$states=$this->States->get_States();
$data=array('states'=>$states);
echo View::make('frontend.list-property')->with('data', $data);
list-property.blade.php:: // this is sub view
#extends('layouts.frontend.frontend_login')
#section('content')
<select>
{{$states_drpdown}} //when I use this statement I am getting error
</select>
#stop
frontend_login.blade.php //This is layout file
<?php
$states_drpdown='';
foreach ($data['states'] as $state):
$states_drpdown.='<option value="'.$state->sid.'">'.$state->statename.'</option>';
endforeach
?>
<select>
{{$states_drpdown}} //I am getting list of options here
</select>
#yield('content')
I am getting following error, can any one help me out pls.
Symfony \ Component \ Debug \ Exception \ FatalErrorException
Method Illuminate\View\View::__toString() must not throw an exception
You may also share a piece of data across all views:
View::share('name', 'Steve');
You can find more about this here.
In your case, try: (warning untested code)
$states = $this->States->get_States();
$data = View::share('states', $states);
return View::make('frontend.list-property');
$data = array('states' => $this->States->get_States());
echo View::make('frontend.list-property', $data);
Assuming $this->States->get_States() returns a string of options, not an array?
<select>
{{$states}}
</select>
If it $this->States->get_States() returns an array, you could just do this (no need for <select> tags):
{{ Input::select('name-of-field', $states, Input::old('name-of-field')) }}
Consider returning the view instead of echoing.
I have data look like this
id rel word
1 A word A
2 B word B
3 B word C
to get the data my controller look like this :
public function get_new()
{
return View::make('form.new')
->with('datas', Data::all());
}
and my View will look like this :
<select>
#foreach($datas as $data)
<option value="{{ $data->id }}" rel="{{ $data->rel }}">{{ $data->word }}</option>
#endforeach
</select>
How i can get that data with Form::select ?
If we use Form::select we can call that with Data::lists('word','id') and pass to the view.
but if i use this i just can get 2 data that is id and word.
how can i get all data that is id, rel and word with Form::select. Please help me.
Laravel 4s FormBuilder and HtmlBuilder aren't going to produce everything for everyone. Taylor himself has said that he wants to keep it lean and simple. The solution you have now is probably the best way you could go about it (using a simple loop).
Form::select isn't capable of automatically populating other attributes of the option element. If you want this functionality you're going to either have to make yourself a custom macro or write your own form generator.
Probably not the answer you're after but that pretty much sums it up.
Just want to update the solution...
<select name="name" id="name">
#foreach($datas as $data)
<option value="{{ $data->id }}" {{ (Input::old('name', 0) === $data->id ? ' selected="selected"' : '') }} rel="{{ $data->rel }}">{{ $data->word }}</option>
#endforeach
</select>
This will give old input on selected option