Laravel Dropdown Selected item send to edit view - laravel

This is my view index page
<div class="form-group">
{!! Form::label('cname','Clients: ') !!}
{!! Form::select('cname',[''=>'Select Category']+$client,null,['class'=>'form-control selectpicker']) !!}
<span class="input-group-btn">
<a class="btn btn-primary" href="">Edit</a>
</span>
</div>
Controller:
public function index() {
$client = Client::pluck('cname','clientid')->all();
return view('client.index', compact('client'));
}
How can i get select item from dropdown and from button click send the selected details to edit view?

You can do something like this
// Controller
public function index()
{
$roles = Roles::all();
$selectedRole = User::first()->role_id;
return view('my_view', compact('roles', 'selectedRole');
}
And then in your view
<select class="form-control m-bot15" name="role_id">
#if ($roles->count())
#foreach($roles as $role)
<option value="{{ $role->id }}" {{ $selectedRole == $role->id ? 'selected="selected"' : '' }}>{{ $role->name }}</option>
#endif
</select>

Related

select2 not showing options within livewire component

I am working with laravel livewire.There is a component to update brands and categories of a product. category and brand select option is a select2 option. dropdown showing as a select2, but does not show the options.
This is my component
<?php
namespace App\Http\Livewire\Components\MemberArea\Product;
use App\Models\ProductBrand;
use App\Models\ProductCategory;
use App\Models\Product;
use Illuminate\Support\Facades\Session;
use Livewire\Component;
class UpdateProductBrand extends Component
{
public $product_id;
public $product_category_id;
public $product_brand_id, $categories, $brands, $product;
public function render()
{
return view('pages.products.components.update-product-brand');
}
public function mount()
{
$this->product = Product::get($this->product_id);
$this->categories = ProductCategory::all();
$this->brands = ProductBrand::all();
$this->product_category_id = $this->product->product_category_id;
}
public function submit()
{
$data = $this->validate();
$product = Product::get($this->product_id);
Product::update($product, $data);
session()->flash('message', 'Brand Details Updated Successfully.');
}
}
This is My Blade
<div class="card-body">
<form wire:submit.prevent="submit">
<div class="row">
<div class="form-group col-lg-6">
<label class="h5">Product Category <sup class="text-danger">*</sup>|<button type="button"
id="new-category" class="btn btn-light ">+</button></label>
<select required class="form-control" wire:model="product_category_id" id="categories_edit">
<option value=""></option>
#foreach ($categories as $category)
<option {{ $product->product_category_id === $category->id ? 'selected' :'' }}
value="{{ $category->id }}">{{
$category->title }}</option>
#endforeach
</select>
</div>
#error('product_brand_id')
<span class="text-danger error">{{ $message }}</span>
#enderror
<div class="form-group col-lg-6">
<label class="h5">Product Brand <sup class="text-danger">*</sup>| <button type="button" id="new-brand"
class="btn btn-light">+</button></label>
<select class="form-control" wire:model="product_brand_id" id="brands_edit">
#foreach ($brands as $brand)
<option {{ $product->product_brand_id === $brand->id ? 'selected' :'' }} value="{{ $brand->id }}">
{{ $brand->title }}
</option>
#endforeach
</select>
</div>
#error('product_category_id')
<span class="text-danger error">{{ $message }}</span>
#enderror
<div class="form-group col-lg-12 text-center">
<button class="btn btn-primary" type="submit">Submit</button>
</div>
</div>
</form>
</div>
#push('js')
<script>
document.addEventListener("livewire:load", function (event) {
window.livewire.hook('afterDomUpdate', () => {
$('#categories_edit, #brands_edit').select2({
placeholder: 'Select an option',
});
});
});
$(document).ready(function () {
$('#categories_edit').select2();
$('#brands_edit').select2();
});
//set category on select
$('#categories_edit').on('change', function (e) {
var category_select = $('#categories_edit').select2("val");
console.log("category when select= " + category_select);
#this.product_category_id = category_select;
});
//set brand on select
$('#brands_edit').on('change', function (e) {
var brand_select = $('#brands_edit').select2("val");
console.log("brand when select= " + brand_select);
#this.product_brand_id = brand_select;
});
</script>
#endpush
this is how show it
when use wire:ignore it also not working. show only dropdown as select2 , but it does not show the options

Laravel search from multi select dropdown

I am trying to create a search filter using dropdown selections. The code only shows results when all dropdowns (make and model) are selected. But it should work even single dropdown (either make or model)is selected. At the moment, if I select a single dropdown, the result page shows "No Matching Data found". this is what I have done -
Controller
class CarFrontController extends Controller
{
public function index_classiccars(Request $request)
{
$filterMakes = DB::table('cars')->select('make')->distinct()->get()->pluck('make');
$filterModels = DB::table('cars')->select('model')->distinct()->get()->pluck('model');
$classiccar = Car::query();
if ($request->has('make')) {
$classiccar->where('make', $request->make);
}
if ($request->has('model')) {
$classiccar->where('model', $request->model);
}
return view(
'layouts.frontend.cars.classiccars_index',
[
'filterMakes' => $filterMakes, 'filterModels' => $filterModels,
'classiccars' => $classiccar->get()
]
);
}
public function store(Request $request)
{
return $request->all();
return view('layouts.frontend.cars.classiccars_index');
}
}
data blade
#forelse ($classiccars as $car)
<div class="row">
<div class="col-lg-5 col-md-5 col-pad">
<div class="car-thumbnail">
<a href="car-details.html" class="car-img">
<div class="price-box">
<span>£{{ $car->price }}</span>
</div>
#php $i=1; #endphp
#foreach ($car->join_caralbum as $image)
#if ($i>0)
<img class="d-block w-100" src="{{asset($image->image_location)}}" alt="car" height="225px" >
#endif
#php $i--; #endphp
#endforeach
</a>
<div class="carbox-overlap-wrapper">
<div class="overlap-box">
<div class="overlap-btns-area">
<a class="overlap-btn" href="{{ url('car_show/'.$car->id) }}">
<i class="fa fa-eye-slash"></i>
</a>
</div>
</div>
</div>
</div>
</div>
<div class="col-lg-7 col-md-7 col-pad align-self-center">
<div class="detail">
<h3 class="title">
{{ $car->make }}
</h3>
<ul class="custom-list">
<li>
{{ $car->model }} /
</li>
......
</ul>
<ul class="facilities-list clearfix">
.....
.....
....
</ul>
</div>
</div>
</div>
</div>
#empty
No matching data found
#endforelse
filter blade
{!! Form::open(['action'=>'CarFrontController#index_classiccars','method'=>'GET']) !!}
<select class="form-control" name="make" id="make">
<option> Make(All)</option>
#foreach ($filterMakes as $make)
<option value="{{ $make }}">{{ $make }}</option>
#endforeach
</select><br>
<select class="form-control" name="model" id="model">
<option> Model(All)</option>
#foreach ($filterModels as $model)
<option value="{{ $model }}">{{ $model }}</option>
#endforeach
</select><br>
{{ Form::submit('Update Search',['class'=>'btn btn-primary']) }}
Your query results empty because even if you don't select any of your options, you actually do. If options don't have a value, the value is the text you give it to the option.
In your case, if you select just a model, actually you are selected 'Make(All)' option too. You can see this simply by putting dd($request->all()) on your index_classiccars method.
For checking selects, you can give empty value to first(default) option and control it with request()->filled('input').

Dropdown filter doesn't work in Laravel 5.8

I tried to do the input dropdown filter using this code:
<div class="col-md-2">
<div class="text-justify" >
<select class="itemName form-control form-filter" style="width:90px;" name="itemName" id="itemName">
#foreach($data as $category){
<option value="{{$category->Umur}}">{{$category->Umur}}</option>
#endforeach
</select>
</div>
</div>
However, my code does not run as I expected (in picture below) - the code only call the first row in database. What should I edit with the code?
<div class="col-md-2">
<div class="text-justify" >
<select class="itemName form-control form-filter" style="width:90px;" name="itemName" id="itemName">
#foreach($data as $category){
<option value="{{$category->Umur}}">{{$category->Umur}}</option>
#endforeach
</select>
</div>
</div>
remove the { after #foreach & change the value like
<div class="col-md-2">
<div class="text-justify" >
<select class="itemName form-control form-filter" style="width:90px;" name="itemName" id="itemName">
#foreach($data as $category)
<option value="{{$category->id}}">{{$category->Umur}}</option>
#endforeach
</select>
</div>
</div>
I too had this issue myself, this is what i used:
<div class="form-group">
<div class="btn-group">
<input list="brow" class="form-control"name="instagram_account_id" id="instagram_account_id" placeholder="Search..">
<datalist id="brow">
#if($users->count() > 0)
#foreach($users as $user)
<option value="{{$user->id}}">{{ $user->username }}</option>
#endforeach
#else
No User(s) Found
#endif
</datalist>
</div>
</div>
As for my controller:
public function index()
{
$comments = Comment::paginate(10);
$users = InstagramAccount::all();
return view('instagramComments', [
'comments' => $comments,
'users' => $users,
]);
}
And (if you're going to use it an a form) my insert in my controller:
public function insert(Request $req)
{
$instagram_comment = $req->input('instagram_comment');
$instagram_account_id = $req->input('instagram_account_id');
$data = array('comment'=>$instagram_comment, 'account_id'=>$instagram_account_id);
DB::table('comments')->insert($data);
return redirect('/comments');
}
(if using the insert above, use this in your web route):
Route::post('/locationsInsert', 'InstagramLocationsController#insert')->middleware('auth');
Hope any of this helps!

How do I change the route depending on selection in laravel

I'm trying to create a page that lets the user select a product and then when they press "add to cart" that product then gets sent to the cart page.
On my product page I can have the same item that has 2 or more codes attached to it, so I've created a selection that has the codes and the user then has to pick one of them
and then press "add to cart" button to go to the cart page.
So the way I have it at the moment is that the form grabs the id of the product page and not of the selection box.
What I would like to know is how would I go about changing the route depending on what I've selected.
Here is my code
<form action="{{ route('product.addToCart', ['id' => $product->id]) }}" method="POST">
{{ csrf_field() }}
<div class="row">
<div class="col-lg-12 pl-0">
<select class="form-control mb-2" id="supplier_code" name="supplier_code">
#foreach($parent_product as $parent)
<option value="{{ $parent->supplier_code }}">{{ $parent->supplier_code }}</option>
#if(count($parent->parent))
#foreach($parent->parent as $child)
<option value="{{ $child->supplier_code }}">{{ $child->supplier_code }}</option>
#endforeach
#endif
#endforeach
</select>
</div>
</div>
{{ Form::submit('Add to Cart', array('class' => "btn btn-dark btn-lg btn-block")) }}
</form>
This is my function
public function getAddToCart(Request $request, $id)
{
$menus_child = Menu::where('menu_id', 0)->with('menusP')->get();
$contacts = Contact::all();
$product = Product::find($id);
$supplier_code = $request->supplier_code;
$oldCart = Session::has('cart') ? Session::get('cart') : null;
$cart = new Cart($oldCart);
$cart->add($product, $product->id, $supplier_code);
$request->session()->put('cart', $cart);
return view('public.shopping-cart', ['products' => $cart->items, 'totalPrice' => $cart->totalPrice, 'menus_child' => $menus_child, 'contacts' => $contacts, 'supplier_code' => $supplier_code]);
}
This is my product list page where the user will click on a product to get its details.
<div class="col-xl-9 col-lg-8 col-md-12 product-list">
<div id="parent">
<div class="row grid-container">
#foreach($products as $product)
<?php
$cat_slug = "";
?>
#foreach($product->category as $category)
<?php
$cat_slug .= " ".$category->slug
?>
#endforeach
<?php
$product_image = getImagesArray($product->image);
?>
<div class="product_items col-xl-2 col-lg-4 col-md-4 col-sm-4 {{ $cat_slug }}">
<a href="{!! route('product.item', [$product->slug]) !!}">
<div>
#if(!empty($product_image))
<img src={!! "product_images/products/$product_image[0]" !!}>
#endif
<p>
{!! $product->title !!}
</p>
</div>
</a>
</div>
#endforeach
</div>
</div>
</div>
I'm not sure if I need to add anything else, so if I do need to please let me know
Change this:
<a href="{!! route('product.item', [$product->slug]) !!}">
to this:
<a href="{{ route('product.item', $product->id) }}">

Laravel 5.2 How to populate edit form with multiple select?

I want to create a zone which contain few cities. So I decided to use jQuery Select2
Here is my create form multiple selection
<div class="form-group {{ $errors->has('cities') ? 'has-error' : '' }}">
<label>Tentukan Kota</label>
<select name="cities[]" class="city form-control" data-placeholder="Pilih Kota" style="width: 100%;" multiple="multiple">
#foreach($cities as $city)
<option value="{{ $city->id }}">{{ $city->name }}</option>
#endforeach
</select>
</div>
I can multiple select just like in documentation.
Here is my controller which handle display create form
public function zone_create()
{
$cities = City::where('zone_id', null)->get();
return view('backend.admin.pricings.zone_create', compact('cities'));
}
The relationship is One Zone Has Many City.
class Zone extends Model
{
protected $fillable = [
'name',
];
public function cities(){
return $this->hasMany(City::class);
}
}
The city belongs to zone
class City extends Model
{
protected $fillable = [
'zone_id',
'name',
];
public function zone(){
return $this->belongsTo(Zone::class);
}
}
Here is my edit method
public function edit($id)
{
$zone = Zone::find($id);
$cities = City::all();
return view('backend.admin.pricings.zone_edit', compact('zone', 'cities'));
}
Here is my edit form so far
<div class="form-group {{ $errors->has('cities') ? 'has-error' : '' }}">
<label>Tentukan Kota</label>
<select name="cities[]" class="city form-control" data-placeholder="Pilih Kota" style="width: 100%;" multiple="multiple">
//load option from cities table
//set selected the city belongs to zone
//the other city which don't belong to zone still here for option
</select>
</div>
But how I can populate my edit form (multiple selection) with City belongs to zone?
Just like this in my view
<select name="cities[]" class="city form-control" data-placeholder="Pilih Kota" style="width: 100%;" multiple="multiple">
#foreach($cities as $city)
#if(in_array($city->id, $zoneCityIds))
<option value="{{ $city->id }}" selected="true">{{ $city->name }}</option>
#else
<option value="{{ $city->id }}">{{ $city->name }}</option>
#endif
#endforeach
</select>
and like this in my controller
public function zone_edit($id)
{
$zoneCityIds = [];
$zone = Zone::find($id);
$cities = City::all();
foreach($zone->cities as $zoneCity)
{
$zoneCityIds[] = $zoneCity->id;
}
return view('backend.admin.pricings.zone_edit', compact('zone', 'cities', 'zoneCityIds'));
}
actually it's jus about option tag selected="true"
for multiple values simply use
<div class="form-group">
{!! Form::label('Categories') !!}<br />
{!! Form::select('categories[]', $cost_centers,
$post->categories->pluck('id')->toArray(),
['class' => 'form-control',
'multiple' => 'multiple']) !!}
</div>
In your edit function you can get all the cities of that particular zone
$zone_cities = implode(',',$zone->cities->lists('name')->toArray());
and pass $zone_cities in your view.
return view('backend.admin.pricings.zone_edit', compact('zone', 'cities','zone_cities'));
You will have to use Form Model binding in your view
Read about laravel collective form model binding [here].1
You will open your form like this
Your Edit form will be like this
{!! Form::label('cities','Cities: ') !!}
{!! Form::text('cities',$zone_cities,['class'=>'input','id'=>'cities','aria-required'=>'true','data-seperator'=>',']) !!}

Resources