How to get Select Value in Laravel - php

Hello I want to try to get the value of the selected option but i always getting a null value when i dump out the variable.
My Select looks like this:
<div class="col-md-6">
<!--<input id="members" type="text" class="form-control" name="members">-->
<select name="members" class="js-example-basic-multiple js-states form-control" id="members" multiple="multiple" >
<!-- #foreach($users as $user)
<option value='{{$user->id}}'>{{$user->name}}</option>
#endforeach-->
<option value="1">A</option>
<option value="2">B</option>
<option value="3">C</option>
<option value="4">D</option>
</select>
#if ($errors->has('members'))
<span class="help-block">
<strong>{{ $errors->first('members') }}</strong>
</span>
#endif
</div>
And my Controller function looks like this:
$sprintname = $request->input('sprintname');
$startdate = $request->input('startdate');
$enddate = $request->input('enddate');
$members = $request->input('members');
dd($members);
I really dont know whats wrong and i hope someone can help me.
The other inputs are fine but only with the select i get a null value.

you used multiple select try name="members[]"
and in Controller
public function Postdata(Request $request) {
$value = $request->members; }
Or you can do:
dd( request()->all() );
To check all of the data.

If you are using a model define it in the controller and in the Routes.php file.
Here what I have used in my sample project.I haven't use a controller for this.Got good results for laravel 5.0,5.1 and 5.2.
Category.php (this is the model in app directory)
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Input;
class Category extends Model
{
protected $fillable = ['name'];
protected $table = 'categories';
public function category()
{
return $this->belongsTo('App\Category');
}
}
in Routes.php
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Input;
use App\Category;
Route::get('/',function()
{
$categories = Category::all();
return view('category.index')->with('categories',$categories);
});
in the view
index.blade.php
<div class="form-group">
<label for="">Categories</label>
<select class="form-control input-sm" name="category" id="category">
#foreach($categories as $category)
<option value="{{$category->id}}">{{$category->name}} </option>
#endforeach
</select>
</div>
This work like a charm.Hope this information will be helpful for you!

Related

Passing variable from controller in components/blade

i have this situation.
Undefined variable: city (View: \newjoblist\resources\views\components\hero.blade.php)
in my controller i have
<?php
namespace App\Http\Controllers;
use App\Models\City;
use Illuminate\Http\Request;
class CitiesController extends Controller
{
public function index(Request $request){
$city = City::orderBy('name') // variable displayed in view
->get();
// return view('components.hero')->with('city',$city);
$this->view('<components.hero')->with('city', $this->city);
}
}
and in components/hero.blade.php
<select class="form-group" id="s" name="s">
<option value=""></option>
#foreach ($city as $cities)
<option value="{{$cities}}">{{$cities->name}}</option>
#endforeach
</select>
i don't know if i need to do something in web.php
you need to update your index method to
public function index(Request $request){
$cities = City::orderBy('name') // variable displayed in view
->get();
return view('components.hero')->with('cities',$cities);
}
and at the view
<select class="form-group" id="s" name="s">
<option value=""></option>
#foreach ($cities as $city)
<option value="{{$city->id}}">{{$city->name}}</option>
#endforeach
</select>
You can also do this
public function index(Request $request)
{
$cities = City::orderBy('name') // variable displayed in view
->get();
return view('components.hero', compact('cities'));
}
You pass in your code city as class membervariable but previously you assign only to $city.
Change this line: $this->view('<components.hero')->with('city', $this->city); to view('components.hero')->with('city',$city); . What you already had a line above ;-)
You can also pass Vars to your blade like that:
view('components.hero', ['city' => $city];
I read the comment i find out that you are trying another way to get the data.
So you can do it like this.
<?php
namespace App\Http\Controllers;
use App\Models\City;
use Illuminate\Http\Request;
class CitiesController extends Controller
{
private $city;
public function __construct() {
$this->city = new City();
}
public function index(Request $request) {
$cities = $this->city->query()->orderBy('name')->get();
return view('<components.hero')->with('cities', $cities);
}
}
And in the view:
<select class="form-group" id="s" name="s">
<option value=""></option>
#foreach ($cities as $city)
<option value="{{ $city->id }}">{{ $city->name }}</option>
#endforeach
</select>
is it possible to continue the error because I want to display in components.hero where is another controller? this is my web.php....
Route::get('/', [Controllers\ListingController::class, 'index']) ->name('listings.index');
and i have others views in this controller
UPDATE*
if I use diff. the route in web.php it's works
Route::get('/search', [Controllers\CitiesController::class, 'index']);
-> displaying cities from DB

Livewire - Increased time each time doing request

Every time I do the same action request, the runtime keeps increasing. Is there any way to fix it?
in my blade :
<div class="col-md-6">
<div class="form-group #error('promotion') has-error #enderror required">
<label>Promotion</label>
<select class="full-width" wire:model="promotion" wire:change="select_promotion">
<option></option>
#foreach($data_promotion as $data)
<option value="{{$data->promotion_id}}" {{$promotion==$data->promotion_id?'selected':''}}>{{$data->promotion_name}}</option>
#endforeach
</select>
<x-jet-hold wire:loading.class="visible"></x-jet-hold>
<x-jet-loading wire:loading.class="visible" wire:target="select_group"></x-jet-loading>
</div>
#error('promotion')
<label id="promotion-error" class="error" for="promotion">{{ $message }}</label>
#enderror
</div>
my method :
public function select_promotion()
{
$this->type_proposal_tap = 'normal';
$this->modal = 0;
$this->source_budget = 1;
$promotion = PromotionNames::select('mst_promotion.*','mst_promotion_form.form_field','mst_promotion_form.form_upload')
->where(['mst_promotion.promotion_id'=>$this->promotion])
->join('mst_promotion_form','mst_promotion_form.form_id','mst_promotion.form_id')->first();
----other code----
}
Doing the same thing, i.e. selecting select option and calling select_promotion() method, livewire time request keep increasing like this :
After searching for a long time, I noticed that the method call performed an unusual execution, so it looks like the same data query is being executed like this:
How to stop it? even i don't have process in method mount() or render()
you don't need to use both wire directive (model and change) in select element. If you use the property for bind to backend you can get the lifecycle hook of it like
<div class="col-md-6">
<div class="form-group #error('promotion') has-error #enderror required">
<label>Promotion</label>
<select class="full-width" wire:model="promotion">
<option></option>
#foreach($data_promotion as $data)
<option value="{{$data->promotion_id}}">{{$data->promotion_name}}</option>
#endforeach
</select>
//............
</div>
public function updatedPromotion($value)
{
dd($value);
$this->select_promotion();
}
if you attempt to use the change action you can do this
<select class="full-width" wire:change="$emit('select_promotion', $event.target.value)">
<option></option>
//.......
protected $listeners = [
'select_promotion'
];
public function select_promotion($value)
{
$this->promotion = $value;
//..... the rest of this method
}

How to call a view through an item on the select menu?

Here is the view that displays the Select Menu that will call another View according to the selected filter:
<form action="<?= base_url() ?>process/processfilter" method="post" >
<div class="col-md-2" >
<div class="form-group">
<label>Filter</label>
<select class="form-control" name="situation" required autofocus>
<option value="All">List All</option> Here I'd like to call another method that has no filter.
<option value="Processing">Processing</option>
<option value="Concluded">Concluded</option>
<option value="Archived">Archived</option>
</select>
</div>
</div>
</form>
Function in Controller Class:
public function processfilter($situation) {
$this->load->model('ProcessModel', 'processo');
$dados['process'] = $this->process->getProcess($situation);
if ($this->process->getProcess($situation)) {
$this->load->view('/reports/process', $dados);
} else {
redirect('dashboard', 'refresh');
}
}
Function in Class Model:
function getProcesss($situation) {
$this->db->select('*');
$this->db->where("situation", $situation);
return $this->db->get('process')->result();
}
Note: I need to make a filter in the View like those in Excel.

Laravel ErrorException, Performing Multiple dynamic drop down

I have four tables:
asset_category table(columns 'asset_category_id', 'category'), an assets table(columns 'asset_id','asset_category_id','manufacturer_id', 'department_id'),
manufacturers table (columns manufacturers_id, manufacturer) and departments table (columns department_id, department). The asset table columns are linked with the other three table. I'm performing a dynamic drop down in the form to insert into the asset table
Asset.blade.php
<form action="{{url('/insertAsset')}}" method="POST" edata-parsley-validate novalidate>
{{ csrf_field() }}
<div class="form-group">
<label for="userName">Asset ID*</label>
<input type="text" name="asset_id" parsley-trigger="change" required placeholder="Asset ID" class="form-control" id="userName" disabled>
</div>
<div class="form-group">
<label for="userName">Asset Category ID*</label>
<select name="asset_category_id" parsley-trigger="change" required placeholder="Asset Category ID" class="form-control">
<option>Select an Asset Category</option>
#foreach( $asset_categories as $asset_category )
<option value=" {{$asset_category->asset_category_id}}"> {{ $asset_category->category }} </option>
#endforeach
</select>
</div>
<div class="form-group">
<label for="userName">Asset Manufacture ID*</label>
<select name="manufacturer_id" parsley-trigger="change" required class="form-control">
<option>Select a Manufacturer</option>
#foreach( $manufacturers as $manufacturer )
<option value=" {{$manufacturer->manufacturer_id}}"> {{ $manufacturer->manufacturer }} </option>
#endforeach
</select>
</div>
<div class="form-group">
<label for="userName">Department ID*</label>
<select name="department_id" parsley-trigger="change" required placeholder="Department ID" class="form-control" >
<option>Select a Department</option>
#foreach( $departments as $department )
<option value=" {{$department->department_id}}"> {{ $department->department }} </option>
#endforeach
</select>
</div>
<div class="form-group text-right m-b-0">
<button class="btn btn-primary waves-effect waves-light" type="submit">
Submit
</button>
</div>
AssetController
<?php
namespace App\Http\Controllers;
use App\Asset;
use App\Asset_category;
use App\Manufacturer;
use App\Department;
use Illuminate\Http\Request;
class AssetController extends Controller
{
public function asset_category(){
$asset_categories = Asset_category::all();
return view('asset', ['asset_categories' => $asset_categories]);
}
public function manufacturer(){
$manufacturers = Manufacturer::all();
return view('asset', ['manufacturers' => $manufacturers]);
}
public function department(){
$departments = Department::all();
return view('asset', ['departments' => $departments]);
}
}
Web.php
<?php
Route::get('/asset', function (){
return view('asset');
} );
Route::get('/asset', 'AssetController#asset_category');
Route::get('/asset', 'AssetController#department');
Route::get('/asset', 'AssetController#manufacturer');
/*POST */
Route::post('/insertAsset', 'AssetController#add');
When run the application, i get an ErrorException which states:
Undefined variable: asset_categories (View: C:\xampp\htdocs\laravel_app\resources\views\asset.blade.php).
After thorough testing i find out that, just one drop down displays because the laravel routes only one variable from one function among the 3 functions in the web.php and the other two variable from the other functions are not sent to the asset.blade.php. So now i want a way to route the other variables to the asset.blade.php. Please help if you understand it. Thanks in advance.
The first thing is that Laravel will only match the first route with the specific ID and specific type. In these case will only find the first route GET /asset.
To do what I undestand you want. You need to create only one method, that will return the three variables to the view
public function asset(){
$asset_categories = Asset_category::all();
$manufacturers = Manufacturer::all();
$departments = Department::all();
return view('asset', ['asset_categories' => $asset_categories,
'manufacturers' => $manufacturers, 'departments' => $departments]);
}
And only one route
Route::get('/asset', 'AssetController#asset');

Laravel 5.1 - Store input data in my session

I have a view show.php of my single product, when user click on ADD PRODUCT my controller CartController.php update my session cart and update my cartSession table,
Now i'm tryng to put new data (Color input,Size input, Quantity input) on my session and also in my CartSession table. But i dont know why it doesn't work.
-I think that the principal problem is that $request->get('input') doesn't pass to my CartController.php , i tried to return
$request->all() and there is not nothing.
CartController.php
namespace dixard\Http\Controllers;
use Illuminate\Http\Request;
use dixard\Http\Requests;
use dixard\Http\Controllers\Controller;
use dixard\Product;
use dixard\CartSession;
use dixard\CartItem;
use dixard\Shipping;
use Session;
// i think that all classes are ok
public function add(Product $product, CartSession $CartSession,Request $request)
{
$id = $request->get('id');
$cart = \Session::get('cart');
$product->quantity = $request->get('qty');
$product->size = $request->get('size');
$product->color = $request->get('color');
$cart[$product->id] = $product;
\Session::put('cart', $cart);
return $request->all(); // here i tried to get all inputs but it shows me nothing result
$subtotal = 0;
foreach($cart as $producto){
$subtotal += $producto->quantity * $producto->price;
}
$session_code = Session::getId();
$CartSession = new CartSession();
$session_exist = CartSession::where('session_code', $session_code)->orderBy('id', 'desc')->first();
if (isset($session_exist)) {
$s = new CartSession;
$data = array(
'subtotal' => $subtotal,
);
$s->where('session_code', '=', $session_code)->update($data);
}else {
$CartSession = new CartSession();
$CartSession->session_code = $session_code;
$CartSession->subtotal = $subtotal;
$CartSession->save();
}
//return $cart;
//salveremo tutte le informazioni nel array cart nella posizione slug
foreach($cart as $producto){
$this->saveCartItem($producto, $CartSession->id, $session_code, $cart);
}
return redirect()->route('cart-show');
}
Routes.php
Route::bind('product', function($id) {
return dixard\Product::where('id', $id)->first();
});
Route::get('cart/add/{product}', [
'as' => 'cart-add',
'uses' => 'CartController#add'
]);
show.php view
Get Data about the product is ok, title, description, color avaible, price ecc. all information pass to my view.
{!! Form::open(['route'=> ['cart-add', $product->id],'class'=>'form-horizontal form-label-left'])!!}
<input type="hidden" name="_method" value="PUT">
<input type="hidden" name="id" value="{{$product->id}}">
<div class="row">
<div class="col-md-4">
<div class="form-group">
<label for="p_color">Colore</label>
<select name="color" id="p_size" class="form-control">
#foreach($colors as $color)
<option value="{{ $color->color }}">{{ $color->color }}</option>
#endforeach
</select>
</div>
</div>
<div class="col-md-4">
<div class="form-group">
<label for="size">Size</label>
<select name="size" id="p_size" class="form-control">
<option value="XS">XS</option>
<option value="S">S</option>
<option value="M">M</option>
<option value="L">L</option>
<option value="XL">XL</option>
</select>
</div>
</div>
<div class="col-md-3">
<div class="form-group">
<label for="qty">Quantity</label>
<select name="qty" id="p_qty" class="form-control">
<option value="">1</option>
<option value="">2</option>
<option value="">3</option>
</select>
</div>
</div>
</div>
<div class="product-list-actions">
<span class="product-price">
<span class="amount">{{$product->price}}</span>
<input type="submit" class="btn btn-lg btn-primary" >
ADD PRODUCT
</input>
</div><!-- /.product-list-actions -->
{!! Form::close() !!}
Thank you for your help!
You have to explicitly set the post-method to be "get" or you need to change your router to accept the request as post. Even though you're calling the route by name, Form::open defaults to "POST"
https://laravelcollective.com/docs/5.2/html#opening-a-form
Option 1. Change
Route::get('cart/add/{product}',...
to
Route::post('cart/add/{product}',...
Option 2. Change
{!! Form::open(['route'=> ['cart-add', $product->id],'class'=>'form-horizontal form-label-left'])!!}
to
{!! Form::open(['method'=>'GET', 'route'=> ['cart-add', $product->id],'class'=>'form-horizontal form-label-left'])!!}

Categories