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())
Related
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>
I have a button that adds a new language section to the form for the users to add a new language to their profile and it was working fine until I added that part which gets the existing user languages from DB to show in case they want to change the language or update it. I have been struggling for hours and can't figure out why is this happening why it stops working when the getUserLanguages function is being called. and when remove the getUserLanguages function from the render method it will start working again.
component controller:
public function addLanguage($i)
{
$i = $this->i;
if ($i <= 3)
{
$i = $i + 1;
$this->i = $i;
array_push($this->languages , ['language_name'=>'', 'language_level'=>'']);
}
else
{
$this->sweetAlert('error', 'You only can add 3 langauges.');
}
}
public function getUserLanguages()
{
if (empty(!UserLanguage::where('user_id', auth()->user())))
{
$this->languages = UserLanguage::where('user_id', auth()->user()->id)->get(['language_name', 'language_level'])->toArray();
$this->i = count($this->languages);
}
}
the view:
#foreach ($languages as $index)
<div class="card card-body mb-4">
<div class="row">
<div class="form-group col-md-6">
<label class="" for="languageName">Language</label>
<select class="form-control form-control-alternative" name="language-name" {{-- id="languageName" --}} wire:model="languages.{{ $loop->index }}.language_name">
<option value="" class="form-control" selected disabled>Select Language</option>
#foreach ($language_names as $name)
<option value="{{ $name->abbreviation }}" class="form-control">{{ $name->language }}</option>
#endforeach
</select>
</div>
<div class="form-group col-md-6">
<label class="" for="languageProficiency">Proficiency</label>
<select class="form-control form-control-alternative" name="language-proficiency" {{-- id="languageProficiency" --}} wire:model="languages.{{ $loop->index }}.language_level">
<option value="" class="form-control" selected disabled>Proficinecy Level</option>
#foreach ($language_levels as $level)
<option value="{{ $level->level }}" class="form-control">{{ $level->name }}</option>
#endforeach
</select>
</div>
</div>
</div>
#error('languages.*.level')
<small class="text-warning">{{ $message }}</small>
#enderror
#error('languages.*.language')
<small class="text-warning">{{ $message }}</small>
#enderror
#endforeach
#if (count($languages) < 3)
<div class="row">
<div class="col-md-12">
<button type="button" class="btn btn-outline-secondary btn-round btn-block" wire:click="addLanguage({{$i}})"><span class="btn-inner--icon"><i class="fa fa-plus fa-2x"></i></span></button>
</div>
</div>
#endif
so finally after i called the getUserLanguages() from the mount method instead of the render method everything works as it should be.
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);
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'
....
];
}
I am creating a blog in Laravel. I have two tables - posts and categories.
When i want to edit one post, i have a problem with categories. My post controller is sending categories to the view, i can see them while editing, but i don't know how to set the current category in the select option.
The tables are conected:
public function posts(){
return $this->hasMany('App\Post');
}
public function category(){
return $this->belongsTo('App\Category');
}
Controller:
public function edit(Post $post)
{
$categories = Category::all();
return view('admin.posts.edit', compact('post'))->withCategories($categories);
}
View:
<form action="{{ route('posts.update', ["post" => $post->id]) }}" method="post" enctype="multipart/form-data" class="form-horizontal">
#csrf
<div class="form-body">
<div class="form-group">
<label class="col-md-3 control-label">Naslov</label>
<div class="col-md-4">
<input type="text" name='title' value='{{ old("title", $post->title) }}' class="form-control">
#if($errors->has('title'))
<div class='text text-danger'>
{{ $errors->first('title') }}
</div>
#endif
</div>
</div>
<div class="form-group">
<label class="col-md-3 control-label">Kategorija</label>
<div class="col-md-4">
<select class="form-control" name="category_id">
#if(count($categories) > 0)
#foreach($categories as $category)
<option value="{{ $category->id }}">{{ $category->name }}</option>
#endforeach
#endif
</select>
#if($errors->has('category_id'))
<div class='text text-danger'>
{{ $errors->first('category_id') }}
</div>
#endif
</div>
</div>
How can i make the category display the current category of the selected post in the option select like i have for title? value='{{ old("title", $post->title) }}'
To show the currently associated Category, you can set a default "selected" option based on the $post->category_id:
<option value="{{ $category->id }}" {{ old('category_id', $post->category_id) == $category->id ? 'selected' : '' }}>{{ $category->name }}</option>
This a simple ternary that sets the selected attribute of one of the options based on the old() value, or, if old() is not set, then based on the value of $post->category_id
What you are looking for is the selected property of the <option> element.
#forelse ($categories as $category)
<option value="{{ $category->id }}" {{ $category->id == old('category_id', $post->category_id) ? 'selected' : '' }}>
{{ $category->name }}
</option>
#endforelse
A minor change as well in the in you blade template to make use of the #forelse directive for less code.
You could also change your controller code a bit:
return view('admin.posts.edit', compact('post', 'categories'));
Further the laravelcollective/html package might help you a lot in creating forms.