2017-12-17 1 views
0

私は、ピボットテーブルを使用して多くの顧客を持つことができるユーザーに焦点を当てたコントローラを持っています。モーメント(動作しない)で、これは私のコントローラである:複数の値がLaravelの列の値と等しいデータベース内の行を検索します

はライン $customerID = $customer->idに関連

Property [id] does not exist on this collection instance.

、明らかにそれは複数返すことができますので:現時点では

public function index() 
    { 
     $customer = Auth::user()->customers; 
     $customerID = $customer->id; 
      $shipFromShipments = shipment::where('ship_from', '=', $customerID)->get(); 
      $shipFromShipmentsCount = $shipFromShipments->count(); 

      $shipToShipments = shipment::where('ship_to','=', $customerID)->get(); 
      $shipToShipmentsCount = $shipToShipments->count(); 

      $billToShipments = shipment::where('bill_to', '=', $customerID)->get(); 
      $billToShipmentsCount = $billToShipments->count(); 

     $customersCount = $customer->count(); 
     return view('home', compact('customer','shipFromShipmentsCount','shipToShipmentsCount','billToShipmentsCount','customersCount')); 
    } 

、私はこのエラーを取得しますインスタンス。

ユーザが複数の顧客(1,3,4など)を持っている場合、$shipFromShipments行の検索で1,3,4のいずれかが表示されている列「ship_from」が検索され、それらの数字のいずれかが存在するすべての行の数を取得します。

私はこれを複数の値ではなく単一の値で行いました。

--update DD($の顧客)

Collection {#310 ▼ 
    #items: array:1 [▼ 
    0 => customer {#309 ▼ 
     #fillable: array:19 [▶] 
     #connection: "mysql" 
     #table: null 
     #primaryKey: "id" 
     #keyType: "int" 
     +incrementing: true 
     #with: [] 
     #withCount: [] 
     #perPage: 15 
     +exists: true 
     +wasRecentlyCreated: false 
     #attributes: array:34 [▶] 
     #original: array:36 [▶] 
     #changes: [] 
     #casts: [] 
     #dates: [] 
     #dateFormat: null 
     #appends: [] 
     #dispatchesEvents: [] 
     #observables: [] 
     #relations: array:1 [▶] 
     #touches: [] 
     +timestamps: true 
     #hidden: [] 
     #visible: [] 
     #guarded: array:1 [▶] 
     #excludedAttributes: [] 
     #auditEvent: null 
    } 
    ] 
} 
+0

を持っているのforeach機能を使用するようなものかもしれ私が最初に取り組ん生MySQLのクエリを取得してお勧めします。 –

+0

$ customer = Auth :: user() - > customersの後にdd($ customer)を入力して – Sohel0415

+0

@ Sohel0415をチェックしてください - リクエストした結果を含めました – Matthew

答えて

1

あなたは、配列のコレクションから単一IDを取得しようとしているようです。

$customerID = $customer->id; 

上記の行では、1つのコレクションであると予想されますが、コレクションの配列を持つことが予想されます。だからこそ、このコレクションインスタンスのエラーにProperty [id]が存在しないということが起こっています。

カスタマーオブジェクトのインスタンスが1つだけ取得されていることを確認してください。次...

$customer = Auth::user()->customer; 

それともとcustomer_idに

関連する問題