Laravel: Dropdown from database - laravel

I am using laravel with infyomlab generator. I want to add data as dropdown from another table.
Here is my code
ProductController.php
$supplier = Supplier::all();
$unit = Unit::all();
$category = Category::all();
$products = $this->productRepository->all();
$product = DB::table('products')->select(
'suppliers.*',
'units.*',
'categories.*')
->join('suppliers','suppliers.id', '=', 'products.supplier_id')
->join('units','units.id', '=', 'products.unit_id')
->join('categories','categories.id', '=', 'products.category_id')
->get();
return view('products.index', compact('supplier', 'unit', 'category', 'product'))
->with('products', $products);
fields.blade.php
<select class="form-control" name="supplier_id" id="supplier_id">
<option value="">Select Supplier</option>
#foreach($suppliers as $supplier)
<option value="{{ $supplier->id }}">{{ $supplier->$supplier_name }}</option>
#endforeach
but when i press add new product it does not show the form for input it shows the following error
Undefined variable: suppliers (View: C:\xampp\htdocs\qa\resources\views\products\fields.blade.php)

try This
#foreach($products as $supplier)
<option value="{{ $supplier->id }}">{{ $supplier->$supplier_name }}</option>
#endforeach
As u returning it

It's just a typo:
Here, you are returning supplier not suppliers compact('supplier', 'unit', 'category', 'product')
So you need to perform in front end like:
#foreach($supplier as $s)
<option value="{{ $s->id }}">{{ $s->$supplier_name }}</option>
#endforech

You replace it by
$supplier = Supplier::all();
to
$suppliers = Supplier::all();
return view('products.index', compact('suppliers', 'unit', 'category', 'product','products'));

Related

Laravel - Drop down list where statement

I currently have a single drop down list that when i select the value it will display the data. However i am now adding in a new drop down list but when i press submit nothing is appearing.
I select distance and press submit data is presented. However, I have now included a price drop down list however nothing is appearing when i press submit. Can someone please help, see code:
SearchController.php
public function index(Request $request)
{
$distances = DB::table('posts')->select('distance')->distinct()->get()->pluck('distance');
$prices = DB::table('posts')->select('price')->distinct()->get()->pluck('price');
$postsInRange = $request->has('distance')
? Post::where('distance', $request->distance)->get()
: [];
return view('Pages.search', [
'distances' => $distances,
'prices' => $prices,
'posts' => $postsInRange
]);
Search.php
<div class="form-group">
<select name="distance" id="distance" class="form-control input-lg dynamic"
data-dependent="state">
<option value="">Distance</option>
#foreach($distances as $distance)
<option value="{{ $distance }}">{{ $distance }}</option>
#endforeach
</select>
<br>
<select name="price" id="price" class="form-control input-lg dynamic" data-
dependent="state">
<option value="">Price</option>
#foreach($prices as $price)
<option value="{{ $price}}">{{ $price}}</option>
#endforeach
</select>
Try this:
public function index(Request $request)
{
$distances = DB::table('posts')->select('distance')->distinct()->get()->pluck('distance');
$prices = DB::table('posts')->select('price')->distinct()->get()->pluck('price');
$postsInRange = $request->has('distance') ? Post::where('distance', $request->distance)->get()
: [];
$postsPrice = $request->has('price') ? Post::where('price', $request->price)->get()
: [];
return view('Pages.search', [
'distances' => $distances,
'prices' => $prices,
'posts' => $postsInRange,
'postsPrice' => $postsPrice
]);

Dropdown Value in serach page remain after the search or refresh the page in laravel

I tried to edit another person's code and I have to fix this small issue which is "Dropdown value will be remain even after refresh the page" and I am also new in laravel.
here is my view(I already tried some code)
<b-field label="Industry" expanded>
<b-select required name="user_industry" expanded>
#foreach ($users as $user)
<option value="{{ $user->id }}" #if( session('forms.user') == $user->id) selected="selected" #endif>{{ $user->user_industry }}</option>
#endforeach
<option value="Agriculture, Forestry, Fishing">Agriculture, Forestry, Fishing</option>
<option value="Mining">Mining</option>
</b-select>
</b-field>
and this is my Controller:
public function main_network(Request $request) {
session()->put('forms.user', $request->get('user_industry'));
$user_industry = $request->has('user_industry') ? $request->get('user_industry') : null;
$user_buisness = $request->has('user_business_name') ? $request->get('user_business_name') : null;
$users = DNEUser::where('role_id',0)->where('user_status','accepted');
if ($request->has('command') and $request->get('command') == 'page-search') {
if ($user_industry != null) {
$users = $users->where('user_industry',$user_industry);
}
if ($user_buisness != null) {
$users = $users->where('user_business_name',$user_buisness);
}
}
$users = $users->get();
return $this->getView('dne.network',compact('page_title','page_description', 'users','user_industry','user_profession','user_buisness'));
}
Can anyone please tell me what will I have to put in #foreach in view page, I actually put '$users' because its already in my controller, so is that right?
try this:
<b-field label="Industry" expanded>
<b-select required name="user_industry" expanded>
#foreach ($users as $user)
<option value="{{ $user->id }}" {{(old('user_industry') == $user->id ) ? "selected" : ""}}>{{ $user->user_industry }}</option>
#endforeach
<option value="Agriculture, Forestry, Fishing">Agriculture, Forestry, Fishing</option>
<option value="Mining" {{(old('user_industry') == 'Mining') ? "selected" : ""}}>Mining</option>
</b-select>
</b-field>

Get data Based On Selected Value in Drop Down menu - Laravel 5.4

I have two tables posts and category.
My controller:
public function index()
{
$posts = Post::orderBy('id', 'desc')->get(['category']);
$data = DB::table('posts')->paginate(5);
$category = DB::table('category')->pluck('cname','id');
return view('posts.index', [
'posts' => $posts,
'category'=> $category,
'posts' => $data
]);
}
My blade:
<select class="form-control" name="category" id="category_add">
<option value="">Select Category</option>
#foreach($category as $key=>$value)
<option value="{{ $key }}">{{ $value }}</option>
#endforeach
</select>
Now my code shows a drop down data from the database...
What I need is: When I select the drop down category it should only shows the particular category of data in the view dynamically
Any one help
Did you mean after selecting a category from the dropdown there will be another section which will show data about that specific category?
One way to do it is through ajax request. I dont know if i got your question right or wrong. can u explain a bit if i got it wrong?

Fetching all users in laravel 5.2

I am trying to fetch all the users in my application so as to pick the user name or id in a select form, but I have not been able to achieve this after a long struggle. My MessageController that retrieves the messages is:
public function getMail(){
$message = Message::orderBy('created_at', 'desc')->get();
$pple = User::where('id', '!=', Auth::id())->pluck('name', 'id')->all();
var_dump($pple);
return view('mail', array('user' => $pple))->with('messages', $message, 'users',$pple );
}
My view that I want to render the users is:
<div>
<select id="recipient">
<option>{{$pple->name}}</option>
</select>
</div>
I have really tried doing this to help me pick up a user id or name to use in my messaging system.I would appreciate if someone helped me with this
You should get your users then pluck to get the result:
$messages = Message::orderBy('created_at', 'desc')->get();
$users = User::where('id', '!=', Auth::id())->pluck('name', 'id');
and return
return view('mail')->with(compact('messages', 'users'));
Then use it this way in your view
<select name="recipient" id="recipient">
#foreach($users as $id => $name)
<option value="{{ $id }}">{{ $name }}</option>
#enforeach
</select>
Don't forget to add a name to your select.
In your view, this should work
#foreach($pple as $userId => $userName)
<option value="{{ $userId }}">{{ $userName }}</option>
#endforeach

How to filter data in laravel 5?

View
...
<span class="opensans size13"><b>Gender</b></span>
<select class="form-control" name="gender" id="gender" placeholder="Gender">
<option value="" selected="">Gender</option>
<option value="M">Male</option>
<option value="F">Female</option>
</select>
<span class="opensans size8" style="color: red;"></span>
<div class="clearfix pbottom15"></div>
<span class="opensans size13"><b>Published</b></span>
<select class="form-control" name="published" id="published" placeholder="Published">
<option value="" selected="">Published</option>
<option value="YES">YES</option>
<option value="NO">NO</option>
</select>
<span class="opensans size8" style="color: red;"></span>
...
<div class="col-md-5">
<div class="right wh70percent">
<select class="form-control" name="status_sort" id="status_sort" placeholder="Status">
<option value="" selected="">Status</option>
<option value="N">N</option>
<option value="BL">BL</option>
</select>
</div>
</div>
...
Service
public function dataCustomerList($param)
{
$status = $param['status_sort'];
$gender = $param['gender'];
$published = $param['published'];
if($published=='NO'){
$published_check = 'NO';
$published = 0;
}
else if($published=='YES'){
$published_check = 'YES';
$published = 1;
}
if(empty($status))
$customer = customer::paginate(10);
else
$customer = customer::where('tb_customer.customer_status', '=', $status)
->paginate(10);
if(!empty($gender) AND !empty($published_check)){
$customer = customer::where('tb_customer.gender', '=', $gender)
->where('tb_customer.published', '=', $published)
->paginate(10);
}
else if(!empty($gender)){
$customer = customer::where('tb_customer.gender', '=', $gender)
->paginate(10);
}
else if(!empty($published_check)){
$customer = customer::where('tb_customer.published', '=', $published)
->paginate(10);
}
return $customer;
}
The interface is like this : http://imgur.com/AoggrZz
When I just choose published and gender and then press the search button, data are displayed according
When I only select status, the displayed data in accordance
But when I choose the gender, published and status, the displayed data do not match
I want to ask again. In addition to my code, whether there are other simpler code?
Thank you
You should build your query every time you add a filter, like:
$customer = customer;
if(!empty($status)) {
$customer = $customer->where('tb_customer.customer_status', '=', $status);
}
if (!empty($gender)) {
$customer = $customer->where('tb_customer.gender', '=', $gender);
}
if (!empty($published_check)) {
$customer = $customer->where('tb_customer.published', '=', $published);
}
return $customer->paginate(10);
in this way you just add conditions and you don't need to check every single case and write again the same query
Disclaimer: I'm not a Laravel user, you could need to slightly edit the code, just saying the way to build the query

Resources