Laravel 7: Undefined variable in foreach loop - php

I want to display to select the campus from the campuses table in my budget allocation create.blade.php template but it displays an error.
create.blade.php
<div class="form-group row">
<label class="col-md-3">Campus</label>
<div class="col-md-9">
<select name="campus_id" class="form-control">
<option value="">--- Select Campus ---</option>
#foreach($campuses as $campus)
{{ var_dump($campus) }}
<option value="{{ $campus->campus_id }}">{{ $campus->campus_name }}</option>
#endforeach
</select>
</div>
<div class="clearfix"></div>
</div>

in your controller you must send the data to the view using second paramter of the view ...
return view('admin.budgetallocation.create',$arr);

Related

How to make dropdown list ascending order in PHP Laravel

This code from product_listing.blade.php and here the dropdown filter. I need all the item name in ascending orde. Right now the list is in order how I can see it in a database table..
<div class="box-title text-center">
{{ translate('Filter by CarType')}}
</div>
<div class="box-content">
<div class="filter-checkbox">
<select class="form-control" data-placeholder="{{ translate('All CarType')}}" name="cartype" onchange="filter()">
<option value="">Select CarType</option>
#foreach (\App\CarType::all() as $cartype)
<option value="{{ $cartype->id }}" #isset($cartype_id) #if ($cartype_id == $cartype->id) selected #endif #endisset>{{ $cartype->name }}</option>
#endforeach
</select>
</div>
</div>
You can use collection's sortBy() method:
<div class="box-title text-center">
{{ translate('Filter by CarType')}}
</div>
<div class="box-content">
<div class="filter-checkbox">
<select class="form-control" data-placeholder="{{ translate('All CarType')}}" name="cartype" onchange="filter()">
<option value="">Select CarType</option>
#foreach (\App\CarType::all()->sortBy('name') as $cartype)
<option value="{{ $cartype->id }}" #isset($cartype_id) #if ($cartype_id == $cartype->id) selected #endif #endisset>{{ $cartype->name }}</option>
#endforeach
</select>
</div>
</div>

How to show div based on data sent to blade in Laravel

i have two divs (inputbarang and inputkategori). I want to make one of the divs disappear when there's no data in it. For example, when the $produk's value is not null, the inputbarang's div will appear, and the inputkategori's div will be hidden. Whereas, if $produk is empty, then the inputbarang's div will be hidden and the inputkategori's div will appear. I try like this. When $produk is null, the inputbarang's div still appears and the inputkategori's div doesn't appear
Blade.php:
#if($produk !== null)
<div class="form-group row " id="inputbarang" >
<label class="col-form-label col-lg-3 col-sm-12">Barang</label>
<div class="col-lg-4 col-md-9 col-sm-12">
<select class="form-control m-select2" width="500px" id="kt_select2_1" name="id_product[]" multiple="multiple">
#foreach($productall as $p)
#if(in_array($p->id, $productbyIds))
<option value="{{ $p->id }}" selected="true">{{ $p->product }}</option>
#else
<option value="{{ $p->id }}">{{ $p->product }}</option>
#endif
#endforeach
</select>
</div>
</div>
#else
<div class="form-group row" id="inputkategori" >
<label class="col-form-label col-lg-3 col-sm-12">Kategori</label>
<div class="col-lg-9 col-md-9 col-sm-12">
<select class="form-control m-select2" width="500px" id="kt_select2_3" name="id_kategori[]" multiple="multiple">
#foreach($kategoriall as $k)
#if(in_array($k->id, $kategoribyIds))
<option value="{{ $k->id }}" selected="true">{{ $k->kategori }}</option>
#else
<option value="{{ $k->id }}">{{ $k->kategori }}</option>
#endif
#endforeach
</select>
</div>
</div>
#endif
Controller:
$produk = Produk::where('m_product.delete', 0)
->join('tb_promo_detail', 'tb_promo_detail.id_product','=','m_product.id')
->select('m_product.id', 'm_product.product')
->where('tb_promo_detail.id_promo',$id)
->get();
Please correct me if its wrong.. Thank you so much!
Since you are sending a collection to your blade from controller you will need to check if that collection is empty or not. Just checking if it is null or empty array will not give you the right conditional effect.
So use:
#if($produk->first())
or you can also use
#if(!$produk->isEmpty())

Laravel Multiple Filters Table with Multiple Collections

I am trying to create a multiple filter function to filter my table. I have a model called Product and it has collections named categories, colors and sizes. I have the following in my view:
<div class="col-6">
<div class="form-group mt-3">
<label>{{ __('Categories') }}</label>
<select class="form-control" id="select-categories">
<option value="0">All</option>
#foreach($categories as $category)
<option value="{{ $category->id }}">{{ $category->name }}</option>
#endforeach
</select>
</div>
</div>
<div class="col-6">
<div class="form-group mt-3">
<label>{{ __('Colors') }}</label>
<select class="form-control" id="select-colors">
<option value="0">All</option>
#foreach($colors as $color)
<option value="{{ $color->id }}">{{ $color->name }}</option>
#endforeach
</select>
</div>
</div>
<div class="col-6">
<div class="form-group mt-3">
<label>{{ __('Sizes') }}</label>
<select class="form-control" id="select-sizes">
<option value="0">All</option>
#foreach($sizes as $size)
<option value="{{ $size->id }}">{{ $size->name }}</option>
#endforeach
</select>
</div>
</div>
And then I use JQuery to send a AJAX request to my controller with the current selected option values of all three dropdowns.
Example data:
Categories: All
Colors: Red
Sizes: Small
Now I wish to show the products with the color red and size small but the category doesn't matter.
I get the collections with the use of one to many relationships and link tables.
$product->categories
$product->colors
$product->sizes
I am not sure how to approach this.
You can do something similar to below:
public function index(ProductRequest $request)
{
$query = Product::where($request->validated());
return ProductResource::collection($query);
}
and in your ProductRequest you can define filters that you want to allow
class ProductRequest extends FormRequest
{
...
public function rules()
{
return [
'categories' => 'exists:categories,id'
....
];
}

How can I use same view file for different role types in Laravel 7?

I am creating a module to ToDo list. If the role type is admin, she/he can assign ToDo to any user. However, if a normal user creates a ToDo, it will automatically, be assigned to current user. I implemented like below:
ToDoController function looks like below:
public function create()
{
return view('todo.create',['countries' => Country::all(), 'business_types' => BusinessType::all(), 'users' => User::all()]);
}
The view file create.blade.php with user selection select box is as below:
#if(!empty(Auth::user()->roles()->where('name','admin')->first()))
<div class="form-group">
<label for="description">Assigned To</label>
<select class="form-control" name="user_id">
#foreach ($users as $user)
#if ($user->id == old('user_id'))
<option value="{{$user->id}}" selected>{{$user->name}}</option>
#else
<option value="{{$user->id}}">{{$user->name}}</option>
#endif
#endforeach
</select>
#if ($errors->has('user_id'))
<div class="alert-danger">
<p>{{ $errors->first('user_id') }}</p>
</div>
#endif
</div>
#else
<div class="form-group" style="display:none;">
<label for="description">Assigned To</label>
<select class="form-control" name="user_id">
<option value="{{$user->id}}" selected>{{$user->name}}</option>
</select>
#if ($errors->has('user_id'))
<div class="alert-danger">
<p>{{ $errors->first('user_id') }}</p>
</div>
#endif
</div>
#endif
In the case of normal user, it is not required to take all users value, but I can't avoid that. Is there any optimized method to implement the same this ?
Thanks in Advance !
Manu

Laravel Dropdown Form Get Form Database

I am using laravel 5.4 . I am trying to get data form Department table and data come also the problem there more dropdown menu come also please tell how can i solve this problem. Thanks advance.[
<div class="form-group">
{{Form::label('department','Department')}}
#foreach($department as $value)
{{Form::select('department',[$value->name], ' ', array('placeholder' => 'Select Department','class'=>'form-control'))}}
#endforeach
</div>
It's not entirely clear what you're after. Your current code seems to output several select boxes, one for each department and my best guess is that you want to output only one select box, with each department as an option in it. You can achieve that by doing something like this (You may have to adapt this code slightly to suit your needs):
The manual approach
<div class="form-group">
<label for="department">Department</label>
<select id="department" name="department" class="form-control">
<option value="">Select Department</option>
#foreach ($departments as $department)
<option value="{{ $department->id }}">{{ $department->name }}</option>
#endforeach
</select>
</div>
Using Laravel Collective's Form Builder
<div class="form-group">
{!! Form::label('department', 'Department') !!}
{!! Form::select('department', ['' => 'Select Department'] + $departments->pluck'name', 'id')) !!}
</div>
Try to do this way :-
<div class="form-group">
{{Form::label('department','Department')}}
<select class="form-control" name="department">
#foreach($department as $value)
<option value="$value">$value</option>
#endforeach
</select>
</div>

Categories