-1

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);
}
krlzlx
  • 5,752
  • 14
  • 47
  • 55
hendraspt
  • 959
  • 3
  • 19
  • 42
  • The Select probably returns nothing which makes the 0 index invalid/undefined – RST Nov 07 '16 at 15:04
  • 1
    Possible duplicate of [PHP: "Notice: Undefined variable" and "Notice: Undefined index"](http://stackoverflow.com/questions/4261133/php-notice-undefined-variable-and-notice-undefined-index) – u_mulder Nov 07 '16 at 15:05
  • 1
    `return("view.blade")->with(["getFormTest" => $getFormTest]);`: This will allow you to access `{{ $getFormTest }}` in the view. – Tim Lewis Nov 07 '16 at 15:07

2 Answers2

1

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
j_freyre
  • 4,623
  • 2
  • 30
  • 47
0

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

Saumya Rastogi
  • 13,159
  • 5
  • 42
  • 45