Laravel 5 issue with select drop down - laravel

The code for the controller:
public function index()
{$items = all_list::where('list_code', 'ABC-U47')->get();
//return $items;
return view('partials.curlist', compact('items'));
I know this works, I tested it with the return $items statement.
The code for the blade view:
</div><div class="form-group">
<select class="form-control" name="item_id">
#foreach($items as $item)
<option value="{{ $item->list_data }}"></option>
#endforeach
</select>
</div>
The view just shows and empty box. I know this is something very simple but, for the life of me I can't get it to work. Any help would be appreciated. Thanks in advance.

You need to have a text value for your option tag. Like this:
<option value="{{ $item->list_data }}">{{ $item->list_data }}</option>

Related

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

Setting selected option in laravel from database

I have a problem here, I want to make an edit form, I want the province that appears in the edit form to be the province selected from the database, here my code
<select name="provinsi" id="provinsi" class="form-control">
<option value="">Pilih Provinsi</option>
#foreach ($province as $prov)
<option value="{{$prov->name}}">{{$prov->name}}</option>
#endforeach
</select>
here my controller code
public function ShowSantri(Request $request,$id)
{
$province = DB::table('provinces')->get();
$profiles = Profile::find($request->id);
return view('admin/modal/edit_santri',compact('profiles','province'));
You can try like this:
<select name="provinsi" id="provinsi" class="form-control">
<option value="">Pilih Provinsi</option>
#foreach ($province as $prov)
<option value="{{$prov->name}}"
#if ($profiles['provName'] == $prov->name)
selected
#endif>
{{$prov->name}}
</option>
#endforeach
</select>
Just I'm not sure what is name in your $profiles for province. But I think that is what you need.
Here and here are some examples.
Good luck!
Use or change this code based on your needs:
<option value="{{$prov->name}}" {{ $prov->name == $profiles->prov_id ? "selected" : "" }}>{{$prov->name}}</option>
I think the $prov->name should be $prov->id, if not, you can change $profiles->prov_id to $profiles->prov_name. It depends on your database structure.

Undefined variable: files (View: \resources\views\home.blade.php)

I am trying to get some data (all filenames) from files table in dropdown but it gives error: Undefined variable: files
Controller:
public function show($id)
{
$data = File::findOrFail($id);
$files = \DB::table('files')->get();
return view('userhome', compact('data', 'files'));
}
Blade template:
<div class="form-group">
<label class="text-left">Select Record</label>
<select name="parent_id">
<option value="">Select Record</option>
#foreach ($files as $filename)
<option value="{{ $filename->id }}">{{ $files->filename }}</option>
#endforeach
</select>
</div>
web.php: https://ibb.co/xMswcTG
What causes this error?
you return userhome.blade.php into your controller but try to get $files and $data variables into home.blade.php
change
return view('userhome', compact('data', 'files'));
to
return view('home', compact('data', 'files'));
all must be working ;)
You need to use "$filename->" object instead of "$files->" object in options tag.
Please update the blade with below code.
<div class="form-group">
<label class="text-left">Select Record</label>
<select name="parent_id">
<option value="">Select Record</option>
#foreach ($files as $filename)
<option value="{{ $filename->id }}">{{ $filename->filename }}</option>
#endforeach
</select>
</div>

laravel pass select option value to form action parameter

I am making a page with select option.
If I select one option and click a button. It will go to that page with parameter in url.
I want to know is there any easy way to insert categoryId as parameter into action?
<form method="GET" action="{{ route('routename', ['categoryId' => 1]) }}">
#foreach($categories as $category)
<select>
<option value="1">{{ $category->name }}</option>
<option value="2">{{ $category->name }}</option>
<option value="3">{{ $category->name }}</option>
</select>
#endforeach
<button>Change</button>
</form>
You are being too specific in your route
You should remove the categoryId from the route. Instead you should point your route to a controller that just accepts a Request. then you can read the value of the select box in the controller and direct the user to the view depending on the selected option.
Your view:
<form method="GET" action="{{ route('routename') }}">
#csrf
<select name='selectOption'>
#foreach($categories as $category)
<option value="{{ $category->id }}">{{ $category->name }}</option>
</select>
#endforeach
<button>Change</button>
</form>
Your Route:
Route::get('url', 'Controller#functionName')->name('routename');
Your Controller:
use Illuminate\Http\Request;
public function functionName(Request $request)
{
$option = $request->selectOption;
if($option == 1){
CODE FOR OPTION 1 HERE
}
}
This way the route will not require the parameter to be defined in the URL. Instead you can pass it as a parameter in the request to be read in the controller. You could then have a if rule for each option that then returns a different view.

how to fix this category select method

Hi I'm just trying to make category for my movie blog and its working fine, but the editing category must be selected right.
So I did something like this:
//fetching all categories
#foreach($categories as $category)
//start option
<option
//fetching current movie categories
#foreach($movie->categories as $cat)
//matching is this category match with
#if($category->name === $cat->name)
//if match selected method works
selected="selected"
#endif
#endforeach
>{{ $category->name }}
</option>
#endforeach
and its working fine but i thinks its a wrong way
can u suggest me good way to do this
thanks
check this:
#if(in_array( $category->name ,$movie->categories->pluck('name'))
{{'selected' }}
#endif
if give you a type arg2 must be an array error. add a toArray() after the pluck methods.
Thats how I do it.
<select class="form-control" name="category_id">
#foreach($categories as $category)
<option value="{{$category->id}}" {{ isset($gallery) && $category->id == $gallery->category_id?'selected':''}}>{{$category->name}}</option>
#endforeach
</select>
You can use ternary operator instead of using if().
If you have many to many relationship then you can try
<select class="form-control" name="category_id">
#foreach($categories as $category)
<option value="{{$category->id}}" {{$movie->categories->contains('name', $category->name )?'selected':'' #endif >{{$category->name}}</option>
#endforeach
</select>
Hope this helps.

Resources