Select default value if no old input is set - php

I am using form model binding and have the following select input :
{!! Form::select('user_id', $users, old('user_id') ?: Auth::id(), ['class' => 'form-control select2 users']) !!}
I would like to accomplish the following:
In the creation form: select the option where the user_id equals the authenticated user's ID, but if there was old input in the session, select that option instead.
In the edit form: always select the option that is stored in the model.
old('user_id') ?: Auth::id() doesn't seem to work when editing, because it always selects the option of the authenticated user and not the one that is stored in the model.

I believe you already have this line,
{!! Form::Model($users, ['route' => ['admin.user.update', $user->id], 'method' => 'PUT']) !!}
then your code should like this:
{!! Form::select('user_id', $users, isset($selectedUser) ? $selectedUser : null) !!}
at Create function do not pass $selectedUser to view but pass the id in your session,and in your edit function you should pass $selectedUser to view ,which is Auth::user()->id

Related

Update specific table field on another Table

I need to update specific one id / row on a separate field on another table. how is the logic to that? so far i have this pseudo code sql query.
table name: settings
current table: aircrafts
UPDATE settings SET description id = 4
i need to update id 4 on my database in database settings
how can i integrate this code into laravel syntax?
assuming i have this controller
public function editAirReg(Request $request, $id)
{
$descr = $request->input('descr_id');
DB::update('update settings set description = ,'$descr', where id = 4',[$name,$id]);
return redirect('/admin/aircrafts')->with('success', 'Settings Updated');
}
and also this view
{!! Form::open(['action' => 'Admin\AircraftsController#getdata', 'method' => 'POST']) !!}
<input type="hidden" class="form-control" name="settings" value="{{$aircraft->air_line}}">
{{Form::submit('BIND', ['class'=>'btn btn-primary btn-block btn-lg', 'name'=>'submit'])}}
{!! Form::close() !!}
First of all, getData() doesn't make sense if you're updating something in mysql. It should be called store() or save or whatever you want to call so that the function name is appropriate.
Let's change the form so that it can pass the id to the controller.
{ Form::open(array('action' => 'Admin\AircraftsController#getdata', $id)) }}
{{ Form::hidden('settings', $aircraft->air_line) }}
{{ Form::submit('Submit', array('class' => 'btn btn-primary')) }}
{{ Form::close() }}
What I did is that change your input to the laravel syntax. Also make sure you pass the id from form to controller. take a look at my example. look where I put $id field.
Now in the controller, this is what you do.
$setting = Setting::findOrFail($id); // you must have a model or you can use DB facade
$setting->description = $request->settings; //name that you called it in form
$setting->save();

Laravel 5.5 correct way for a CRUD form select

So I have this CRUD where I use the same form to create and edit entries.
I need in several form selects, when creating (no data present for that particular field yet) my select to show the placeholder, but when editing, my select to show whatever is stored on database for that particular id filed. So I have:
Controller:
...
public function create()
{
$house = houses::pluck('name', 'id');
//$thisclient = null;
$clients = client::pluck('last_name', 'id');
$reps = user::where('role_id', 5)->orderBy('first_name')->get()->pluck('full_name', 'id');
return view('prospects.create', compact('house', 'clients', 'reps'));
}
...
public function edit($id)
{
$house = houses::pluck('name', 'id');
//$thisclient = user::whereId($id)->first();
$clients = client::pluck('last_name', 'id');
$reps = user::where('role_id', 5)->orderBy('first_name')->get()->pluck('full_name', 'id');
$prospect = Prospect::findOrFail($id);
return view('prospects.edit', compact('prospect', 'house', 'clients', 'reps'));
}
and my view form:
Working for create:
{!!Form::select('client_id', $clients, null, ['class' => 'form-control', 'placeholder' => 'Please Select'] ) !!}
Working for edit:
{!! Form::select('client_id', $clients, $prospect->client_id, ['class' => 'form-control'] ) !!}
I'm having 2 troubles here, if I have null as my selected field, it won't bring the selected data on edit, if I have $prospect->client_id , it will return a error on create as there's no data yet.
I tried to solve this by creating a variable $thishouse on controller and passing it to view on return view('prospects.create', compact('house', 'thisclient','clients', 'reps')); and view Form::select('client_id', $clients, $thisclient, ['class' => 'form-control'] ) !!} but seems a bit dirty whne having several form selects...
The second trouble is if I leave a placeholder on Edit, it will show the placeholder, not $prospect->client_id itself.
What's the best and simplest way to achieve all of this and use the same form for create and edit?
Thanks
You can use Form::open and Form::model to create and edit. As an example, you can set in your view:
#if(isset($prospect))
{!! Form::model($prospect, ['action' => ['ProspectController#update', $prospect->id], 'method' => 'patch']) !!}
#else
{!! Form::open(array('action' => 'ProspectController#store', 'method' => 'POST')) !!}
#endif
And then you can create the select like this:
{!! Form::select('client_id', $clients, old('client_id'), ['class' => 'form-control'] ) !!}
So, when you are editing, Laravel will select the attribute from the variable on model function.
And since you are using Laravel 5.5, you could also use #isset instruction.

Laravel - Form select multiple

what I am trying to do is to convert standard Blade select form with multiple options where you must select options with ctrl pressed to one where you use checkboxes to select multiple choices.
My form look like this:
{!! Form::open(array('action' => 'UserController#visit_step_two', 'method' => 'post')); !!}
{!! Form::select('services', $servicesList, null, array('multiple'=>'multiple','name'=>'services[]')); !!}
{!! Form::submit('Next'); !!}
{!! Form::close(); !!}
What should I do to change that?
The Form::select() receives an array or collection which it then iterates over to generate all the <select> element's options. If you want to use checkboxes instead, you need to iterate manually to create each checkbox:
{!! Form::open(array('action' => 'UserController#visit_step_two', 'method' => 'post')); !!}
#foreach ($servicesList as $value => $name)
{!! Form::checkbox('services[]', $value, ['id' => 'service' . $value]); !!}
{!! Form::label('service' . $value, $name) !!}
#endforeach
{!! Form::submit('Next'); !!}
{!! Form::close(); !!}
The above will create a form with a list of checkboxes that have labels attached. The id attribute is added so that you can also click on the label to select the associated checkbox, since the first parameter of Form::label() will be used to generate the for attribute of the <label> which is associated with a checkbox <input> field that has the same value for the id, and since an id has to be unique it is generated using the value like so 'service' . $value because all values should also be unique.
You need jquery plugin to do that, or you can write your own.
I know a plugin is Multiple Select http://wenzhixin.net.cn/p/multiple-select/docs/#checkall-uncheckall

how to set the selected option using illuminate Form

I am using the HtmlServiceProvider in laravel 5.1 to build my form , I want to set the selected option in select dropdown list tag when I get the data from my model class
My code so far:
{!! Form::model($advertisment,['url'=>'dashboard/edit/advertisment']) !!}
{!! Form::select('Gender', [""=>'Select Gender',"Male"=>'Male', "Female"=>'Female'],0, ['class' => 'form-control', 'id' => 'form_control_6' ,'required']) !!}
{!! Form::close() !!}
Set the 0 parameter to "Male" for example and you will figure it out
If you don't want to override the value from the model, pass in null

laravel populate select box

I have seen similar questions asked before, but was unable to find a solution to my problem. So I have a ClientController and within it this function
public function edit(Client $client)
{
return view('clients.edit', compact('client'));
}
So that passes the client object to my edit view. This view is pretty much the following
{!! Form::model($client, ['method' => 'PATCH', 'route' => ['clients.update', $client->slug]]) !!}
#include('clients/partials/_form', ['submit_text' => 'Edit Client'])
{!! Form::close() !!}
So it is using a partial for the form. At the moment, the partial looks like so
<div class="form-group">
{!! Form::label('clientName', 'Client Name:') !!}
{!! Form::text('clientName') !!}
</div>
<div class="form-group">
{!! Form::label('clientStatus', 'Client Status:') !!}
{!! Form::select('clientStatus') !!}
</div>
When I visit the edit page for a client, I can see the form. The clientName is populated with the clientName value. The clientStatus is populated if I put it as a text input, but I cant get it to populate within a select as shown above. Furthermore, clientStatus can either be New or Existing. I need the select box to be pre-populated with the status of the client that is being edited, but I need the other option available within the select as well. So if the clientStatus is New, New should be pre-selected within the select box and if I open the select, Existing should be the other option.
What would be the best way to achieve this?
Thanks
Modify your select to include an array of the possible values.
Basic Select - Label is value
{!! Form::select('clientStatus',['New','Existing']) !!}
Key Value Select - Key is value
{!! Form::select('clientStatus',[ 1 => 'New', 2 => 'Existing']) !!}
Form model will then set the value in the select to the one in the model.
More information see the docs.

Categories