2016-05-06 8 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; 
 

 
    } 
 

 
}

私は私のコントローラからモデルのインスタンス化は、以下のようにしながら、必要な値を渡すためにしようとしています。私はwww.example.com/floods/cityのようなURLをトリガーする場合私のモデルでは、我々は以下の通りの命名規則は「city_floods」としてテーブルを構築するべきであることを意味

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]); 
 
     
 
    } 
 
     
 
    } 
 
}

。 また、テーブル名が正しくビルドされているが、このエラーが発生しています。そして、奇妙なことに、このテーブル名をハードコードすると、私のコードは正常に動作します。すなわち

$ this-> table = "city_floods"は正常に動作しますが、 $ this-> table = $ disasterAreaです。 '_'。 $災害はありません。私は違いが何であるか分かりません。誰かが私が間違っていることをお勧めしますか?

私はUBUNTU 14.04でLaravel 4.2フレームワークを使用しています。

編集

enter image description here

+0

これをtに割り当てる前に、変数を '__construct'に記録してみてくださいできる。次に、正確に何が渡されているかを見ることができます。 – aynber

+0

@aynberあなたは変数を表示することを意味しますか? –

+0

はい。 'Log :: info($ disasterArea);'はあなたのログファイルに変数を書き出します。ログファイルは通常app/storage/logsに保存されます。 – aynber

答えて

0

元抽象雄弁モデルはarrayとして最初のパラメータを持っているので、まああなたが雄弁モデルにそのようにデータを渡すことはできません!それをこのように置こうとします。

あなたのスクリーンショットで見ることができるようにEDIT

- モデルは、それはあなたの文字列varibleに(propably)空の配列を置くしようとしていることを意味するものnewInstance方法でどこかに解決されます。

ソリューション

最良のオプションは、メイン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); 
    } 

} 

、あなたのコントローラ希望のルックスlike:

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

私はこれをやっている理由を教えてください。私たちは、さまざまな場所/国の災害に関する投稿を表示する、ブログの種類のアプリケーションを構築しています。そのためには、複数のテーブルにアクセスする必要があります。そして、私はテーブルごとに新しいモデルを作成したくないです。 1つのDisaster.phpモデルですべてのテーブルを処理する適切な代替方法はありますか? –

+0

私は上記のコーツを書きます。 –

+0

ありがとう.. –

関連する問題