2016-05-06 41 views
0

我想根据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上工作。

编辑

enter image description here

+0

尝试在'__construct'中记录变量,然后将其分配给t能够。然后你可以看到究竟传递了什么。 – aynber

+0

@aynber你的意思是显示变量? –

+0

是的。 'Log :: info($ disasterArea);'会将变量写入您的日志文件中,这通常存储在app/storage/logs中。 – aynber

回答

0

但是你不能以这种方式传递数据,以雄辩模型,因为原来的抽象模型口才拥有的第一个参数为array!它会试图用这种方式来表达。

编辑

正如你可以看到你的屏幕截图 - 模型与方法newInstance什么意思,它试图把(propably)的空数组你串varible地方解决。

解决方案

最好的办法是将逻辑分成扩展的主要Disaster模型,并从工厂解决他们像一些灾难模型:

class UsaFloodDisaster extends Disaster 
{ 
    protected $table = 'usa_flood'; 
} 

class FranceFloodDisaster extends Disaster 
{ 
    protected $table = 'france_flood'; 

} 

(...) 

class DisasterFactory 
{ 
    public function make($area, $disaster = 'flood') 
    { 
     $className = ucfirst(camel_case($area.'_'.$disaster)); 

     return app($className); 
    } 

} 

那么你的控制器将容貌例如:

class DisasterController extends \BaseController { 

    public function getFloodTweets($floodArea){ 
     $flood = app(DisasterFactory::class)->make($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]); 

    } 
} 
+0

ok我告诉你为什么我这样做。我们正在构建一种博客类应用程序,在其中显示与不同地区/国家/地区的灾难有关的帖子。为此,我需要访问多个表。我不想为每个表格创建新的模型。你可以建议有没有适当的替代方法来处理一个Disaster.php模型中的所有表? –

+0

我会写上面的。 –

+0

好的。谢谢.. –