System System - 1 year ago 107
PHP Question

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:


namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Http\Requests;

class AccountController extends Controller
$userInfo = array(
'accountType' => 'Developer',
'username' => 'Sys',
'email' => '',
'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.

Answer Source

Please read documentation:

You need to pass data to a view like this:

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


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

Then you can use these variables in a blade view:

@foreach ($friends as $friend)
    {{ $friend }}
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download