There are multiple ways to return a view with variables

1// First way ->with()
2return view('index')
3 ->with('projects', $projects)
4 ->with('tasks', $tasks)
5 
6// Second way - as an array
7return view('index', [
8 'projects' => $projects,
9 'tasks' => $tasks
10 ]);
11 
12// Third way - the same as second, but with variable
13$data = [
14 'projects' => $projects,
15 'tasks' => $tasks
16];
17return view('index', $data);
18 
19// Fourth way - the shortest - compact()
20return view('index', compact('projects', 'tasks'));

Like our articles?

Become a Premium Member for $129/year or $29/month
What else you will get:
  • 22 courses (477 lessons, total 38 h 20 min)
  • 2 long-form tutorials (one new every week)
  • access to project repositories
  • access to private Discord