0

I am new in laravel, How to passed request data into controller ? Just like happen on view ?

Route::get('/kelihatan', function (Request $request) {
    return view( 'pages' , [ 'page' => 'index' , '_request' => $request->all() ] );
});

How to passing request data to controller before passed into view or model ? Just like this ??

Route::get( '/{page}', 'UserController@show' );
3
  • 1
    now in your usercontroller do public function show(Request $request) { $page = $request->page;} Commented Feb 8, 2017 at 6:39
  • @KhanShahrukh what i mean was how to passed data in controller ? replacing "return view ..." with something that called controller and passed request data to that controller Commented Feb 8, 2017 at 7:04
  • @KhanShahrukh It's work but how about $_request ? Where is initialize ? Commented Feb 8, 2017 at 7:13

4 Answers 4

2

Mention the path in route first depending upon the type of request i.e. Get or Post

 Route::get('/invite/{code}', 'MyController@get_my_action');
 Route::post('/invite/{code}', 'MyController@post_my_action');

Then in controller named as MyController create a function like

public function get_my_action($code){
 //your code goes here
}
or
public function post_my_action(Request $request, $code)
{
  //your code goes here
}
Sign up to request clarification or add additional context in comments.

1 Comment

No, the Request is always present and is injected also with get or any other method. Just declare get_my_action(Request $request, $code) and try.
1

With that route:

Route::get('/{page}', 'UserController@show');

In the controller you can do that:

 public function show()
 {
     $page = request()->route('page');

or that:

public function show($page)

or that:

public function show(Request $request, $page)

Comments

0

Try this:

Routes.php

Route::get( '/{page}', 'UserController@show' );

UserController.php

public function show($page) {
   dd($page);
}

2 Comments

what kind of "dd" ?
DD() is a Laravel function used to dump a variable and then exit the execution.
0

Inside your controller define your show function as below:

function show(Request $request)
{
   echo $request['page'];//or whatever data you want to pass

}    
///add follwoing line before you define controller

use Illuminate\Http\Request;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.