Laravel store Multiple input value for each item - php

I want to store each member's shopping day using a laravel collection, but it doesn't show me each member's data, it only shows one user's data:
Blade template:
#foreach($members as $member)
<div class="form-group row">
<label class="col-sm-4 col-form-label">{{$member->name}}</label>
<div class="col-sm-8">
<select style="background-color: #e6e9f1" class="js-select2 form-control" name="day[{{$member->id}}][]" style="width: 100%;" data-placeholder="Choose single or multiple day" multiple="">
<option></option>
#foreach($days as $day)
<option value="{{$day->name}}">{{$day->name}}</option>
#endforeach
</select>
</div>
</div>
#endforeach
Controller:
public function assign(Request $request)
{
$collections = collect($request->day);
foreach ($collections as $collection){
return $collection;
}
}

Here $collections get both two users data. But when 1st iteration in loop it returned that user's (first user) data and next iteration will never happen as function has already returned data. You can Just return the $collections variable. You will find all there.

Related

Laravel 5.4 Undefined variable error - problem with controller?

I'm getting this error: Undefined variable: sales_rep_names (View: C:\wamp64\www\VLCMRenewals\resources\views\search\index.blade.php)
when I try adding a new search dropdown in my blade. This is the new code I've added that's causing the problem:
{{--select Sales Representative --}}
<!-- adding sales rep placeholder
Can we make this a drop-down menu?
need to make sure "name=" is correct -->
<div class="form-group col-md-4">
<label class="mr-sm-2" >Sales Representative</label>
<select class="custom-select mr-sm-2" name="primary_sales_rep">
<option selected></option>
#if(count($sales_rep_names) >0)
#foreach ($sales_rep_names as $sales_rep_name)
<option value='{{$sales_rep_name->Primary_Sales_Rep}}'>{{$sales_rep_name->Primary_Sales_Rep}}</option>
#endforeach
#endif
</select>
</div>
And here's an example of a dropdown that DOES work (I copied the format):
{{--select Manufacturer --}}
<div class="form-group col-md-4" >
<label class="mr-sm-2" >Manufacturer Name</label>
<select class="custom-select mr-sm-2" name="company">
<option selected></option>
#if(count($manufacturer_names) >0)
#foreach ($manufacturer_names as $manufacturer_name)
<option value='{{$manufacturer_name->Manufacturer_Name}}'>{{$manufacturer_name->Manufacturer_Name}}</option>
#endforeach
#endif
</select>
</div>
Lastly, here's the code I have my in Controller (manufacturer and customer both work):
public function index()
{
// will need to add Sales Rep here
$customer_names = DB::table('dbo.contract_view')
->distinct()
->orderBy('Customer_Name','asc')
->get(['Customer_Name']);
$manufacturer_names = DB::table('dbo.contract_view')
->distinct()
->orderBy('Manufacturer_Name','asc')
->get(['Manufacturer_Name']);
// when I tested with product part number it also gave me an error with index.blade.php. maybe this is the wrong spot?
$sales_rep_names = DB::table('dbo.contract_view')
->distinct()
->orderBy('Primary_Sales_Rep','asc')
->get(['Primary_Sales_Rep']);
return view('search.index', ['customer_names' => $customer_names], ['manufacturer_names' => $manufacturer_names], ['sales_rep_names' => $sales_rep_names]);
}
Any advice?
You are sending the variables as 3 different arrays separated by commas. The view parameters are $view, $data, $mergeData, so the forth parameter you are sending is ignored.
The correct way to pass variables to the view is like this:
return view('search.index', [
'customer_names' => $customer_names,
'manufacturer_names' => $manufacturer_names,
'sales_rep_names' => $sales_rep_names
]);
or
return view('search.index')
->with(compact('customer_names', 'manufacturer_names', 'sales_rep_names');
or
return view('search.index')
->with('customer_names', $customer_names)
->with('manufacturer_names', $manufacturer_names)
->with('sales_rep_names', $sales_rep_names)

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 use a variable as "value" attribute in <option> tag

I'm currently using Laravel and I want to have a dropdown box display category names from my database, but when selected the value it returns to the server should be a numerical value of the name selected which is also stored in the database.
Like so:
#foreach($categories as $category)
<option name="pid" value="$category->id" id="cname">{{$category->name}}</option>
#endforeach
The values are passed from my Controller to the Blade template. As you can see, I want it to display the category names in the drop down (which it does fine), but once selected and submitted, I want the category ID to be returned. Since the "value" attribute returns the value, I want to have the variable $category->id as the "value". I want to do this without using JavaScript or jQuery. Only with PHP. How would I go about accomplishing this and is it even possible?
When I try to use value="{{$category->id}}" I get the Laravel error:
Integrity constraint violation: 1048 Column 'parent_id' cannot be null
So it seems like it isn't submitting the value of the dropdown.
Here is the code I'm currently using in my Blade template:
<form class="" action="{{route('createsubcategorypost')}}" method="post">
<div class="form-group">
<label for="cname">Sub-Category name:</label>
<input type="text" name="cname" value="" placeholder="Sub-Category Name" id="cname" class="form-control">
</div>
<div class="form-group">
<label for="pid">Parent ID</label>
<select class="form-control">
#foreach($categories as $category)
<option name="pid" value="{{$category->id}}" id="pid">{{$category->name}}</option>
#endforeach
</select>
</div>
<div class="form-group">
<center>
<button type="submit" name="button" class="btn btn-success">Create Sub-Category</button>
</center>
</div>
{{csrf_field()}}
</form>
And this is the code in my Controller:
public function CreateSubCategoryPost(Request $request){
if ($request->cname == null) {
session()->flash('errormessage',' Invalid sub-category name.');
return redirect()->back();
}
$scc = SubCategory::where('name',$request->cname)->first();
if ($scc !== null) {
session()->flash('errormessage',' Sub-category with that name already exists.');
return redirect()->back();
}
$subCategory = new SubCategory;
$subCategory->name = $request->cname;
$subCategory->uniqueid = 'SCA'.str_random(28);
$subCategory->slug = str_slug($request->cname,'-');
$subCategory->parent_id = $request->pid;
$subCategory->save();
return redirect()->route('categories');
}
Same way you you are displaying the category name inside the option tag :
#foreach($categories as $category)
<option name="pid" value="{{$category->id}}" id="cname">{{$category->name}}</option>
#endforeach
Everything you call from a database in Laravel should have the format {{$category->someName}} no matter where you place it in your view. In your case you need to enclose $category->id in {{ }}

How do i send and store values of a dynamically populated select field with Laravel framwork

I'm working with Laravel 5.4 and I have a select field that is dynamically populated from a department's table.
EmployeeController create model
public function create()
{
$departments = Department::orderBy('name', 'ASC')->get();
return view('emp.create', compact('departments'));
}
Select from field dynamically populated
<div class="form-group">
<label for="department" class="col-md-4 control-label">Department</label>
<div class="col-md-6">
<select name="dept_id" id="dept" class="form-control" required autofocus>
<option></option>}
#foreach ($departments as $department)
<option value="{{ $department->id }}">{{ ucfirst($department->name )}}</option>
#endforeach
</select>
</div>
In my employee's table, I have the dept_id column which references the department's id column. Every time I submit the form, 0 is inserted in the employees dept_id column instead of the actual value. When I dd($request) I can see the actual department.id being sent in the request array.
EmployeeController store model
public function store(Request $request)
{
//dd($request);
$employee = Employee::create(request()->all());
}
What's the reason the query inserts 0 everytime?

Populate multi select with selected values in laravel 5.2

I have stored multiselect values in database as string
Now while trying to edit the values in need to fetch this highlighted string as array and send it to edit view , so that i can populate multi select with this selected values.
My code in view for multi select
<div class="form-group">
<label class="control-label col-md-3">News For
<span class="required" aria-required="true"> * </span>
</label>
<div class="col-md-4">
{!! Form::select('news_todisplay[]',['users'=>'Users','staff'=>'Staff','cinemahall'=>'Cinemahall'],$todisplayarray,array('class'=>'form-control','multiple'=>'multiple')) !!}
</select>
</div>
</div>
Where $todisplayarray is the array fetched from database i.e they are users,staff, cinemahall
My controller code
public function edit($id)
{
$newsdetails=General_news::findOrFail($id);
$todisplayarray=explode(',',$newsdetails->news_todisplay);
return view('admin.editnews',compact('newsdetails','todisplayarray'));
}
Page view-source shows this
<select class="form-control" multiple="multiple" name="news_todisplay[]"><option value="users">Users</option><option value="staff">Staff</option><option value="cinemahall" selected="selected">Cinemahall</option></select>
This only shows last value as selected, where as it should display 3 of the values as selected.
Kindly let me know where I am going wrong.
Find the below answer
<?php
//get the old values from form
$old = old('category');
//get data from database table field
$cate= explode(',',$data->category_ids);
//stay the values after form submission
if($old){ $cate=$old; }
?>
<select id="categories" name="category[]" multiple>
#foreach ($categories as $val)
<option value="{{ $val->id }}" <?php echo in_array($val->id,$cate)?"selected" :"" ;?> >{{ ucfirst($val->category_name) }}</option>
#endforeach
</select>
I've used loop the categories data with id's and match the data stored in the table

Categories