1

need print column data in my laravel app. My controller is TaskController.php

public function index()
{
    $tasks = Task::all();
    return view('tasks.index')->with('tasks', $tasks);
    }

index.blade.php file is

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

I am going include this index view file in show.blade.php file as following

@include('tasks.index')

but unable to print table data no any errors occurred. how can I print task_name in show view file?

Lilan
  • 153
  • 1
  • 3
  • 14
  • `no errors occurred` really? Could it be because of all your @´s? https://stackoverflow.com/questions/1032161/what-is-the-use-of-the-symbol-in-php – Andreas Aug 01 '17 at 16:31
  • The `@` is part of Laravel's blade templating, it's not an error suppressor. – aynber Aug 01 '17 at 16:33
  • `@include('tasks.index')` includes just the blade file, not the data from the controller. Try passing the tasks in with the controller that returns the `show` blade. – aynber Aug 01 '17 at 16:35
  • do you have any solutions – Lilan Aug 01 '17 at 16:37
  • Can you show the controller that returns the show view? I can show you how to tweak that. – aynber Aug 01 '17 at 16:41
  • you mean may I put a function to TaskControler to show tasks data? I do not have any show function in My TaskController – Lilan Aug 01 '17 at 16:44
  • Which Controller function returns the `show.blade.php`? That's where you want to add the tasks in. – aynber Aug 01 '17 at 16:47
  • I called both ProjectController and TaskController functions show.blade.php in this case I need call TaskController is it ok? – Lilan Aug 01 '17 at 16:52

2 Answers2

0

If you want to show data on task.index then use this code.

 public function index()
 {
  $tasks = Task::all();
  //this line will pass $task data to this view     
  return view('tasks.index',compact('tasks'));
  }
0

If you are including the index blade inside the show blade, then you need to change your controller and pass data to the final blade (show instead of index) like this

return view('tasks.show')->with('tasks', $tasks);
Makamu Evans
  • 491
  • 1
  • 10
  • 25