2017-12-17 8 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

을 CUSTOMER_ID 가지고 의 foreach에게 기능을 사용할 수 있습니다. –

+0

$ customer = Auth :: user() -> customers 다음에 dd ($ customer)를 입력하고 – Sohel0415

+0

을 확인하십시오. @ Sohel0415 - 요청한 결과를 포함 시켰습니다. – Matthew

답변

1

당신이 모음 배열의에서 단일 ID를 가져하려고하는 것 같다.

$customerID = $customer->id; 

위의 줄에는 단일 컬렉션이지만 컬렉션 배열이 있어야합니다. 그래서이 컬렉션 인스턴스 오류에 Property [id]가 존재하지 않습니다.

고객 개체의 인스턴스가 하나만 있어야합니다. ...

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

를 다음과 같이 뭔가 아니면 내가 먼저 작업 원시 MySQL의 쿼리를 얻기 추천하려는