简体   繁体   中英

Where to add logic to in Laravel 5.6 multi-relation setup

I'm fairly new to laravel and currently building my first project. I'm at a stage now, where I'm not really clear about how to correctly build the db table relationships and where to add the logic for the app.

I have or want the following setup:

users table
id | name | email
-----------------
1  | Cathy| cathy@mail.com
2  | Hans | hans@mail.com

gifts table
id | name     | description
-----------------------------------------
45  | Football | Adidas Telstar 18
46  | Football shoes | Adidas Predator 18.3

purchases table
id | gift_id | confirmed
---------------------------
1  | 45         | null

reservations table
id | gift_id | user_id
---------------------------
1  | 46      | 2

Explanation:

The goal is for a crowd of people to organize what they will give.

There is a gifts table, which via the index-method of it's resource controller prints a table of all gifts to a view. Within this table, I want two buttons for everyone to be clickable: "mark as purchased" and "mark as reserved". One may mark a gift on the "wishlist" as reserved and due to that others can't do that anymore (and know that the other guy will give it as a gift). Or one may mark a gift as purchased, than it requires and admin to confirm that purchase.

I currently created three models and one resource controller. The gift model has 2 "hasOne" relation ships to first the purchase model and second the reservation model. Purchase model and reserveration model are setup with "belongsTo" relation to the gift model.

What I'm currently unable to do is, to distinguish between the different states of the buttons for "mark as reserved" and "mark as purchased" based on whether an gift has an entry in the purchases or reservations table - where do I best place this logic?

Pseudo-Code for my view is currently:

foreach(gifts as gift)
    gift->name
    gift->description
    makePurchase form-submit-button
    makeReservation form-submit-button
endforeach

Hopefully this description is sufficient, otherwise let me know.

Thanks for your help!

You can simply check if the relationships exist:

@if ($gift->reservation && $gift->reservation->user_id == auth()->id())

    //Show purchase button

@elseif(!$gift->reservation && !$gift->purchase)

    //Show reservation button

@endif

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM