Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing page URL parameter to controller in Laravel 5.2

In my application I have a page it called index.blade, with route /index. In its URL, it has some get parameter like ?order and ?type.

I want to pass these $_get parameter to my route controller action, query from DB and pass its result data to the index page. What should I do?

like image 917
Abolfazl Avatar asked May 03 '16 20:05

Abolfazl


People also ask

How can pass query string in URL in Laravel?

To pass an array as URL parameter you can make use of php built-in function http_build_query(). The http_build_query() returns you an URL-encoded query string.

How can use URL ID in Laravel?

$request->id; You can get id from POST method like this. public function getDetail(Requests $rq){ $product = Product::where('id',$rq->id)->get(); } .


2 Answers

If you want to access the data sent from get or post request use

public function store(Request $request)
{
    $order = $request->input('order');
    $type = $request->input('type');
    return view('whatever')->with('order', $order)->with('type', $type);
}

you can also use wildcards.

Exemple link

website.dev/user/potato

Route

Route::put('user/{name}', 'UserController@show');

Controller

public function update($name)
{
    User::where('name', $name)->first();
    return view('test')->with('user', $user);
}

Check the Laravel Docs Requests.

like image 95
Achraf Khouadja Avatar answered Oct 12 '22 15:10

Achraf Khouadja


For those who need to pass part of a url as a parameter (tested in laravel 6.x, maybe it works on laravel 5.x):

Route

Route::get('foo/{bar}', 'FooController@getFoo')->where('bar', '(.*)');

Controller:

class FooController extends Controller
{
    public function getFoo($url){
    return $url;
    }
}

Test 1:

localhost/api/foo/path1/path2/file.gif will send to controller and return:

path1/path2/file.gif

Test 2:

localhost/api/foo/path1/path2/path3/file.doc will send to controller and return:

path1/path2/path3/file.doc 

and so on...

like image 35
Fellipe Sanches Avatar answered Oct 12 '22 15:10

Fellipe Sanches