Within my update profile form, I have a select list for title. Admittedly, the items should be derived from the database however for the moment, I would like this particular select list to do the following..
Show the selected item on initial page load. So, if the $user['title'] is a 'Mrs', this is displayed.
Should laravel validation bring up errors on other form fields, and the user has changed the title to 'Mr', the selected state should be on the item 'Mr'.
I have the following so far, but it isn't working correctly.
<select name="title" id="title">
<option value="Mr" #if(($user['title']) == 'Mr') selected #else #if((Input::old('title')) == 'Mr') selected #endif #endif>Mr</option>
<option value="Mrs" #if(($user['title']) == 'Mrs') selected #else #if((Input::old('title')) == 'Mrs') selected #endif #endif>Mrs</option>
....
</select>
You should pass the data for the select from your Controller and also uset the Form component to build the select in the View, for example:
In Controller method:
$users = User::lists('title', 'id');
return View::make('user')->with('users', $user);
In the View:
// By default, item with id 1 (the first item) will be selected
echo Form::select('title', $users, Input::old('title', 1), ['id'=>title]);
If you are using Blade then:
// By default, item with id 1 (the first item) will be selected
{{ Form::select('title', $users, Input::old('title', 1), ['id'=>title]) }}
Read the documentation properly. You are using Laravel with old school PHP coding.
Related
I have table user and branch like this
id name branch_code
1 ABC ["011","012","013","014"]
2 DEF ["014"]
table branch
id branch_code branch_name
1 011 XXX
2 012 YYY
3 013 ZZZ
4 014 WWW
when create a new user, there is multi-select dropdown for BRANCH, so one user can select many branches.
i can save the multi-select dropdown it into table user in column branch_code. But i have problem to show the selected value in edit form
here is the multi-select dropdown for edit form
<select class="form-control" name="branch[]" id="users" multiple>
#foreach ($branch as $branch)
<option #if ($branch->branch_code==$user->branch_code)
selected
#endif value="{{$branch->branch_code}}">
{{$branch->branch_name}}
</option>
#endforeach
</select>
the edit controller
public function edit($id)
{
$branch = Branch::where('status','1')->get();
$user = User::where('id',$id)->first();
return view('admin.edit', compact('user','branch'));
}
in edit form, i want to show all branches with selected branches and also unselected branches in the dropdown. and for selected branches, it will highlighted with "selected" remark or something like that. But now it's only show all branches and no remark for selected branches
$user->branch_code is array or json string of an array.
If it's json string then $user->branch_code = json_decode($user->branch_code) first.
When you have array - you can do something like:
<option
#if (in_array($branch->branch_code, $user->branch_code)) selected #endif
value="{{$branch->branch_code}}"
>
{{$branch->branch_name}}
</option>
I am sending data to my view as below;
I am displaying all contacts in a multi select form and trying to get "selected" printed in options listings. (Then I will use jquery to populate the select field with chosen plugin) In my other pages, nested foreach loop works(something like below), it works #if check is checking against a single value. But now, I have multiple values to check. Nested foreach loop fails (see below)
$contacts = ContactDirectory::where('customer_id', Auth::user()->customer_id)->get(); //Say 50 contacts
$rfito = rfito::where('rfi_id',$rfiid)->get(); // say 5 contacts
then
#foreach ($contacts as $contact)
#foreach ($rfito as $rfito)
<option value="{{$contact->id}}" #if($contact->id == $rfito->contact_id) selected #endif >
{{$contact->firstname}}
</option>
#endforeach
#endforeach
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.
I have basketball match details page and a page to purchase a ticket. In the match details page, the user can select through a select option the type of tickets and how many of each one they want. And then click in "Buy" to go to the payment page. In the payment page I want to show a summary of what user selected, a summary like:
Type Quantity Price Subtotal
center 2 100$ 200$
left 1 50$ 50$
right 1 50$ 50$
Total 300$
Do you know how to get the quantity of each ticket selected by the user in the match details page and send that information to the payment page to be possible to show the summary?
Code structure that I have for this question:
I have a FrontController and it has the single() method to show the match details page:
public function single($id){
$match = Match::where('id', $id)->first();
$tickets = Ticket::where('match_id', $match_id->id)->get();
return view('matchs.show')->with('match',$match)->with('match', $match)->with('tickets', $tickets);
}
Then in the match details page I have the types of tickets listed and for each one there is a select menu so the user can select how many tickets want for each one:
<ul>
#foreach($tickets as $ticket)
<li>
<span>{{$ticket->title}}</span>
<form method="post" action="{{route('matchs.payment', ['id' => $match->id, 'slug' => $match->slug])}}">
<select name={{ $ticket->title }}>
<option selected>0</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
</select>
</form>
<span>X {{$ticket->showPrice()}}</span>
</li>
#endforeach
<li>
<span>TOTAL</span>
<span>0.00€</span>
</li>
<input type="submit" value="Go To Payment Page"/>
</form>
</ul>
Then there is also in the FrontController the payment method to show the payment page (I dont know if this a good approach or if the payment() method should be in other controller, but for now is in the FrontController):
public function payment(){
return view('matchs.payment');
}
The route to show the payment page:
Route::get('/match/{id}/{slug?}/payment', [
'uses' => 'FrontController#payment',
'as' =>'matchs.payment'
]);
First of all change your route from Route::get to Route::post in other way you get exception notMethodAllowed
Next: there will be probably an csrf exception - you need to add {{ csrf_field() }} into your form
I belive there is shorter and more elegant way to get one element from DB:
$match = Match::findOrFail($id) or you can even search for route-model binding
You can dispaly views with data in this shorter way: return view('matchs.show', compact('match', 'tickets'));
Basicaly you should get data inside payment() (it should be called store or something like that) and put that data into database or session and then redirect to the payment function with view where you retrive from session or DB that data from previous step
The way: single() -> submit form -> store(Request $request) -> save data and redirect -> payment()
Remember about validation all data from form.
I'm trying to use blade to display a dropdown list from table data. The problem I have, is that I want to display the results of two fields in the table concatenated, not just one.
So I want something to render something like;
<select id="agreement_type" name="agreement_type">
<option value="1">Agreement Field 1 - Agreement Field 2</option>
<option value="2">Agreement Field 1 - Agreement Field 2</option>
<option value="4">Agreement Field 1 - Agreement Field 2</option>
</select>
My controller currently looks like this;
$agreements = Agreement::lists('agreement_type','id');
return View::make('client_agreements.create')
->with('agreements', $agreements);
My blade view currently looks like this;
<div class="form-group">
{{ Form::label('agreement_type', 'Agreement type') }}
{{ Form::select('agreement_type', $agreements) }}
</div>
I have tried to amend the view and controller in various ways to get the desired output. But can't get it to work.
I want to display agreement_type and level and have the id set as the value so I tried;
$agreements = Agreement::lists('agreement_type'.'level','id');
But this just displays level as the value and completely ignores id.
This is the simplest method. Just use a foreach loop to build the options array:
$agreements = Agreement::all();
$agreementOptions = array();
foreach($agreements as $agreement){
$agreementOptions[$agreement->id] = $agreement->agreement_type.' '.$agreement->level;
}
return View::make('client_agreements.create')
->with('agreements', $agreementOptions);
However you can also define an attribute accessor in your model. You can use that to create new properties that can be accessed normal, but you can run logic to generate them (like combining to attributes)
public function getSelectOptionAttribute(){
return $this->attributes['agreement_type'].' '.$this->attributes['level'];
}
And then you can use lists:
$agreements = Agreement::all()->lists('select_option', 'id');
(Laravel converts SelectOption (studly case) from the method name to select_option (snake case) for the attribute, so don't get confused by that)