我想根据URL字符串为我的模型中的$ table属性赋值。在那里我得到“数组到字符串转换”错误。以下是代码级别详细信息。有人可以帮忙吗?谢谢。Laravel 4.2数组到字符串转换错误
我编写了我的模型__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]);
}
}
}
这意味着,如果我触发一个URL像www.example.com/floods/city我的模型应该建表为“city_floods”,这是我们下面的命名约定。 而且,我观察到表名正在正确构建,但抛出此错误。奇怪的是我的代码工作正常时,我硬编码这个表名。即
$ this-> table =“city_floods”正常,但 $ this-> table = $ disasterArea。 '_'。 $灾难不。我不明白有什么区别。有人可以建议我在哪里做错了。
我使用Laravel 4.2框架在UBUNTU 14.04上工作。
编辑
尝试在'__construct'中记录变量,然后将其分配给t能够。然后你可以看到究竟传递了什么。 – aynber
@aynber你的意思是显示变量? –
是的。 'Log :: info($ disasterArea);'会将变量写入您的日志文件中,这通常存储在app/storage/logs中。 – aynber