Laravel - Route with variable

4

Good morning Sirs. I have a question about the route in Laravel, I have done searches trying to find the result but I did not find what I wanted.

I need that from the id that is redirected (for example: / discipline / 3) it sends the number "3" to the parameter of my method return_disciplina ($ id) so that I can work with it. I tried to do as below but nothing worked out so far. What should I do?

Thank you

Code in the web.php file

Route::get('/disciplina/{id}', "select@retorna_disciplina(id)")->middleware('login');

Code not controller

 public function retorna_disciplina($id)
    {
        $prontuario = session('prontuario');
        $cargo = session('cargo');

        if($cargo == "P")
        {
            $disciplina = DB::table('oferecimento_disciplina')
                ->where('id_professor','=', $prontuario)
                ->where('dsa', '=', $id)
                ->first();

            if(count($disciplina)>0)
            {
                $postagens = DB::table('postagens')
                    ->where('dsa', '=', $id)
                    ->get();
                return view('disciplinas.disciplina')->with([
                        'disciplina' => $disciplina,
                        'postagens' => $postagens
                    ]);
            }
            else{
                Redirect::to('/perfil')->withErros("A disciplina não existe ou você não tem permissão de acesso");
            }
        }
}
    
asked by anonymous 17.11.2016 / 06:09

2 answers

3

Your Route has a small error, it must be this way (I assume that the driver and its file name is correct and is "select"):

Route::get('/disciplina/{id}', "select@retorna_disciplina")->middleware('login');

At the controller you should have:

use Illuminate\Http\Request; // caso não tenha já isto, deve estar no topo ao lado dos outros "use" que deve ter
...
public function retorna_disciplina(Request $request) {
    $id = $request->id;
    ....
}

The variable $request , stores the information about the request, where part of it are the parameters sent via GET , in this case the id.

    
17.11.2016 / 10:47
2

You only need to specify in the expression of your route what parameter will be variable in the url. Then, in your controller, you set the desired parameter to capture the url parameter in your specific method.

See the example below.

Controller:

class SiteController extends Controller
{
      public function getArticle($id)
      {
         dd($id);
      }
}

Route:

Route::get('site/article/{id}', 'SiteController@getArticle');

In the above case, when you call the site/article/5 route, the $id parameter of the getArticle method will have the value 5 .

Note that to indicate which portion of the url will have the variable parameter, you need to use a word wrapped by {} .

If you need to use Request in your method, you can do this:

class SiteController extends Controller
{
      public function getArticle(Request $request, $id)
      {
         dd($id);
      }
}
    
17.11.2016 / 12:55