2017-10-20 9 views
1

Lumen 5.5.2を使用し、照会/データベースv5.5.17を使用します。Eloquent relationshipはnullを返しますが、同様のものは正常です。

私は3つのモデルを設定しています。一方はもう一方に属しています。したがって、見積もりにはエリアとデポがあります。

デポとの関係が正常に機能している場合、領域はnullを返します。例えば

$quoteModel = new Quote();   
    $quote = $quoteModel 
      ->with('area') 
      ->with('depot') 
      ->where('id', '=', $id) 
      ->first(); 


    echo 'depot id : ' , $quote->depot->id , "<br>\n"; 
    echo 'area id : ' , $quote->area->id , "<br>\n"; 

デポIDは、オブジェクトではないので、領域がエラーの原因となり、エコーされるであろう。

モデル名を配列​​として渡すか、または領域を要求する(どちらの方法でも)、それを修正することはできません。

Quote.php

namespace App\Models; 

use Illuminate\Database\Eloquent\Model as EloquentModel; 

class Quote extends EloquentModel { 

    protected $table = 'quotes'; 

    public function area() { 
     return $this->belongsTo('\App\Models\Area', 'area_id', 'id'); 
    } 

    public function depot() { 
     return $this->belongsTo('\App\Models\Depot', 'depot_id', 'id'); 
    } 

} 

Area.php

namespace App\Models; 

use Illuminate\Database\Eloquent\Model as EloquentModel; 

class Area extends EloquentModel { 

    protected $table = 'areas'; 

    public $timestamps = false; 
    public $incrementing = false; 

    public function quotes() { 
     return $this->hasMany('\App\Models\Quote', 'area_id', 'id'); 
    } 

} 

Depot.php

namespace App\Models; 

use Illuminate\Database\Eloquent\Model as EloquentModel; 

class Depot extends EloquentModel { 

    protected $table = 'depots'; 

    public $timestamps = false; 
    public $incrementing = false; 

    public function quotes() { 
     return $this->hasMany('\App\Models\Quote', 'depot_id', 'id'); 
    } 

} 

私はArea.phpでパースエラーを作成する場合はスクリプトが証明し、失敗しますそれは含まれています。

リスナーが設定されているため、クエリをログに記録することができ、正常に表示されます。

select * from `quotes` where `id` = 99192 limit 1 
select * from `areas` where `areas`.`id` in (072) 
select * from `depots` where `depots`.`id` in (07) 

領域クエリを手動で実行すると、予想通りの行が返されます。

私は領域関係の名前を変更しようとしましたが、それは役に立ちません。

+1

あなたはどのような雄弁なバージョンを使用していますか?それらを連鎖するのではなく、 - >(['area'、 'depot'])を試してみることができますか? – matiit

+0

バージョンのお知らせをありがとう。彼らは執筆時点で最新のものです(私は5.4であり、アップグレードは助けても傷つきませんでした) - 私は質問を更新しました。悲しいことに配列の構文が助けにならなかった – CodeMonkey

答えて

1

パズルの欠けている部分は、このプロジェクトは、既存のWebアプリケーションの更新の一部としてレガシーデータベースに対してセットアップされていたということでした。

データ型の不一致がありました。別のモデルを問題のない地域にうまくリンクさせることができたら、これを見つけました。 area_idのフィールドは、通常、ゼロで埋められたintですが、何らかの理由でquotesテーブル上にcharがあります。管理者をブラウズするときにデータが正しく見え、コピーして貼り付けたときには機能しましたが、Eloquents内部のどこかで一致しませんでした。

テーブルのデータ型を変更すると、問題が修正されます。

+0

あなたがそれを理解したのを見てうれしい! – matiit

関連する問題