Lilan
Lilan

Reputation: 163

unable to print table data on blade file in laravel

need print Task table task_name column values in index.blade.php file

this is index.blade.php

@if(isset($tasks))
@foreach ($tasks as $task)
<h1>{{ $task->task_name }}</h1>
@endforeach
@endif

TasksController.php is

 public function index()
{
    $tasks = Task::all();

   return view('tasks.index')->withTasks($tasks);
}

No any error message but do not print data...how to fix

Upvotes: 1

Views: 423

Answers (2)

Mayank Pandeyz
Mayank Pandeyz

Reputation: 26258

Change:

return view('tasks.index')->withTasks($tasks);

to

return view('tasks.index')->with('tasks', $tasks);

or

return view('tasks.index', array('tasks' => $tasks));

and try again.

Explanation:

The second parameter of the view function is the the array that contains the data in it on different indexes. As an alternative to passing a complete array of data to the view helper function, you may use the with method to add individual pieces of data to the view.

Reference

Upvotes: 1

Bibhudatta Sahoo
Bibhudatta Sahoo

Reputation: 4894

Just replace

return view('tasks.index')->withTasks($tasks);

with

return view('tasks.index',['tasks'=>$tasks]);

And try again.

Upvotes: 0

Related Questions