私は、ピボットテーブルを使用して多くの顧客を持つことができるユーザーに焦点を当てたコントローラを持っています。モーメント(動作しない)で、これは私のコントローラである:複数の値が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
}
]
}
を持っているのforeach機能を使用するようなものかもしれ私が最初に取り組ん生MySQLのクエリを取得してお勧めします。 –
$ customer = Auth :: user() - > customersの後にdd($ customer)を入力して – Sohel0415
@ Sohel0415をチェックしてください - リクエストした結果を含めました – Matthew