ErrorException: Undefined variable laravel - php

I'm trying to pass variables from controller to view i tried both compact and with but nothing works, i get this error:
ErrorException undefined variable: programs.
Controller
public function getManageCourse(){
$programs = Program::all();
$academics = Academic::orderBy('academic_id','DESC')->get();
return view(('courses.manageCourse'),compact('programs','academics'));
}
View
<div class="col-sm-4">
<label for="program">Course</label>
<div class="input-group">
<select class="form-control" name="program_id" id="program_id">
<option value="">------------</option>
#foreach($programs as $key =>$p)
<option value="{{$p->$program_id}}">{{$y->program}}</option>
#endforeach
</select>
<div class="input-group-addon">
<span class="fa fa-plus" id="add-more-program"></span>
</div>
</div>
</div>
I have looked into similar problems but i didn't find a solution, why is this happening? what am i doing wrong?

I found the solution i was doing $p->$program_id instead of $p->program_id

make it like this:
return view('courses.manageCourse',compact('programs','academics'));
not like you are doing it:
return view(('courses.manageCourse'),compact('programs','academics'));

write
return view('courses.manageCourse',compact('programs','academics'));
and
<option value="{{$p->$program_id}}">{{$p->program}}</option>
instead of
<option value="{{$p->$program_id}}">{{$y->program}}</option>

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
}

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');

Undefined property: Illuminate\Database\Eloquent\Collection::$name Laravel 5.3

The Route is Route::resource('/admin/post', 'postController');
in controller the code is
public function index()
{
$s = Singer::where('id','<',5)->get()->name;
return view("admin.new-post", ['s'=>$s]);
}
And the view is
<select class="form-control select2 select2-hidden-accessible" multiple="" data-placeholder="Select a State" style="width: 100%;" tabindex="-1" aria-hidden="true">
<?php foreach ($s as $ss){
echo '<option>'. $ss .'</option>';
}?>
</select>
I want to list down all the names of singers from data base in select. And the problem is that when I get a single value using first() in query instead of get() it returns single value correctly widhout any error. but when I use get() and in view use foreach loop to get all singers then it gives this error. Undefined property: Illuminate\Database\Eloquent\Collection::$name. I have reviewed code many times but could not understand that what the error is. while there is no problem to get a single name.. please provide me some solution.
Controller Code
public function index()
{
$s = Singer::where('id','<',5)->get();
return view("admin.new-post", ['s'=>$s]);
}
View Code
<select class="form-control select2 select2-hidden-accessible" multiple="" data-placeholder="Select a State" style="width: 100%;" tabindex="-1" aria-hidden="true">
<?php foreach ($s as $ss){
echo '<option>'. $ss->name .'</option>';
}?>
</select>
When you use first() you get a single result, when you use get() you are getting a collection of results, even if your searching for one user.

How to get Select Value in Laravel

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!

Categories