Laravel 5.7 ApiResource GET params empty - laravel-5

I use Laravel 5.7 for my JSON API web application.
In my routes/api.php file, I created the following route :
Route::apiResource('my_resource', 'API\Resource')->except(['delete']);
I added the corresponding controller and methods (index, show,...) and everythink work perfectly. My issue is the following : I would like to add optional GET params like this :
http://a.x.y.z/my_resource?param=hello&param2=...
And for instance being able to retrieve 'hello' in my index() method. However, when I print the value of $request->input('param'), it's empty. I just don't get anything.
Yet, if I create a route like this, with an optional parameter:
Route::get('/my_resource/{param?}', 'API\Resource');
I'm able to get the parameter value in my controller method.
Here is my index method :
class Resource extends Controller {
public function index(Request $request)
{
print($request->input('param'));
// ...
}
// ...
}
Am I missing something ? I'm still new in Laravel maybe I missed something in the documentation.
Thanking you in advance,

You can use:
$request->route("param");

Related

How to catch get parameter and cast it to int in Laravel?

I have the following controller:
class CustomerController extends Controller
{
public function index(int $customerId = 0) : Renderable
{
dd($customerId);
...
And I'm requesting it by the following url http://localhost/customers?customerId=123. I would like to catch customerId as int. But It always gives 0 (default value the for method's signature). Ok, I can do something like:
$customerId = $request->input('customerId');
But the approach with getting parameter from the method's signature is more attractive for me.
I have seen this answer. It looks like very nice! But it does not work for my case. Why? Where is the mistake? Thank you for attention!
Update
Here I will show my routes definitions:
Auth::routes(['register' => false, 'reset' => false]);
Route::middleware('auth')->group(function () {
Route::resource('customers', 'CustomerController');
});
If you want to use the Resource Route you have to pass the customerID parameter like below. And also you have to use one of these methods: show, edit,update or destroy in your controller.
Route::resource('customers', 'CustomerController')->parameters([
'customers' => 'customerId'
]);
For more details: https://laravel.com/docs/5.7/controllers#resource-controllers
AFAIK Laravel doesn't inject parameters this way.
If such URI is really needed - you should manually inspect request input for your variables.
If you want to use controller as written, you should use route parameters (e.g. /customers/{id}) instead of parameters after ?.
By default resource controllers use index() for displaying all entities and show() for displaying only one entity.
If you use Eloquent, you can also inject Customer model, e.g.
public function index(Customer $customer) : Renderable
Customer will be automatically found by id and injected in controller method.

Creating a route to custom function in controller in Laravel

I still can't understand why I can't point my blade to the custom function I made in my controller. I create a route like this,
Route::get('/orders/storeInitialItems', 'OrdersController#storeInitialItems')->name('orders.storeInitialItems');
and in my controller I have this,
public function storeInitialItems()
{
return view('orders.storeInitialItems');
}
but when I run the page, storeInitialItems.blade.php, the error seems calling the show() function of my controller.
Why is that happening?
update
Complete routes for ORDERS
Route::get('/orders','OrdersController#index')->name('orders.index');
Route::get('/orders/create', 'OrdersController#create')->name('orders.create');
Route::post('/orders', 'OrdersController#store')->name('orders.store');
Route::get('/orders/{order}/edit', 'OrdersController#edit')->name('orders.edit');
Route::post('/orders/{order}', 'OrdersController#update')->name('orders.update');
Route::delete('/orders/{order}', 'OrdersController#destroy')->name('orders.delete');
Route::resource('orders', 'OrdersController');
Route::put('orders/{order}/pub', 'OrdersController#publish')->name('orders.publish');
Route::put('orders/{order}/cancel', 'OrdersController#cancel')->name('orders.cancel');
Route::put('orders/{order}/delivered', 'OrdersController#delivered')->name('orders.delivered');
Route::get('/orders/storeInitialItems', 'OrdersController#storeInitialItems')->name('orders.storeInitialItems');
Route::get('/orders/{order}/delivery', 'OrdersController#viewdeliveryItems')->name('orders.delivery');
Route::get('/orders/acceptDelivery', 'OrdersController#acceptDelivery')->name('orders.acceptDelivery');
Add your orders.storeInitialItems route
Route::get('/orders/storeInitialItems', 'OrdersController#storeInitialItems')->name('orders.storeInitialItems');
before,
Route::resource('orders', 'OrdersController');
or add some extra path with your storeInitialItems
Route::get('/orders/storeInitialItems/add-some-extra-path', 'OrdersController#storeInitialItems')->name('orders.storeInitialItems');

Trying to get intended url in LoginController in Laravel 5.6

I'm trying to capture the intended url in my LoginController so I can execute some logic in a showLoginForm() method I added to the controller so I can send the user to a specific view based on the intended URL.
I've tried the following and I cannot get it to work:
public function showLoginForm()
{
$intededUrl Session::put('url.intended', URL::full());
// my base application url is http://www.websites.com:8080
if (starts_with($intededUrl, url('/admin'))) // i want all routes that begin with http://www.websites.com:8080/admin to go here
return view('auth.login');
return view('themes.'.env('APP_THEME', 'mango').'.auth.login'); // but it keeps taking me here
}
I'm using Laravels starts_with() method to try and match the start of the url string.
I just figured it out. I needed Session::get('url.intended');

Issue using View()->Composer() in AppServiceProvider

I am trying to pass data to a view using the boot method in the AppServiceProvider, and have followed the steps outlined in one of the Laracast fundamentals videos. However, I am getting an error in my controller when trying to use this variable.
In my AppServiceProvider:
public function boot()
{
view()->composer('home', function($view){
$view->with('current', Post::orderBy('created_at', 'desc')->first());
});
}
So this should pass the data to my home view as far as I understand, so that I don't need to keep referencing it in all the methods I have in the controller for that view. I then have a homeController with the following method:
public function index()
{
//Get user
$user = Auth::user();
//OMDB API setup - Get Movie name, remove spaces, add into API request
$movie_one = str_replace(" ", "+", $current->movie_one);
$movie_two = str_replace(" ", "+", $current->movie_two);
return view('home', compact(array('user')));
}
Previously I was adding a variable called current in this method to get an entry from my Post table, but wanted to add it to the AppServiceProvider as I will have to re-declare it in other methods as well. The issue I have is that I try to use the $current object again in this method, but it isn't available to it? I get the following error:
ErrorException in homeController.php line 30:
Undefined variable: current
What can I do here? Can I pass that data from to a controller as well as the home view? Or is that not possible?
the code you written inside the boot method, will only be accessible inside home view, if you want to access the Post object inside controller too, you have to follow this, through this you can access your Post object in controller, as will as view.
app()->singleton('current',function($app){
return Post::orderBy('created_at', 'desc')->first();
});
So in your code, you can access the Post object like this,
$post_obj = App::make('current');
$post_obj->movie_two;
or directly
App::make('current')->movie_two;

Laravel 4: get url parameter from controller

I'm new to laravel 4 and I'm currently having the following problem:
In my routes.php I have the line:
Route::get('maps/{map_type}', 'MapsController#loadMaps');
And in my MapsController I'd like to get the {map_type} to use it.
So my question: How can I retrieve map_type in my Controller?
Your first argument in your loadMaps method is your map type.
public function loadMaps($map_type) {
return $map_type;
}
Take a look at the first two code snippets on http://laravel.com/docs/controllers.
Controllers receive parameters as parameters in the called function:
class MapsController extends BaseController {
public function loadMaps($map_type) {}
}
Check http://laravel.com/docs/controllers#basic-controllers for more info

Resources