Laravel: prevent user from accessing past and future events -


i have laravel application events, of happened in past, of them current , organised in future. table 'events' looks 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 number of items table 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 current events on homepage. in example, event 2 , 3 current events. each event has link user view items belonging event.

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

i prevent user able view non-current events (e.g. events past , in future). in example, means 'not found' page (or similar) should shown if user tries go to:

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

how can prevented?

you can use findorfail() method additional clause.

public function show($eventid) {     $now = carbon::now();     $event = event::where('event_startdate', '<=', $now)         ->where('event_closedate', '>=', $now)         ->findorfail($eventid);      //returning specific view } 

Comments

Popular posts from this blog

jOOQ update returning clause with Oracle -

java - Warning equals/hashCode on @Data annotation lombok with inheritance -

java - BasicPathUsageException: Cannot join to attribute of basic type -