0

I'm working on a Laravel 5.2 project and I have users ,flags and countrys. What im trying to achieve is that each user can click on the Flag menu, and it should present a list of flags for the country the user is in.

so User has country_id

Flags have country_id.

at the moment I can show the flags for each user and theyr respective country.

Here is the route.

 Route::get('flags/{Country_id}','FlagController@showFlags');

the view

<a href="flags/{{Auth::user()->country_id}}">

and my controller

public function showFlags($id)
{

    $country = new Country;
    $country = $country->find($id);

    $flags = $country->flags;


    return view('layouts.f.mainf',compact('flags'));

}

The problem is, if i change the county id on the url to anything else, it will show the flags of another country, how can I restric that it can only be acessible if the users country matches the url country id? I've read something about middleware but to be honest Im not sure how to use it.

marybane
  • 75
  • 1
  • 1
  • 7

1 Answers1

1

I don't think that middleware is required here,just simply do this

public function showFlags($id)
{
    if($id != \Auth::user()->country_id)
    {
        throw new ProperException;
    }
    $country = new Country;
    $country = $country->find($id);

    $flags = $country->flags;
    return view('layouts.f.mainf',compact('flags'));
}
Hrach
  • 337
  • 2
  • 10
  • Can you explain what middleware is good for then? and thanks for the answer after seeing the solution I kind of feel stupid – marybane Mar 22 '16 at 17:43
  • Nah,don't feel bad,I'm sure everybody get sometimes into this kind of things,but to be honest I don't clearly understand your question,you want to know where to use middlewares? – Hrach Mar 22 '16 at 17:45
  • Exactly, what can i use middleware for? – marybane Mar 22 '16 at 18:14