【问题标题】:Heredoc in Laravel database seedLaravel 数据库种子中的 Heredoc
【发布时间】:2014-11-21 14:53:12
【问题描述】:

我正在尝试在 laravel 中创建一个数据库种子,它使用 heredoc 将 xml 结构设置为变量,然后将该变量用作种子中的值之一:

class CodeTableSeeder extends Seeder {

    public function run()
    {

        DB::table('code')->delete();

        $xml = <<<RawXML
<?xml version="1.0"?>
<fmxmlsnippet type="FMObjectList">
    ...
RawXML;

        Code::create(array('user_id' => 1, 'code' => $xml));
    }
}

当我运行播种机时,我从工匠那里收到一条错误消息:

Seeded: UserTableSeeder

  [ErrorException]                 
  Undefined variable: searchValue  

我知道searchValue 在 xml 代码中:

    <Script includeInMenu="True" runFullAccess="False" id="1" name="Perform a Find (searchValue, searchField, LayoutName)">

似乎数据库播种器正在将 xml 文档作为代码而不是 heredoc 字符串读取。有没有办法阻止播种机读取 xml?有没有更好的方法将 xml 包含在种子中?

将 xml 包含在种子中并不是必须的,但最好在其他开发人员的设置过程中保留一个额外的步骤。

【问题讨论】:

  • 您可以尝试使用 nowdoc,因为它不会像heredoc 那样插入。
  • 完美! nowdoc 成功了!谢谢您的帮助。如果您想将其添加到答案部分,我会给您支票。

标签: php xml laravel-4 heredoc nowdoc


【解决方案1】:

您可以使用nowdoc,而不是使用heredoc。来自文档:

Nowdocs 对于单引号字符串,就像 heredocs 对于双引号字符串一样。 nowdoc 的指定与heredoc 类似,但在nowdoc 内部不进行解析。

另请参阅 deceze 的 this answer,其中给出了两者之间区别的实际示例:

$foo = 'bar';

$here = <<<HERE
    I'm here, $foo!
HERE;

$now = <<<'NOW'
    I'm now, $foo!
NOW;

$here 是“我在这里,酒吧!”,而 $now 是“我现在,$foo!”。

【讨论】:

  • 我还有 4 分钟的时间才能真正开出支票。我会在几秒钟内添加它。
猜你喜欢
  • 2015-02-04
  • 2016-07-13
  • 2017-11-27
  • 2017-01-30
  • 1970-01-01
  • 1970-01-01
  • 2014-01-24
  • 2018-08-18
  • 2020-05-23
相关资源
最近更新 更多