dongyun8891 2016-08-13 09:21
浏览 123
已采纳

从控制器打印简单数据到视图

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' => '123@example.com',
          '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.

  • 写回答

1条回答 默认 最新

  • douao3636 2016-08-13 09:23
    关注

    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
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 TLS1.2协议通信解密
  • ¥40 图书信息管理系统程序编写
  • ¥20 Qcustomplot缩小曲线形状问题
  • ¥15 企业资源规划ERP沙盘模拟
  • ¥15 树莓派控制机械臂传输命令报错,显示摄像头不存在
  • ¥15 前端echarts坐标轴问题
  • ¥15 ad5933的I2C
  • ¥15 请问RTX4060的笔记本电脑可以训练yolov5模型吗?
  • ¥15 数学建模求思路及代码
  • ¥50 silvaco GaN HEMT有栅极场板的击穿电压仿真问题