View:
{{ Form::open(array('url'=>'agents/searchStaff',
'class'=>'form-search',
'role'=>'form')) }}
<select class="form-control">
#foreach ($company->users as $user)
<option id="{{ $user->id }}" name="agent">
{{ $user->firstname }} {{ $user->surname }}
</option>
#endforeach
</select>
{{ Form::close() }}
Route:
Route::post('agents/searchStaff', 'AgentsController#postSearchStaff');
Controller:
public function postSearchStaff()
{
$agent_id = Input::get('agent');
dd($agent_id); // For testing purposes - Always returns NULL.
}
I'm unsure as to why the option value's ID isn't being passed onto the controller. Any help and guidance would be appreciated.
You need to change id={{ $user->id }} to value={{ $user->id }},
but this:
<select class="form-control">
#foreach ($company->users as $user)
<option id="{{ $user->id }}" name="agent">
{{ $user->firstname }} {{ $user->surname }}
</option>
#endforeach
</select>
can be done in one line with a bit sugar in your model:
// view
{{ Form::select('agent', $users, $selectedAgentId, $attributes) }}
// User model
public function getFullnameAttribute()
{
return $this->attributes['firstname'].' '.$this->attributes['surname'];
}
// controller:
$users = $company->users->lists('fullname','id'); // Collection method
Change
<select class="form-control">
#foreach ($company->users as $user)
<option id="{{ $user->id }}" name="agent">
{{ $user->firstname }} {{ $user->surname }}
</option>
#endforeach
</select>
to
<select class="form-control" name="agent">
#foreach ($company->users as $user)
<option value="{{ $user->id }}">
{{ $user->firstname }} {{ $user->surname }}
</option>
#endforeach
</select>
Related
I am trying to show roles which is related to user but unfortunately userRoles are not showing related to user in selection option, please help me how can I match that thanks.
Note :- I am using spaite laravel permission docs
controller
public function edit(User $user)
{
$data = [
'isEdit' => true,
'user' => $user,
'roles' => Role::where('guard_name', '=', 'web')->select(['id', 'name'])->get(),
'userRole' => $user->getRoleNames(),
// i am getting userRole in array related to user
// ['employe']
];
// return $data['userRole'];
return view('cms.user_management.add-user', $data);
}
html view
<div class="col-md-6">
<div class="form-group">
<label>Role <span class="text-danger">*</span></label>
<select class="form-control" name="roles[]">
<option selected="selected" disabled >please
select</option>
#foreach ($roles as $item)
<option value="{{ $item->name }}"{{ $userRole ==
$item->name ? ' selected' : '' }}>{{ $item->name }}</option>
#endforeach
</select>
</div>
<span class="text-danger">{{ $errors->first('roles') ?? null }}
</span>
</div>
Try the following changes in your HTML
If $userRole is array than check if $item->name exists in array, using in_array.
'userRole' => $user->getRoleNames()->toArray(),
html view
{{ in_array($item->name, $userRole) ? 'selected' : '' }}
<div class="col-md-6">
<div class="form-group">
<label>Role<span class="text-danger">*</span></label>
<select class="form-control" name="roles[]">
<option selected="selected" disabled>please select</option>
#foreach ($roles as $item)
<option value="{{ $item->name }}"{{ in_array($item->name,$userRole) ? 'selected' : '' }}>{{ $item->name }}</option>
#endforeach
</select>
</div>
<span class="text-danger">{{ $errors->first('roles') ?? null }}</span>
</div>
you can use this to get the roles asssociated with current user or
Auth::user()->roles->pluck('name');
for any specific user roles.
User::find($id)->roles->pluck('name')
Controller:
public function index(){
$bike1 = Bike::get()->first();
$bike2 = Bike::get()->skip(1)->first();
return view('compare.index')->with(['bike1'=> $bike1, 'bike2'=> $bike2 ]);
}
Here I have passed only the first and second row to the blade view.
Blade view:
Select first bike
<form action ="{{ route('compare.get', [$bike1->id, $bike2->id]) }}" method="get">
<div class="first_bike" style="width:200px;">
<select name="bike1">
<option value="{{ $bike1->id }}"> {{ $bike1->name }}</option>
</select>
</div>
<br>
Select second bike
<div class="second_bike" style="width:200px;">
<select name="bike2">
<option value="{{ $bike2->id }}"> {{ $bike2->name }}</option>
</select>
</div>
<br>
<button type="submit"> Compare </button>
</form>
The get controller passes the value to another view that displays the data. It works.
How can I pass all the rows to the blade view? I know I can use all functions and loop the variable in the blade to get all the rows. Like this
#foreach($bike1 as $b1)
<option value="{{ $b1->id }}"> {{ $b1->name }}</option>
#endforeach
#foreach($bike2 as $b2)
<option value="{{ $b2->id }}"> {{ $b2->name }}</option>
#endforeach
If I do this, then how can I use the $b1 and $b2 as route parameters in the action properties in the form?
Can I manipulate the solution from the controller leaving my blade as it is? Or how do I change the blade view in order to achieve my goal? I am stuck
well if I am getting you correctly your Controller should be like
public function index(){
$bike1 = Bike::all();
$bike2 = Bike::all();
return view('compare.index')->with(['bike1'=> $bike1, 'bike2'=> $bike2 ]);
}
Then you modify your view to
Select first bike
<form action ="{{ route('compare.get'}}" method="get">
<div class="first_bike" style="width:200px;">
<select name="bike1">
#foreach($bike1 as $b1)
<option value="{{ $b1->id }}"> {{ $b1->name }}</option>
#endforeach
</select>
</div>
<br>
Select second bike
<div class="second_bike" style="width:200px;">
<select name="bike2">
#foreach($bike2 as $b2)
<option value="{{ $b2->id }}"> {{ $b2->name }}</option>
#endforeach
</select>
</div>
<br>
<button type="submit"> Compare </button>
</form>
For submitting you don't have to put them as route parameters,, just let the form submit and get the GET variables in the Controller that you are submitting the form to,,You can use the Request object to do this
$bike1 = Request->input('bike1');
$bike2 = Request->input('bike2');
hope you understand this,, just make sure the Request object is set as a parameter in the controller that you are submitting to
I am creating a blog in Laravel. I have two tables - posts and categories.
When i want to edit one post, i have a problem with categories. My post controller is sending categories to the view, i can see them while editing, but i don't know how to set the current category in the select option.
The tables are conected:
public function posts(){
return $this->hasMany('App\Post');
}
public function category(){
return $this->belongsTo('App\Category');
}
Controller:
public function edit(Post $post)
{
$categories = Category::all();
return view('admin.posts.edit', compact('post'))->withCategories($categories);
}
View:
<form action="{{ route('posts.update', ["post" => $post->id]) }}" method="post" enctype="multipart/form-data" class="form-horizontal">
#csrf
<div class="form-body">
<div class="form-group">
<label class="col-md-3 control-label">Naslov</label>
<div class="col-md-4">
<input type="text" name='title' value='{{ old("title", $post->title) }}' class="form-control">
#if($errors->has('title'))
<div class='text text-danger'>
{{ $errors->first('title') }}
</div>
#endif
</div>
</div>
<div class="form-group">
<label class="col-md-3 control-label">Kategorija</label>
<div class="col-md-4">
<select class="form-control" name="category_id">
#if(count($categories) > 0)
#foreach($categories as $category)
<option value="{{ $category->id }}">{{ $category->name }}</option>
#endforeach
#endif
</select>
#if($errors->has('category_id'))
<div class='text text-danger'>
{{ $errors->first('category_id') }}
</div>
#endif
</div>
</div>
How can i make the category display the current category of the selected post in the option select like i have for title? value='{{ old("title", $post->title) }}'
To show the currently associated Category, you can set a default "selected" option based on the $post->category_id:
<option value="{{ $category->id }}" {{ old('category_id', $post->category_id) == $category->id ? 'selected' : '' }}>{{ $category->name }}</option>
This a simple ternary that sets the selected attribute of one of the options based on the old() value, or, if old() is not set, then based on the value of $post->category_id
What you are looking for is the selected property of the <option> element.
#forelse ($categories as $category)
<option value="{{ $category->id }}" {{ $category->id == old('category_id', $post->category_id) ? 'selected' : '' }}>
{{ $category->name }}
</option>
#endforelse
A minor change as well in the in you blade template to make use of the #forelse directive for less code.
You could also change your controller code a bit:
return view('admin.posts.edit', compact('post', 'categories'));
Further the laravelcollective/html package might help you a lot in creating forms.
I want to retrieve all the users and associated roles to the one table in laravel. but i couldn't continue because i'm getting error called
This is my controller function
public function index(){
if(Auth::user()->isAdmin==1){
$users = User::with('roles')->get();
return view('users', campact($users));
} else {
abort(403, 'Unauthorized action.');
}
}
This is my User Model's roles function
public function roles()
{
return $this->belongsToMany(Role::class, 'role_users');
}
This is my Role Class's Users function
public function users()
{
return $this->belongsToMany(User::class,'role_users');
}
This is my data table
#foreach ($users as $user)
<tr>
<td>{{ $user->id }}</td>
<td>{{ $user->name }}</td>
<td>{{ $user->email }}</td>
<td>
<form method="post">
{{ csrf_field() }}
<div class="form-group">
<select class="form-control" data-id="{{$user->id}}" id="role" name="role">
<option value="0">Select a Role</option>
#foreach ($users->roles as $role)
<option value="{{ $role->id }}">{{ $role->name }}</option>
#endforeach
</select>
</div>
</form>
</td>
<td>{{ $user->created_at }}</td>
<td>{{ $user->updated_at }}</td>
<td><div class="form-group">
<form method="post" id="userActivate">
{{ csrf_field() }}
<label>
<input type="checkbox" class="flat-red isActive" data-id="{{$user->id}}" #if ($user->isActive) checked #endif>
</label>
</form>
</div>
</td>
<td>
<form id="userDelete" method="post" >
{{ csrf_field() }}
<button type="button" id="delete" data-id="{{$user->id}}" class="btn btn-block btn-danger btn-flat">Delete</button>
</form>
</td>
</tr>
#endforeach
Please help me to solve this.
Change your Controller code
public function index(){
$users = User::with('roles') // Eager loading
->get();
return view('users')->with('users', $users);
}
Then, change your blade code from
#foreach ($users->roles as $role)
<option value="{{ $role->id }}">{{ $role->name }}</option>
#endforeach
to
#if(count($user->roles) > 0)
#foreach ($user->roles as $role)
<option value="{{ $role->id }}">{{ $role->name }}</option>
#endforeach
#endif
Edit this line:
return view('users', campact($users));
to:
return view('users', compact('users'));
in my controller, I am passing a list of clients to the view
public function edit(Project $project)
{
$clients = Client::select('clientName', 'id')->get();
return View::make('projects.edit', compact('project', 'clients'));
}
Now in my view, I am currently doing this
<div class="form-group">
{!! Form::label('clientName', 'Client Name:', array('class' => 'col-sm-5 control-label blue')) !!}
<div class="col-sm-7">
<select class="clientName" name="clientName">
#foreach($clients as $client)
#if (Input::old('clients') == $client->id)
<option value="{{ $client->id }}" selected="selected">{{ $client->clientName }}</option>
#else
<option value="{{ $client->id }}">{{ $client->clientName }}</option>
#endif
#endforeach
</select>
</div>
</div>
What I am trying to do is have the default select option set as the old input. At the moment, the select displays with all the clients, but the old value is not default.
How would I go about making it the default option?
Thanks
Update
I do a alternative way I am trying. In my edit function I do
public function edit(Project $project)
{
$clients = Client::lists('clientName', 'id');
return View::make('projects.edit', compact('project', 'clients'));
}
And then in my view I do
<div class="form-group">
{!! Form::label('clientName', 'Client Name:', array('class' => 'col-sm-5 control-label blue')) !!}
<div class="col-sm-7">
{!! Form::select('clientName', $clients, Input::old('clients'), ['class' => 'clientName']) !!}
</div>
</div>
Seem to have the same issue though, the client is not the old client as the default selected option.
Thanks
Your select name is clientName but your old input is looking to a field with the name clients.
The following should work:
<option value="{{ $client->id }}" {!! old('clientName', $project->client->id) == $client->id ? 'selected="selected"' : '' !!}>{{ $client->clientName }}</option>