wiwa1978
wiwa1978

Reputation: 2707

Laravel: prevent user from accessing past and future events

I have a Laravel application with events, some of which happened in the past, some of them are current and some will be organised in future. Table 'events' looks as follows:

+----+-------------+------------+-------------------+---------------------+---------------------------+---------------------+---------------------+
| id | category_id | event_name | event_description | event_startdate     | event_closedate           | created_at          | updated_at          |
+----+-------------+------------+-------------------+---------------------+---------------------------+---------------------+---------------------+
|  1 |           1 | Event 1    | Event 1           | 2016-05-02 00:00:00 | 2016-06-30 00:00:00       | 2016-07-07 15:59:03 | 2016-07-08 12:26:07 |
|  2 |           1 | Event 2    | Event 2           | 2016-06-02 00:00:00 | 2016-07-30 00:00:00       | 2016-07-07 15:59:03 | 2016-07-08 12:26:22 |
|  3 |           2 | Event 3    | Event 3           | 2016-07-02 00:00:00 | 2016-08-19 00:00:00       | 2016-07-07 15:59:03 | 2016-07-08 12:27:04 |
|  4 |           2 | Event 4    | Event 4           | 2016-09-01 00:00:00 | 2016-10-30 00:00:00       | 2016-07-07 15:59:03 | 2016-07-07 15:59:03 |
+----+-------------+------------+-------------------+---------------------+---------------------------+---------------------+---------------------+

and each event contains a number of items for which the table is as follows:

+----+------------+-----------+-----------------------+
| id | event_id   | item_name | item_description      |
+----+------------+-----------+-----------------------+
|  1 |          1 | Item 1    | Item 1 - description  |
|  2 |          1 | Item 2    | Item 2 - description  |
|  3 |          2 | Item 3    | Item 3 - description  |
|  4 |          2 | Item 4    | Item 4 - description  |
|  5 |          3 | Item 5    | Item 5 - description  |

I'm displaying the current events on the homepage. In this example, event 2 and 3 are the current events. Each event has a link for the user to view the items belonging to that event.

http://localhost:5000/event/2/items
http://localhost:5000/event/3/items

I would like to prevent a user to be able to view non-current events (e.g. events from the past and in the future). In this example, it means a 'not found' page (or similar) should be shown if a user tries to go to:

 http://localhost:5000/event/1/items
 http://localhost:5000/event/4/items

How can this be prevented?

Upvotes: 1

Views: 70

Answers (1)

Phargelm
Phargelm

Reputation: 718

You can use findOrFail() method with additional clause.

public function show($eventId)
{
    $now = Carbon::now();
    $event = Event::where('event_startdate', '<=', $now)
        ->where('event_closedate', '>=', $now)
        ->findOrFail($eventId);

    //returning specific view
}

Upvotes: 4

Related Questions