hendraspt hendraspt - 1 month ago 7
PHP Question

How to solve the "Undefined variable" error in laravel?

I am using laravel, and I have 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);
}

Answer

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