How to solve the "Undefined variable" error in Laravel? - php

I am using Laravel, and I got an error:
Undefined variable: getFormTest (View: C:\xampp\htdocs\survey\resources\views\tambahformtest.blade.php)
That error references to this view:
<input value="{{ $getFormTest[0]->ms_test }}">
I have put $getFormTest in my controller:
public function TambahFormTest()
{
$ms_id = FormTest::max('ms_id');
$getFormTest = FormTest::Select('ms_test')->where('ms_id', '=', $ms_id)->get();
return view('tambahformtest', $getFormTest);
}

When returning a view in laravel, you have to pass an array of params.
return view ('myView', ['param1' => $v1, 'param2', $v2]);
then in your view
#if(isset($param1)
{{ $params->property }}
#endif

You should take a use of compact method of php
public function TambahFormTest()
{
$ms_id = FormTest::max('ms_id');
$getFormTest = FormTest::Select('ms_test')->where('ms_id', '=', $ms_id)->get();
return view('tambahformtest', compact('getFormTest'));
}
This would be sent to view as - ['getFormTest' => $getFormTest]
Hope this helps

Related

Cannot pass full array from controller in laravel to a view using redirect()

I am unable to solve passing of array issue
below is my function in controller
public function fetchData($id)
{
$id=base64_decode(urldecode($id));
prod_detail=ProductDetail::select('prod_id','supplier_id','price','open_stock','discount_rate','min_order_level')->where('prod_id','=',$id)->get();
return redirect()->route('prod_d_view', compact($prod_detail));
}
below is my route
Route::get('/product_view', function(){
return view('/admin/product_d_mgt');
})->name('prod_d_view');
below is my error
Undefined variable: prod_detail (View: \admin\product_d_mgt.blade.php)
I am unable to pass the full array from one controller using redirect()->route() to another view
Maybe you can use something like this:
In your controller function:
...
return Redirect::to('product_view')->with('prod_detail', $prod_detail);
And in your product_view.blade.php file (in resources/view directory):
#if(Session::has('prod_detail'))
#foreach (Session::get('prod_detail')as $key => $value)
{{ $value->ColumnName }}
{{ $value->ColumnName2 }}
#endforeach
#endif
It has typo. Missing $ symbol before variable name prod_detail.
correct version:
public function fetchData($id)
{
$id = base64_decode(urldecode($id));
$prod_detail=ProductDetail::select('prod_id','supplier_id','price','open_stock','discount_rate','min_order_level')->where('prod_id','=',$id)->get();
return redirect()->route('prod_d_view', compact($prod_detail));
}

Undefined variable issue Laravel 5.4

I've got stuck with this error so if ever pls forgive me because I'm still new at laravel. I got this error
Undefined variable: clientTransactions (View:
C:\xampp\htdocs\dcgwapo\resources\views\service_details\create.blade.php)
but I have a right code but I still wondering why it is still undefined variable given I define it in my controller.
create.blade.php in service details code
<div class="form-group">
<label for="client_transaction_id">Client Trans ID: </label>
<select class="form-control" name="client_transaction_id">
#foreach ($clientTransactions as $clientTransaction)
<option value= "{{ $clientTransaction->id }}">
{{ $clientTransaction->id }}
</option>
#endforeach
</select>
</div>
ServiceDetailsController code
public function create()
{
$users = User::pluck('fname', 'lname', 'id');
$services = Service::pluck('name', 'id');
$clientTransactions = ClientTransaction::all();
return view('service_details.create', ['users' => User::all()], ['services' => Service::all()], ['clientTransactions' => ClientTransaction::all()]);
}
ServiceDetail.php model code
public function clientTransaction()
{
return $this->belongsTo(ClientTransaction::class);
}
I hope you can help me. Thanks!
You're sending variables to your view the wrong way. The seconds argument should be an array with all your variables. As of now your adding a new parameter to the view function for each variable.
view('view', [...], [...], [...])
It should be like this:
view('view', [...1, ...2, ...3])
So what you need to change is the return statement to this:
return view('service_details.create', ['users' => User::all(), 'services' => Service::all(), 'clientTransactions' => ClientTransaction::all()]);
Second parameter to view function accepts an associative array of data, you are passing an indexedArray of arrays, Just use this return statement and you are good to go. ;)
return view('service_details.create', [
'users' => User::all(),
'services' => Service::all(),
'clientTransactions' => ClientTransaction::all()
]);
You can use compact to pass data from controller to view:
public function create()
{
$users = User::pluck('fname', 'lname', 'id');
$services = Service::pluck('name', 'id');
$clientTransactions = ClientTransaction::all();
return view('service_details.create',compact('users','services','clientTransactions');
}

undefined variable php laravel 5.4

i kept getting the same error,
undefined variable: theaters on my blade, line 14.
here is my addcine.blade.php line 14
#foreach ($theaters as $theater)
<option value="{{ $theater->name }}"/>
#endforeach
here is my addcinemacontroller
public function index(){
$theaters = Theater::all();
return view('schedules.addcine', compact('name'));
and my route
Route::get('addcinema','AddCinemaController#index');
});
It should be:
return view('schedules.addcine', compact('theaters'));
compact('theaters') does exactly this:
return view('schedules.addcine', ['theaters' => $theaters]);
return view('schedules.addcine', compact('theaters'));
you are not sending the $theaters value to the view,
You are sending name instead of theaters in controller
public function index(){
$theaters = Theater::all();
return view('schedules.addcine', compact('theaters'));
You need to return the $thaters variable to view so that you can use it there.
Return like this:
return view('schedules.addcine', compact('theaters'));

Why my blade have Undefined Variable - Laravel 4.2

This is on my routes.php
Route::get('registration/verify/{confirmation}', ['as'=>'verify', 'uses'=>'HomeController#verify']);
Route::get('login', ['as'=>'login', 'uses'=>'HomeController#getLogin']);
and on my blade is this
then on my HomeController.php where the error belong
public function verify($confirmation)
{
$user = User::where('activation_code', '=', $confirmation)->first();
return Redirect::route('login')
->withInput(['email' => $user->email])
->with('fuck', 'wtf');
}
and I got error like this
Undefined variable: fuck (View: C:\Program Files (x86)\Ampps\www\tridg\local\app\views\auth\login.blade.php)
I don't know where I been wrong, I'm so confident that this is correct.
EDIT1:
I even tried this
public function verify($confirmation)
{
$user = User::where('activation_code', '=', $confirmation)->first();
// $user->active = 1;
// $user->save();
return Redirect::route('login', ['fuck'=>'wtf'])
->withInput(['email' => $user->email]);
}
Nevermind, The answer is {{ Session::get('var_name') }} not {{ $var_name }}

Passing arguments from route through view via controller doesn't work

I'm creating a Laravel 4 webapp and got the following route:
Route::get('products/{whateverId}', 'ProductController#index');
This is my index-function in ProductController:
public function index($whateverId)
{
$products = Product::all();
$data['whateverId'] = $whateverId;
return View::make('products', compact('products'), $data);
}
In my view, this returns the following error:
<p>Product: {{ $data['product'] }}</p>
ErrorException
Undefined variable: data (View: /Users/myuser/webapp/app/views/products.blade.php)
return View::make('products', compact('products'), "data"=>$data);
(or compact('data'))
Try passing it as:
$data['whateverId'] = $whateverId;
$data['products'] = Product::all();;
return View::make('products', $data);
And you'll have acces to it as
{{ foreach($products as ...) }}
and
{{ $whateverId }}
Or you can
$products = Product::all();
$data['whateverId'] = $whateverId;
return View::make('products')
->with('products', $products)
->with('whateverId', $whateverId);

Categories