【发布时间】:2016-09-01 13:20:24
【问题描述】:
我正在尝试根据 URL 字符串为模型中的 $table 属性分配一个值。我收到“数组到字符串转换”错误。以下是代码级别的详细信息。有人可以帮忙吗?谢谢。
我已经对我的模型 __constructor() 进行了编码,如下所示。
<?php
use Illuminate\Auth\UserTrait;
use Illuminate\Auth\UserInterface;
use Illuminate\Auth\Reminders\RemindableTrait;
use Illuminate\Auth\Reminders\RemindableInterface;
class Disaster extends Eloquent {
use UserTrait,
RemindableTrait;
/**
* The database table used by the model.
*
* @var string
*/
protected $table;
/**
* The attributes excluded from the model's JSON form.
*
* @var array
*/
protected $hidden = array('password', 'remember_token');
public function __construct($disasterArea, $disaster = "flood") {
$this->table = $disasterArea . '_' . $disaster;
}
}
我试图在从我的控制器进行模型实例化时传递所需的值,如下所示。
class DisasterController extends \BaseController {
public function getFloodTweets($floodArea){
$flood = new Disaster($floodArea, "floods");
$floodTweets = $flood->orderBy('created_at','desc')->groupBy('tweets')->paginate(10);
$floodAreaUc = ucfirst($floodArea);
return View::make("disaster.floodTweets",['title'=>"$floodAreaUc Flood",'floodTweets'=>$floodTweets,'floodArea'=>$floodAreaUc]);
}
}
}
这意味着如果我触发像 www.example.com/floods/city 这样的 URL,我的模型应该将表构建为“city_floods”,这是我们遵循的命名约定。 而且,我观察到表名正在正确构建但抛出此错误。奇怪的是,当我硬编码这个表名时,我的代码工作正常。即
$this->table = "city_floods" 工作正常,但 $this->table = $disasterArea 。 '_' 。 $disaster 没有。我不明白有什么区别。有人可以建议我在哪里做错了。
我使用 Laravel 4.2 框架在 UBUNTU 14.04 上工作。
编辑
【问题讨论】:
-
尝试将变量记录在
__construct中,然后再将其分配给表。然后你就可以看到到底传递了什么。 -
@aynber 你的意思是显示变量?
-
是的。
Log::info($disasterArea);会将变量写入您的日志文件,该文件通常存储在 app/storage/logs 中。 -
好的。我记录了 Log::info($disasterArea);和日志::信息($灾难);变量。我可以将它们视为二维数组。 [2016-05-06 13:10:31] production.INFO:阿萨姆 [] [] [2016-05-06 13:10:31] production.INFO:洪水 [] []
-
它们实际上是字符串。
[][]是它放在所有日志行末尾的内容。错误消息是否来自代码的特定行?
标签: php laravel-4 ubuntu-14.04