I need help with my dropdown. Currently the dropdown display all options from database. However, what I want it to display option based on the user id only. For example, this form is for Urban Seafood, suppose the dropdown will only display Urban Seafood option instead of showing all options. Below is the controller and view blade. Thank you.
Adjustment Controller:
public function create() {
$restorants = Restorant::where(['active' => 1])->get();
return view('ingredient::adjustments.create', compact('restorants'));
}
View:
<select class="form-control select2" name="restorant_id">
<option disabled selected value> -- {{ __('Select an option') }} -- </option>
#foreach ($restorants as $restorant)
<option <?php if(isset($_GET['restorant_id'])&&$_GET['restorant_id'].""==$restorant->id.""){echo "selected";} ?> value="{{ $restorant->id }}">{{$restorant->name}}</option>
#endforeach
</select>
You need the Auth facade
use Illuminate\Support\Facades\Auth;
// ...
public function create() {
$restorants = Restorant::where('active', 1)->where('user_id', Auth::id())->get();
return view('ingredient::adjustments.create', compact('restorants'));
}
Related
Note : I am using Laravel
This is my dropdown and what I want to be able to update my php variable which is stored in my database $SetNewMatch = Auth::user()->dailyMatch;
<form>
<select name = "dailyMatch">
<option value="Pop">Pop</option>
<option value="Classical">Classical</option>
<option value="Rock">Rock</option>
</select>
<button type="submit">
{{ __('Submit') }}
</button>
</form>
Once the user submits a value by pressing the submit button , I want a boolean variable to also update to true , so $book = true.
Once the user has selected value from the dropdown and the boolean value is true. I want the blade layout will appear or even a Div class will appear.
I've tried the below but its not displaying
<?php
if ($book == true)
{
#include 'layouts.divAPIclass');
}
else
{
echo "error";
}
?>
I know this is a lot but if you have any questions just ask. If you have any better ways in doing this please suggest. Thanks
You've got options for this. I don't know what your routes file looks like, but I'm going to assume you have the following in your web routes file.
Route::post('/your/route', 'MatchController#update')->name('dailyMatch');
Option #1:
Redirect to a named route
Your blade that the form is on:
<form method="POST" action="/your/route">
#csrf
<select name="dailyMatch" id="dailyMatch" onchange="this.form.submit()">
<option value="pop">Pop</option>
<option value="classical">Classical</option>
</select>
</form>
#if( $book == true )
#include('layouts.divAPIclass')
#endif
Your MatchController:
public function update(Request $request)
{
$dailyMatch = $request->dailyMatch;
// process your data
...
// then redirect back with your boolean value
return redirect()->route('routenamehere', [
'book' => true,
'dailyMatch' => $dailyMatch
]);
}
Option #2:
Take advantage of laravels back() helper
<form method="POST" action="/your/route">
#csrf
<select name="dailyMatch" id="dailyMatch" onchange="this.form.submit()">
<option value="pop">Pop</option>
<option value="classical">Classical</option>
</select>
</form>
#if( session('dailyMatchUpdated') )
#include('layouts.divAPIclass')
#endif
Your MatchController:
public function update(Request $request)
{
$dailyMatch = $request->dailyMatch;
// Process your dailyMatch data
...
// Then use the back() function
return back()->with('dailyMatchUpdated', 'You selected a new match');
}
Option #3:
Redirect to a controller action
If you are navigating to the original form page via a controller action, I would suggest to go this route
<form method="POST" action="/your/route">
#csrf
<select name="dailyMatch" id="dailyMatch" onchange="this.form.submit()">
<option value="pop">Pop</option>
<option value="classical">Classical</option>
</select>
</form>
#if( $book == true )
#include('layouts.divAPIclass')
#endif
Your MatchController:
public function update(Request $request)
{
$dailyMatch = $request->dailymatch;
// Process your data
...
// Then redirect to the original controller action and pass it the book value
return redirect()->action('SomeController#someFunction', ['book' => true]);
}
Hopefully this helps you out!
I have two tables, schools and grades and they are many to many related. I have successfully set up the many to many relationships in the model already.
I am able to insert the select box value into the database but i having difficulty try to fetch.
When i click on a grade with id = 1 for instance, i am trying to fetch its corresponding school into the select box including the values. i get an error Integrity constraint violation: 1052 Column 'id' in field list is ambiguous.
What am i not doing right in my query?
controller
public function editPage($id)
{
$grade = Grade::whereId($id)->firstorFail();
$schools = $grade->schools()->pluck('id')->toArray();
return view('grades.edit',compact('grade','schools'));
}
Model
public function schools()
{
return $this->belongsToMany('App\School')
->withTimestamps();
}
View
<div class="form-group">
<label for="select" class="col-lg-2 control-label">School</label>
<div class="col-lg-10">
<select class="form-control" id="school" name="school[]" mulitple>
#foreach($schools as $school)
<option value="{!! $school->id !!}" #if(in_array($school->id, $schools)) selected="selected" #endif >
{!! $schools->name !!}
</option>
#endforeach
</select>
</select>
</div>
</div>
$school is an array in your case, so your view most likely can't decide what to do with $school->id...
Seems more likely you wanna just use school models in your view, so just only compact('grade') and then use foreach($grade->schools as $school) in your view.
Also your conditional for selected options seems wrong, every $school (which is only an id at this point, because $schools is an array of ids) is obviously in $schools because that's how your foreach is set up. But you should fix the other thing first.
I think it's probably because it's not sure what 'id' you want to pluck.
Try
$schools = $grade->schools()->pluck('schools.id')->toArray();
You could also just pass the grade and get the schools in the view...
public function editPage($id)
{
$grade = Grade::whereId($id)->with('schools')->firstorFail();
return view('grades.edit', compact('grade'));
}
And then
$school_options = $grade->schools->pluck('name', 'id');
#foreach($school_options->all() as $key => $value)
<option value="{{ $key }}">{{ $value }}</option>
#endforeach
I have a login form with select option with languages. So, in my form I have this:
<select class="form-control" name="language">
<option>Choose language...</option>
#foreach(LaravelLocalization::getSupportedLocales() as $localeCode => $properties)
<option value="{{$localeCode}}" #if(App::getLocale() === $localeCode) selected="selected" #endif>
{{$properties["native"]}}
</option>
#endforeach
</select>
After login I try to set it in authenticated method:
protected function authenticated(Request $request, $user)
{
App::setLocale(Input::get('language'));
$user->countOfLogins++;
$user->save();
}
But I always get the English language. Btw, when I do dd(Input::get('language'), I get de or whatever... Also, I use this package https://github.com/mcamara/laravel-localization
I have an edit page where the data of a product is passed, on this page I have a select box for the category change, in it I need to insert all the categories registered in my database but only shows the category related to the product of the page.
Select Box of Edit view
<select class="selectpicker" data-live-search="true">
#foreach($produtos->categoria as $categoria)
<option data-tokens="{{$categoria->erp_name}}" value="{{$categoria->erp_categoryid}}">{{$categoria->erp_name}}</option>
#endforeach
</select>
ProdutosController
public function edit($id)
{
$produtos = Produtos::find($id);
return view('functions.edit')->with('produtos', $produtos);
}
Routes
Route::get('/product/update/{id}', 'ProdutosController#edit')->name("product::updateGet");
Route::post('/product/update/{id}', 'ProdutosController#update')->name("product::updatePost");
Any suggestions?
The problem is you are looping through the categories of that product, not all categories. Here is a solution:
public function edit($id)
{
$produtos = Produtos::find($id);
$categories = Category::all();
return view('functions.edit', compact('produtos', 'categories'));
}
then in your view:
<select class="selectpicker" data-live-search="true">
#foreach($categories as $categoria)
<option data-tokens="{{$categoria->erp_name}}" value="{{$categoria->erp_categoryid}}">{{$categoria->erp_name}}</option>
#endforeach
</select>
I have 2 tables. Employee table and Roles tables. In my employee.show blade I want to render the dropdown list of the role already assgined or available to assign. I am not using collective forms and am a little stuck as all tutorials use thse.
My controller
public function show($employee)
{
$employee = Employees::find($employee);
$roles = Roles::pluck('role_name', 'id');
return view('employees.show')->withEmployee($employee)->withRoles($roles);
}
and my show.blade. Question is how do i pull the complete list from db of all other roles so a user can update?
<div class="form-group">
<label class="col-md-4 control-label" for="roles_id">Role</label>
<div class="col-md-4">
<select id="roles_id" name="roles_id" class="form-control">
<option value="{{ $employee->roles->id }}">{{ $employee->roles->role_name }}</option>
</select>
</div>
</div>
What you've done in your controller seems correct. Your view however should loop through the roles and create options for them. Bear in mind, that you've used Role::pluck('role_name', 'id'), which will return an array of 'id' => 'role_name'.
You can decide which one is selected by adding a selected attribute to the employee's role option by using a ternary statement, which compare's the employee's role_id to the role_id we're currently looking at in the loop.
Try something like this:
<select name="roles_id">
<option value="">Select...</option>
#foreach ($roles as $roleId => $roleName)
<option value="{{ $roleId }}"{!! $employee->role_id == $roleId ? ' selected' : '' !!}>{{ $roleName }}</option>
#endforeach
</select>