doudu8291 2016-05-23 21:04
浏览 99

Laravel 5:将已定义的数组返回到View时出现奇怪的“未定义变量”错误

As the title states, I'm getting an odd error in Laravel 5. I'm new to Laravel, and this week I dived into Jobs/Queues. I've gotten an "Undefined Variable: $errors" error in the past, and that one I was able to understand and fix. But now, I can't seem to get past this one. To my knowledge, everything looks fine. The following breakdown will (hopefully) give you an idea of what I'm doing/where the error happens:

class PostFormFields extends Job implements SelfHandling
{
use InteractsWithQueue, SerializesModels;

/**
 * The id (if any) of the Post row
 */

protected $id;


/**
 * List of fields and default value for each field
 */

protected $fieldList = [
    'title' => '',
    'subtitle' => '',
    'page_image' => '',
    'content' => '',
    'meta_description' => '',
    'is_draft' => '8',
    'publish_date' => '',
    'publish_time' => '',
    'layout' => 'blog.layouts.post',
    'tags' => [],
];


/**
 * Create a new job instance.
 *
 * @return void
 */

public function __construct($id = null)
{
    $this->id = $id;
}

 /**
 * Execute the job.
 *
 * @return void
 */
public function handle()
{
    $fields = $this->fieldList;

    if($this->id)
    {
        $fields = $this->fieldsFromModel($this->id, $fields);

    } else {
        $when = Carbon::now()->addHour();
        $fields['publish_date'] = $when->format('M-j-Y');
        $fields['publish_time'] = $when->format('g:i A');
    }

    /**
     * Populate with old values, if they exist
     * @var [type]
     */
    foreach ($fields as $fieldName => $fieldValue)
    {
        $fields[$fieldName] = old($fieldName, $fieldValue);
    }

    $fields = array_merge($fields, ['allTags' => Tag::lists('tag')->all()]);

    return $fields;
}

Above is the code inside the handler function of my Job class, the file it sits in is called PostFormFields.php. It's job, essentially, is just to return an array filled with all the values pertaining to a post, based on the Post Model and what's in the database that pertains to that specific Post ('title','content',etc) if a user's entered them in the past

  public function create()
{

    $data = $this->dispatch(new PostFormFields());
    $data['title'] = 'testing';
    var_dump($data);

    return view('admin.post.create', $data);
}

Above is the code inside my PostController class, in the create() method. As you can tell, I'm using a resource controller for my Post Controller. It dispatches the PostFormFields Job and stores all the returned data in an array $data. However, since the create() method will be used to create a new post, only the keys should be returned, with values set to their default value ''.

This works. As you can see, i run a 'var_dump()' on the variable $data to see what, if anything, is returned. I then pass the $data array to the create View. This is where the error comes up.

Laravel "Undefined Varieble" Error

Above is a picture of the error I get when I try to access the /create route. It's clear that the $data does have the $title variable defined, as well as all the other keys in the array. Why am I getting an "Undefined Variable" array when I clearly have it defined by the time it's sent to the create View?

The line of code is says the error is in is the following:

<input type="text" class="radius" name="title" id="title" value="{{ $title }}">
  • 写回答

1条回答 默认 最新

  • douke6857 2016-05-24 06:35
    关注

    You have to pass that array to view via compact function of laravel. So that you can use it in view as you want. Please check about compact here - https://laracasts.com/discuss/channels/general-discussion/phps-compact-pros-and-cons?page=1

    public function create()
    {
    
        $data = $this->dispatch(new PostFormFields());
        $data['title'] = 'testing';
        var_dump($data);
    
        return view('admin.post.create', compact('data'));
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 BP神经网络控制倒立摆
  • ¥20 要这个数学建模编程的代码 并且能完整允许出来结果 完整的过程和数据的结果
  • ¥15 html5+css和javascript有人可以帮吗?图片要怎么插入代码里面啊
  • ¥30 Unity接入微信SDK 无法开启摄像头
  • ¥20 有偿 写代码 要用特定的软件anaconda 里的jvpyter 用python3写
  • ¥20 cad图纸,chx-3六轴码垛机器人
  • ¥15 移动摄像头专网需要解vlan
  • ¥20 access多表提取相同字段数据并合并
  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算