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?
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.
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...
Related
OrderController.php
if (request('payment_method') == 'online') {
return redirect(route('payments.pay', $order->id));
}
web.php
Route::POST('/pay/{orderId}', 'PublicSslCommerzPaymentController#index')->name('payments.pay');
PublicSslCommerzPaymentController.php
session_start();
class PublicSslCommerzPaymentController extends Controller
{
public function index(Request $request, $ordId)
{
//code...
}
}
Here in index function I need the order id from `OrderController.
But the Error I am getting
Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException
The GET method is not supported for this route. Supported methods: POST.
if you want to redirect to named route you can use this:
return redirect()->route('payments.pay', ['orderId' => $order->id]);
if you want generate redirect to controller action you can try this:
return redirect()->action(
'PublicSslCommerzPaymentController#index', ['ordId' => $order->id]]
);
Just change in your web.php from POST method to GET method
Something like this
Route::GET('/pay/{orderId}', 'PublicSslCommerzPaymentController#index')->name('payments.pay');
I want to make a settings screen for my users, and I want the URL to be easy an memorable: domain.com/user/settings
I want this route to point use the UserController#edit method, however, the edit() method requires ID parameter.
Is there someway I can use user/settings URL without specifying the ID in the URL, but still use the same edit() method in the UserController?
The edit URL could be use without any parameter. In that case you can mention the user in controller.
Route
Route::get('user/settings');
Controller
public function edit()
{
$user = Auth::user();
}
Seems like I managed to solve it?
web.php:
Route::get('user/settings', 'UserController#edit')->name('user.settings');
Route::resource('user', 'UserController');
UserController.php:
public function edit(User $user = null)
{
$user = Auth::user();
}
I want to have a URL like this:
/v1/vacations?country=US&year=2017&month=08
How do I set the route in Laravel 5.3 and where can I put the controller and logic to accept the query string?
your route should look like;
Route::get('v1/vacations', 'VacationsController#index');
then on VacationsController
public function index()
{
dd(request()->query());
$query=request()->query();
//search database using query
//return view with results
}
Query strings can not be defined in your route since the query string is not part of the URI.
To access the query string you should use the request object. $request->query() will return an array of all query parameters. You can also use it as such to return a single query param $request->query('key')
You just would check the Request object for the url parameters like so:
// The route declaration
Route::get('/v1/vacations', 'YourController#method');
// The controller
class YourController extends BaseController {
public function method(Illuminate\Http\Request $request)
{
$country = $request->country;
// do things with them...
}
}
Hope this helps you.
how pass and get params from one route to another
return redirect('/registration')->with('some_params', $input);
and here is registrationController index method:
public function index(){
$some_params = Request::get('some_params'); //no result
}
with method flashes data to the session, you have to retrieve the data using the Session::get method.
public function index(){
$some_params = Session::get('some_params');
}
I defined routes for a controller this way :
/model/{id}/view
/model/something/{id}/edit
I need to get the id parameter in the contructor of this controller. For example :
class ArtController extends Controller {
public function __construct(Request $request){
//dd($this->route('id')); //Doesn't work
//dd($request->segments()[1]); //this works for the first route but not the second
}
}
How can you get the parameter id in the constructor of a Controller in Laravel?
You should be able to do something like this
$id = Route::current()->getParameter('id');
Update:
Starting in laravel 5.4 getParameter was renamed to parameter
$id = Route::current()->parameter('id');
public function __construct(Request $request)
{
$id = $request->route('id');
dump($id);
}
You can call anywhere:
request()->route('id');
No need for injection
not use segments
use segment
public function __construct(Request $request){
dd($request->segment(1));}