Currently I am developing a small to middle level application in laravel I came across middleware in laravel, My question is Can i use middleware for making changes in my table for eg, In my application(Canteen Management System), When user orders something from the menu and make request for order then before inserting the order into the model table i want to subtract the order amount from his balance. Reason i am thinking of doing this is because balance attribute is a part of user table and order amount is another part of Order table and i am not being able to develop any data relation between them (but I derive many to one relation between them). So i am not thinking of doing only this thing using data relationship , So that's when i come accross middleware. So help me about this, also Can i use two model in one controller function ?
1 に答える
1
ミドルウェアは、リクエストが処理される前または後に実行されます。説明しているビジネス ロジックを実行する場所ではありません。
ニーズにより適したツールは、Eloquent のモデル オブザーバーです。詳細については、http: //laravel.com/docs/5.0/eloquent#model-observersをご覧ください。
あなたの場合、注文後にユーザーの残高を減らす OrderObserver を登録できます。基本的な例:
class OrderObserver {
public function created($order) {
$user = $order->user;
$user->balance = $user->balance - $order->quantity;
$user->save();
}
}
于 2015-11-29T14:04:09.303 に答える