저는 피봇 테이블을 사용하여 많은 고객을 보유 할 수있는 사용자를 중점적으로 다루는 컨트롤러가 있습니다. 순간 (작동하지 않는)에서이 내 컨트롤러 :여러 값이 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
}
]
}
을 CUSTOMER_ID 가지고 의 foreach에게 기능을 사용할 수 있습니다. –
$ customer = Auth :: user() -> customers 다음에 dd ($ customer)를 입력하고 – Sohel0415
을 확인하십시오. @ Sohel0415 - 요청한 결과를 포함 시켰습니다. – Matthew