Home > Net >  I am Stuck at Undefined variable error when accessing data in view laravel
I am Stuck at Undefined variable error when accessing data in view laravel

Time:03-23

I wanted to access the item formName in the array $data. Am getting the error $formName is undefined in the view.I've tried everything that is commented also in the below given code but nothing works , everything gives the Undefined variable: formName error only. Tried clearing all the cache also.Actually not only, formName, no items like formId,dataTypes are accessible from this array. Any one know how to fix this?Any help is much appreciated.

Controller

use Illuminate\Support\Facades\View;

$data=['formName'=>$data['name'],'formId'=>$data['id'],'dataTypes'=>$dataTypes,'fields'=>$fields];
//dd($data);
return View::make('fieldList')->with($data);
//return View::make('fieldList',$data);
//return view('fieldList',['formName'=>$data['name'],'formId'=>$data['id'],'dataTypes'=>$dataTypes,'fields'=>$fields]);

/*$formName=$data['name'];
$formId=$data['id'];
$compactData=array('formName', 'formId', 'dataTypes','fields');
return View::make("fieldList", compact($compactData));*/

View

<input  id="exampleInputEmail1" name="formName" readonly value="{{$formName}}">

dd($data) shows the below given result

array:4 [▼
  "formName" => "fg"
  "formId" => 44
  "dataTypes" => array:9 [▶]
  "fields" => Illuminate\View\View {#329 ▼
    #factory: Illuminate\View\Factory {#324 ▶}
    #engine: Facade\Ignition\Views\Engines\CompilerEngine {#325 ▶}
    #view: "fieldList"
    #data: array:1 [▼
      "fields" => []
    ]
    #path: "C:\wamp\www\Laravel\forms\resources\views/fieldList.blade.php"
  }
]

EDIT

in cases where my data is like below that i need a foreach in view like @foreach($data as $form) {{$form['name']}} @endforeach to access items, i am able to access the data. In other cases like above where no indexed array is present, it's unable to access data in view.

[▼
  0 => array:6 [▼
    "id" => 1
    "name" => null
    "created_at" => "2022-03-22T13:48:59.000000Z"
    "updated_at" => "2022-03-22T13:48:59.000000Z"
    "deleted_at" => null
    "fields" => []
  ]

CodePudding user response:

controller:

return view(fieldList,['data'=>$data]);

blade file:

 value="{{$data['formName']}}"

CodePudding user response:

$compactData=array('formName', 'formId', 'dataTypes','fields');

The line of code above returns an indexed array of strings.

array (
  0 => 'formName',
  1 => 'formId',
  2 => 'dataTypes',
  3 => 'fields',
)

Which isn't your actual intention.

You should instead try this:

view($view = null, $data = [], $mergeData = [])

Get the evaluated view contents for the given view.

Controller

return view("fieldList", $data);

View

<input  id="exampleInputEmail1" name="formName" readonly value="{{$formName}}">
  • Related