i'm fetching data from database and want to make dropdown list with one value selected by default
i tried this
Laravel-5 how to populate select box from database with id value and name value
but nothing happen
my view file:
<div class="row">
<!-- Country ID Field -->
<div class="form-group col-sm-6">
{!! Form::label('country_id', 'Country ID:') !!}
{!! Form::select('country_id',$countries, isset($user) ? $user->country_id : 'Nepal', ['class' => 'form-control']) !!}
</div>
i'm new to laravel-collective..please help :)
The Laravel Collective code is really helpful... but it is also buggy in some odd ways.
There is an automatic binding that you can take advantage of by using null in the Collective select() constructor:
<div class="row">
<div class="form-group col-sm-6">
{!! Form::label('country_id', 'Country ID:') !!}
{!! Form::select('country_id',$countries, null, ['class' => 'form-control']) !!}
</div>
This is usually really good if you use form-model binding on the forms. However, there may be cases when it doesn't pick up the user model. If so, you were correct with your original code. BUT, for some reason, Collective occasionally reads the isset section better when the isset block is within parenthesis:
<div class="row">
<div class="form-group col-sm-6">
{!! Form::label('country_id', 'Country ID:') !!}
{!! Form::select('country_id',$countries, (isset($user) ? $user->country_id : 'Nepal'), ['class' => 'form-control']) !!}
</div>
Try either of these - hopefully one will help you.
The other potential item to check is to make sure your $countries are set and contain some ids, INCLUDING the id for the $user->country_id. If the user's country is not in the $countries list, it will not work even if the user's id is set.
This is bit broader than the original question about <select> inputs, but note Laravel Collective also has form model binding using the model() method, so if you were using multiple fields from your 'country' model, to use this example, you don't need to specify $countries as an argument for every input, just once at the beginning.
Instead of calling Form::open, you do:
{!! Form::model($countries, [
'method' => 'POST',
'url' => ['/admin/country'],
]) !!}
This is the cleanest implementation I've found, because can also use it for 'create' in a "CRUD" context; i.e. where you want to pass some default values to an otherwise empty form.
You create a new instance of your model (e.g. $country = new Country()) and set the relevant values, without calling ->save(), then use compact() to pass it to your view and call Form::model, as above, within that view.
This allows you to use the same view for creating and editing, without having to write conditionals to handle any defaults (and avoids the risk of accidentally adding a default value to an existing record if a user wipes a field).
Related
At the moment I can successfully display some generated content in my blade view by calling a variable with a key, like this:
<div>
{{ $data['key'] }}
</div>
What I would like to achieve, is to reuse the key for a class name for example. The reason for that is that I want to easily access this content with JavaScript by using a selector. Something that would look like this:
<div class='key'>
{{ $data['key'] }}
</div>
This code work, but as I have many fields, I would like to avoid duplicating the key. To achieve that, I thought of creating a blade components, and pass the key in the slot. So that I can reuse this key both in the class name, and in the placeholder for data.
But it seems that my variable {{ $data }} is not accessible from my component file, as it throws the error: $data is undefined.
Here is my code:
<!-- In main.blade.php -->
#component('my-component')
key
#endcomponent
<!-- In my-component.blade.php -->
<div class="{{ $slot }}">
{{ $data[$slot] }}
</div>
I am not familiar to Blade so I am open to another approach if necessary.
You should pass your data and key as arguments to your component, thereby being more explicit. You don't need to use a slot for this, as a slot is generally used for blocks of text or HTML, not for simple attributes.
Create your component like this,
<div class="{{ $key }}">
{{ $data[$key] }}
</div>
And then render it like this
#component('my-component', ['data' => $data, 'key' => 'key'])
I am using laravel form collective for automatic form generation in project.
I used same form for add and update with route model binding
{!! Form::model($operator, ['route' => ['operator.update', $operator->id]]) !!}
and route for this is
Route::resource('operator','OperatorController');
This generates automatic routes for method like POST for store and PUT for updates
if I use form collective then by default method is POST for both store and update
if I change to PUT then it changed for both
I need automatic method detection like if I am using for store, it should be POST and PUT if it is edit form
can we do this ?
Mainly if you want to detect both of this situation you should use Form::open for create mode and Form::model for update mode (with method PUT) like this:
#if(!empty($operator))
{!! Form::model($operator, ['route' => ['operator.update', $operator->id], 'method' => 'PUT']) !!}
#else
{!! Form::open(['route' => ['operator.store', $operator->id]]) !!}
#endif
This above will automaticlly add _method hidden field to your form (with PUT value) on updating.
You should also split for to actions (store and update) as it's in Laravel Docs about Resource Controller.
Trying to display a empty string from Model to html control in Laravel 5 Blade template.
{!!
Form::label('labelOccupation',$mastermodel->occupation,['style'=>'background-color:#BCBCBC'])
!!}
{!!
Form::text('textOccupation',$mastermodel->occupation,['style'=>'background-color:#BCBCBC'])
!!}
Both the text and label control can display value of occupation field correctly. But when the value is an empty string, the label control will display the wording "occupation", while the text control still able to show as empty.
Does it means I have to check empty string exists in Model every time when loading the value into label? Any other easier methods to handle such case?
You can do this way on your controller before passing the label on the form:
if(empty($mastermodel->occupation) {
$occupation_label = 'occupation';
} else {
$occupation_label = $mastermodel->occupation;
}
Then pass it to use in your blade:
{!! Form::label('labelOccupation',$occupation_label,['style'=>'background-color:#BCBCBC']) !!}
I think it's the easiest way to achieve this.
I am using Laravel 5.1. In form I am generating drop down as:
{!! Form::select('ptype', $p_types,null,['class' => 'form-control text-capitalize']) !!}
While in controller $p_types is set as:
$p_types = PType::lists('name', 'id');
I want to show an option as Select here on top of drop down. How do I do tat?
There is no simple solution to doing what you're asking, as mentioned here. Form:recipes add placeholder attribute What you can do is in the lists method insert a record into the collection before it's passed to the blade template. Checkout this link Collection Methods. That is by far the easiest solution.
$p_types->push("Select Here");
You can also do like this in blade template to add the Select here option on top of drop down
{!! Form::select('ptype',([''=>'Select here']+$p_types->toArray()) ,null,['class' => 'form-control text-capitalize']) !!}
Working on a job portal, so I arrived at a point where employers need to edit their posted jobs,On page load it gave me an error Route [employers/job/save/Mw==] not defined, please I need help my deadline is 3hours from now!
Here is my code:
Routes:
//Route for Employer's specified Job Editting -> To get ID as argv
Route::get('employers/job/edit/{id}', 'employerController#editJob');
//Route for Employer's to save specified Job after Editting -> To get ID as argv
Route::post('/employers/job/save/{id}', [
'as' => 'saveJob',
'uses' => 'employerController#saveJob'
]);
View:
{{ Form::open(['action'=>'employers/job/save/'.base64_encode($jobData->id),'class'=>'full-job-form', 'id'=>'jobForm','role'=>'form']) }}
<div class="form-group col-lg-12 col-sm-12 col-md-12">
<label class="sr-only" for="">Job Title</label>
<input type="text" class="form-control"
name="job_title" placeholder="Job Title"
value="{{ $jobData->job_title }}">
<span class="help-block">Eg. Marketing Manager</span>
</div>
Your issue is that you're using the action parameter for your Form::open() call. This expects the name for a controller method (e.g. {{ Form::open(['action' => 'employerController#saveJob']) }}. If you want to link to a pre-generated URL use the url parameter:
{{ Form::open(['url' => 'employers/job/save/'.base64_encode($jobData->id)]) }}
That said, that's not the best practice, as, if you change your routing system, you now have to change all these hardcoded URLs. As such, you should rely on named routing or controller actions.
Now, your route is already named ('as' => 'saveJob') so you should actually use the route parameter of Form::open():
{{ Form::open(['route' => ['saveJob', base64_encode($jobData->id)]]) }}
Alternatively, you could use the action parameter as you are currently trying to do (albeit erroneously):
{{ Form::open(['action' => ['employerController#saveJob', base64_encode($jobData->id)]]) }}
See the docs on forms for more information.
Also, as #TheShiftExchange says, its a bit odd to be using the base 64 encoded id, why not just use the raw id?