System
System

Reputation: 13

Printing simple data from controller to view

Currently I am learning Laravel by doing some basic tasks.

I am wanting to print data from a controller to the view for a profile page.

Current code I have for my Controller:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Http\Requests;

class AccountController extends Controller
{
     $userInfo = array(
          'accountType' => 'Developer',
          'username' => 'Sys',
          'email' => '[email protected]',
          'firstName' => 'Greatest',
          'lastName' => 'Forever',
          'gender' => 'M'
      );

      $posts = array(
        'fromUser' => 'Bob',
        'msg' => 'Hello World!',
        'sentTime' => '2016-07-07 18:01:00'
      );

      $friends = array(
        'Bob', 'Jim', 'Speed'
      );
}

My view currently has nothing in as things I have tried haven't worked.

Upvotes: 1

Views: 107

Answers (1)

Alexey Mezenin
Alexey Mezenin

Reputation: 163768

Please read documentation:

https://laravel.com/docs/5.2/views#passing-data-to-views

You need to pass data to a view like this:

return view('your.view', ['userInfo' => $userInfo, 'posts' => $posts, 'friends' => $friends]);

Or:

return view('your.view', compact('userInfo', 'posts', 'friends'));

Then you can use these variables in a blade view:

@foreach ($friends as $friend)
    {{ $friend }}
@endforeach

Upvotes: 1

Related Questions