Laravel function with optional parameter

laravel function parameter default value
how to make parameter optional in laravel route
php function optional parameter
laravel route function with parameter
laravel multiple optional route parameters
laravel optional parameter route 404
laravel optional argument command
route nullable parameter laravel

In my web file, I have a route that accepts a $id as a value to be passed to a function within my PagesController. However, I want the function to still execute and show the intended form even when the $id is not passed to the function.

web.php file

Route::get('/request/{id}', 'PagesController@makeRequest');

PagesController.php file

public function makeRequest($id)
{
   if(!empty($id)){
      $target = Partner::find($id);
   }
   return view('pages.makeRequest')->with('target', $target);
}

makeRequest.blade.php

<input type="text" class="form-control" value="{{$target->inst_name}}" required disabled>  

I want the page to display details from the database with the $id when provided or have empty spaces when the $id isn't provided.

As the Laravel Documentation states: Use Optional Parameters like this:

Route::get('/request/{id?}', 'PagesController@makeRequest'); //Optional parameter

Controller

public function makeRequest($id = null)
    {
        if(!empty($id)){
            $target = User::find($id);
            return view('pages.makeRequest')->with('target', $target); 
        } else {
            return view('pageslist'); ///set default list..
        }        
    }

Laravel function with optional parameter, Laravel function with optional parameter. Posted 1 year ago by NanaQhuacy. In my web file, I have a route. Route::get('/request/{id}',� Laravel Route Optional Parameters. There are the Following The simple About Route Optional Parameters using La-ravel 5.8 Full Information With Example and source code.. As I will cover this Post with live Working example to develop Handling route with multiple optional parameters, so the laravel route parameters controller for this example is following below.

All your solutions were helpful. The main thing was that when I called just the view without passing $target to the view, the page displayed an error. So this is what I did.

Route::get('/request/{id?}', 'PagesController@makeRequest');

Then in the controller,

public function makeRequest(Request $request, $id=null)
    {
        if ($id != null) {
            $target = Partner::find($id);
            return view('pages.makeRequest')->with('target', $target);
        }
        return view('pages.makeNullRequest');
    }

If you didn't understand what happened, I created a new view which had this instead of what I had posted in the question.

<input type="text" class="form-control" value="" required readonly> 

Sorry I didn't update you guys in time. I think Jignesh Joisar came closest to helping me solve this issue. really appreciate all you guys. You're just awesome

How to pass an optional parameter to a route?, I have the following controller <?php namespace App\Http\Controllers; use How can I correctly pass this optional param to the action method? Teams. Q&A for Work. Stack Overflow for Teams is a private, secure spot for you and your coworkers to find and share information.

For optional parameter pass id with ? in route and give $id = null in your function's parameter like this:

Route::get('/request/{id?}', 'PagesController@makeRequest'); //Optional parameter

makeRequest($id = null) {

// Code here...
...

}


Routing - Laravel, Because in my opinion there is no way to define the parameter as optional. 0 Route::get('{languageCode}/{category}/{title}', function ($languageCode = 'pl',� Only $optis truly optional, $pagemust be supplied if $optis, and $idmust be if $pageis This is a limitation brought about by the way that Laravel maps the named segments to function/method parameters. You could theoretically implement your own logic to make this work, however:

in your routes file (web.php , as mentioned in your question)

Route::get('/request/{id?}', 'PagesController@makeRequest');

and in your controller PagesController.php

public function makeRequest($id = null)
{

}

To read more about this, just read https://laravel.com/docs/5.7/routing#parameters-optional-parameters

How to make first parameter optional, ', function($id, $start=null, $end=null) { if(!$start) { //set start } if(!$end) { //set end } // What is the syntax that goes here to call 'path@index� You may customize the status code using the optional third parameter: Route::redirect('/here', '/there', 301); You may use the Route::permanentRedirect method to return a 301 status code: Route::permanentRedirect('/here', '/there'); View Routes. If your route only needs to return a view, you may use the Route::view method.

You can use optional parameter :

Route::get('/request/{id?}', 'PagesController@makeRequest');

Now, as the parameter is optional, while defining the controller function you need to assign its default value to null in argument declaration.

<?php 

public function makeRequest($id = null)
{
   if($id){

        $target = Partner::findOrFail($id);

        return view('pages.makeRequest')->with(compact('target'));
   }

   // Return different view when id is not present
   // Maybe all targets if you want

   $targets = Partner::select('column1', 'column2')->get();

   return view('pages.all')->with('targets');

}

I am using findOrFail instead of find. Its Laravel's very handy function which automatically throws a ModelNotFound exception and for frontend user throws a simple 404 page.

So if anyone is accessing www.url.com/request/2, its a valid id then it will show a valid page with data. If the accessed url is www.url.com/request/blahblah then it will throw 404. It avoids efforts of handling this manually.

Laravel (5), jpg ), when the controller is invoked, the $height and $width parameters will not be passed to the function. So we must default it to NULL or any� The optional function accepts any argument and allows you to access properties or call methods on that object. If the given object is null , properties and methods will return null instead of causing an error:

Laravel optional route parameters + PHP 7.1 Nullable Types � Issue , How to create PHP function with optional arguments or parameters? PHP code snippets which create functions with optional arguments and parameters. By default, Route::resource will create the route parameters for your resource routes based on the "singularized" version of the resource name. You can easily override this on a per resource basis by using the parameters method. The array passed into the parameters method should be an associative array of resource names and parameter names:

PHP function with optional parameter or argument, Laravel Routing Parameters with Laravel Tutorial, What is Laravel, Laravel <? php; Route::get('/', function(); {; return "This is a home page";; } To make the route parameter optional, you can place '?' operator after the parameter name. The filter validator, which uses PHP's filter_var function under the hood, ships with Laravel and is Laravel's pre-5.8 behavior. The dns and spoof validators require the PHP intl extension.

Laravel Routing Parameters, Default argument values �. A function may define C++-style default values for scalar arguments as follows: Example #3 Use of default parameters in functions. All Laravel applications include Tinker by default. However, you may install it manually if needed using Composer: composer require laravel/tinker Usage. Tinker allows you to interact with your entire Laravel application on the command line, including the Eloquent ORM, jobs, events, and more.

Comments
  • Would laravel.com/docs/5.7/routing#parameters-optional-parameters help? (And don't forget to make your method parameter optional/give it a default)
  • Thanks for your response. I just tried it and I got an error. ``` Too few arguments to function App\Http\Controllers\PagesController::makeRequest(), 0 passed and exactly 1 expected ```
  • did you pass id in your url?
  • I want it to work both for instances, ie. variable passed or not.