Input::get() retrieving variable unique id instead of value laravel - laravel

I created a form and have a dropdown option which shows a list of option, after selecting the selected value it gets the description value instead. However I would like to retrieve the unique id instead of the description, how can I do so?
In my view.blade.php,
<div class="form-group">
<label class="control-label col-sm-3" for="role_id">Role:</label>
<div class="col-sm-5">
<select class="form-control" id="role_id" name="role_id">
#foreach ($roles as $role)
<option>{{ $role->role_description }}</option>
#endforeach
</select>
</div>
</div>
Then in my controller,
// create the data for our user
$user = new User;
$user->name = Input::get('name');
$user->email = Input::get('email');
$user->password = Hash::make(Input::get('password'));
$user->mobile = Input::get('mobile');
$user->role_id = Input::get('role_id');
// save our user
$user->save();
Is this part of the code sufficient enough for my explanation? Or shoould I show you the entire code instead.

You have to add a value attribute to your option tags:
#foreach ($roles as $role)
<option value="{{ $role->id }}">{{ $role->role_description }}</option>
#endforeach
And it will return you selected role id.

Related

How to display user selected countries with coma separation?

FORM
<div class="form-group mb-3">
<label>Country:</label>
<div class="col-sm-4">
<select id="country-dd" name="country[]" class="js-example-basic-multiple form-control" multiple="multiple">
<option value="">Select Country</option>
#foreach ($countries as $data)
<option value="{{$data->id}}">
{{$data->name}}
</option>
#endforeach
</select>
#if ($errors->has('country'))
<span class="text-danger">{{ $errors->first('country') }}</span>
#endif
</div>
</div>
BLADE FILE
<tbody>
#foreach($users as $user)
<tr>
<td>{{$user->name}}</td>
<td>{{$user->email}}</td>
<td>{{$user->countries->name}}</td>
<td>{{$user->states->name}}</td>
<td>{{$user->cities->name}}</td>
<td>{{$user->roles->name}}</td>
<td><button>EDIT</button></td>
<td><button class="deleteuser" data-id="{{ $user->id }}" data-token="{{ csrf_token() }}" >DELETE</button>
</td>
</tr>
#endforeach
</tbody>
CONTROLLER
public function viewuser(){
$this->authorize('Admin');
$users = User::with(['roles', 'countries','states', 'cities'])->get();
// dd($users);
$roles = Role::all();
$countries = Country::all();
$states = State::all();
$cities = City::all();
return view('viewuser', compact('users', 'roles', 'countries', 'states', 'cities'));
}
ROUTE
Route::get('/viewuser',[UserController::class, 'viewuser'])->name('viewuser');
If user selected 2 countries then both countries should get displayed on user listing table separated with coma. Please help me with the same. Thanks in advance.
NOTE: I have not defined foreign key for country in users table.

Trying to get property 'level' of non-object when trying to edit data and showing it in select option

I wanted to edit data and show the data in select option.
Heres my controller code:
public function edit($id)
{
$user = User::findOrFail($id);
return view('user.edit', compact('user'));
}
I'm trying like this but getting an error
<select class="form-control" name="level" id="level">
<option value="{{ $user->level }}"> {{ $user->level }} </option>
#foreach ($user as $item)
<option value="{{ $item->level }}">{{ $item->level }}</option>
#endforeach
</select>
WHat you're trying to do is loop a foreach on a single object, which is not correct.
if you see your controller, User::findOrFail($id) does not return a collection of objects, it returns only 1 object, and in your view, you're trying to loop thru that single object

Laravel - Highlighting roles which are already assigned to a user

On user\edit view I'm displaying all roles as checkboxes. I'd like to have roles which are already assigned to a user checked. All other fields in the form are filled in with details from the database already. I'm trying to work with Spatie's Laravel Permissions.
Image of the view
#foreach ($roles as $role)
<input type="checkbox" value="{{$role->id}}" name="{{$role->name}}" > {{$role->name}}</input>
#endforeach
I would like to use pure HTML. I read about eager loading but from what I understood it will give me only roles assigned to a user, not all of them.
I want to use sychRoles() function when I click update button.
I was hoping to build similar view to what Voyager uses: example.
try below:
in controller:
$roles = $user->roles;
in blade file:
#foreach ($roles as $role)
<input type="checkbox" value="{{$role->id}}" name="{{$role->name}}"> {{$role->name}}</input>
#endforeach
Try this. It will check if there is any logged in user. Then it will match the roles of that user with id from roles relationship.
#foreach ($roles as $role)
<input type="checkbox" value="{{$role->id}}" name="{{$role->name}}"
#if(auth()->check())
#foreach(auth()->user()->roles as $userRole)
#if($userRole->id==$role->id) {{"checked"}}
#endif
#endforeach
#endif> {{$role->name}}</input>
#endforeach
Here is controller code
public function edit($id)
{
$role = role::find($id);
$permissions = Permission::all();
return view('admin.role.edit',compact('role','permissions'));
}
And in view
<div>
<label for="name">Assigned Permissions</label>
#foreach ($permissions as $permission)
#if ($permission->for == 'user')
<div class="checkbox">
<label><input type="checkbox" name='permission[]' value="{{ $permission->id }}"
#foreach ($role->permissions as $give_role)
#if ($give_role->id == $permission->id)
checked
#endif
#endforeach
>{{ $permission->name }}</label>
</div>
#endif
#endforeach
</div>

Laravel $request value is null

I want to make dropdown menu where you choose company and it shows you owners that are in that company but when i request data from my blade.php in my controller i get NULL.
This is my blade.php
<form>
<label>Company:</label>
<select id="test" name="test" class="form-control input-sm">
#foreach($companies as $company)
<option value="{{$company->id}}">{{$company->name}}</option>
#endforeach
</select>
</form>
and this is my controller
public function index(Request $request)
{
$companies = Company::all();
$company_id = $request->get('company');
$owners = Owner::where('company_id',$company_id)->get();
return view ('owners', compact('owners','companies'));
}
and it's not showing any user but if I put any number manually like this
$company_id = 1;
then it shows owners from company where ID is 1.
UPDATE 2
My form looks like this now
<form action="{{ action('OwnerController#index') method="get"}}">
and my route is this
Route::get('owners', 'OwnerController#index');
Its still same.
Not sure what you want. But here is something for you.
File => create.blade.php
<form action='/item' method='POST'>
<label>Item Name:</label>
<input type="text" name="name" class="form-control input-sm">
<label>Company:</label>
<select id="company" name="company" class="form-control input-sm">
#foreach($companies as $company)
<option value="{{ $company->id }}">{{ $company->name }}</option>
#endforeach
</select>
<button class="btn btn-primary">Create</button>
</form>
File => ItemController.php
public function store(Request $request)
{
try {
$this->validate($request, [
'company_id' => 'required',
'name' => 'required|unique:items,name'
]);
$obj = new Item();
$obj->company_id = $request->input('company_id');
$obj->name = $request->input('name');
$obj->save();
return redirect()->route('item.index')
->with('success', 'Create Successful');
} catch (\Exception $ex) {
Log::error($ex->getMessage());
return redirect()->route('item.create')
->with('fail', $ex->getMessage());
}
}
As I said before I was missing some script. This solved my problem.
<form method="get" id="company_change">
<label for="company">Company:</label>
<select id="company" name="company" onchange="document.getElementById('company_change').submit()">
<option value="">----Select Company-----</option>
#foreach($companies as $company)
<option value="{{$company->id}}">{{$company->name}}</option>
#endforeach
</select>
</form>
My route is
Route::resource('owners', 'OwnerController');
You dont need a form to fill your dropdown;
You create a relationship in your model between Company and Owner and you dont even need a where,simply use eloquent like this
$owners = Owner::get();
And then in your view simply get the company using eloquent:
$owner->company->name;
That's it done!

How to do a Search By Category and Area using Laravel

I want to be able to search user by the category to which they belong and also by the area in which they are located.
I have 4 tables for that which include,
A User table with id, name, surname, email, phone, type, photo and area_id.
A category table with id, name, description
A Service table with id, name, description, and category_id.
and I have a Pivot table between Service and User named service_user with attributes id, name, price, description, user_id, and service_id.
I wish to know how can I search users by their category and area, provided all the information exists in the database using Laravel. I have managed to do a search by area alone and it works. Just now remaining the category which I don't know why it's not working or what exactly to do since in my database the category id migrates to the service table, also the service_id migrates to the pivot table(service_user) alongside with the user_id. So it's a many to many relationship btw users and services.
In my web file, I have this
Route::get('/search', 'UserController#search')->name('search');
In my user Controller, I did something like this
public function search(Request $request) {
$category = $request->get('category');
$area = Input::get('area');
//dd($category);
//dd($area);
// $p = $town->id;
// $v = Input::get('town.$town->id');
//$va = $request->getPathInfo();
if(!(empty($category)) && !(empty($area))) {
$results = User::with(['area', 'services', 'category'])
->where('services.category.category_id', 'like', "%$category%")
->where('area_id', 'like', "%$area%")
->get();
//Section::inject('title','Search');
// dd($results);
return view('Search.search', compact('results'));
}
elseif (!(empty($category)) && empty($area)) {
$results = User::with(['area', 'services'])
->where('services.category.category_id', 'like', "%$category%")
->get();
//dd($results);
return view('Search.search', compact('results'));
}
elseif(empty($category) && !empty($area)) {
$results = User::with(['area', 'services'])
->where('area_id', 'like', "%$area%")
->get();
//dd($results);
return view('Search.search', compact('results'));
}
return view('Users/jobberlist');
}
and in my Home page with the search form, I did this
<form action= "{{ route('search') }}" method="GET">
<div class="row">
<div class="col-lg-7 col-md-5 col-sm-5 col-xs-12">
<div class="job-field">
<select name="category">
<option value="">Je recherche une personne pour...</option>
#foreach ($categories as $category)
<option value="{{ $category->id }}">{{ $category->name }}</option>
#endforeach
</select>
</div>
</div>
<div class="col-lg-4 col-md-5 col-sm-5 col-xs-12">
<div class="job-field">
<select data-placeholder="City, province or region" class="chosen-city" name="area">
<option value="">Choissisez un quartier...</option>
#foreach ($areas as $area)
<option value="{{ $area->id }}">{{ $area->name }}</option>
#endforeach
</select>
<i class="la la-map-marker"></i>
</div>
</div>
<div class="col-lg-1 col-md-2 col-sm-2 col-xs-12">
<button type="submit"><i class="la la-search"></i></button>
</div>
</div>
</form>
Can someone help me out to solve this problem? I will gladly appreciate.
Something like this might work for you
User::with(['area', 'services'])
->where('type', 'jobbers')
->where('area_id', $area_id)
->whereHas('services', function ($query) use ($category_id) {
$query->where('category_id', $category_id);
})
->get();

Resources