Author - StudySection Post Views - 651 views
laravel

Useful Laravel Helpers

  1. data_get: The data_get() helper in Laravel is used to get a value from an array or object with dot notation. This functions similarly to array_get() as well. You can pass a third parameter which is optional and is used to set a default value if the key is not found in an array or object.
    $array = [fruits => [apple => ['count' => 15]]];
    $count = data_get($array, 'fruits.apple.count'); // 15
    $object->fruits>apple->count = 40;
    $count = data_get($object, 'fruits.apple.count'); // 40
  2. str_plural: The str_plural() helper converts a string to its plural form. It only supports English language. You can set an optional second parameter to choose the plural or singular form.
    str_plural('apple'); // apples
    str_plural('bat'); // bats
    str_plural('apple', 2); // apples
    str_plural('bat', 1); // bats
    str_plural('child'); // children
    str_plural('person'); // people
    str_plural('fish'); // fish
    str_plural('deer', 2); // deer
  3. route: The route() helper generates a URL for the named route. You can also pass route parameters as an option second argument. If additional parameters are not named Laravel will try to find best match for the attributes on the route then will add any remaining parameters to the end of the URL
    Route::get('cars', 'CarsController@index')->name('cars');
    route('cars'); // http://domain.com/cars
    route('cars', ['color' => 'red']); // http://domain.com/cars?color=red
    Route::get('cars/{id}', 'CarsController@show')->name('cars.show');
    route('cars.show', 1); // http://domain.com/cars/1
    route('cars.show', ['id' => 1]); // http://domain.com/cars/1
  4. abort_if: The abort_if() helper throws an exception when a given condition is true. First parameter is condition, second parameter is for http code, third is an option parameter to set response text and the fourth optional parameter is to set headers.
    abort_if(! Auth::user()->isSuperAdmin(), 403);
    abort_if(! Auth::user()->isSuperAdmin(), 403, 'You are not an authorized);
  5. optional: The optional() helper to access the properties of an object without getting an exception “PHP Error: Trying to get property of non-object”. If the property of an object is null or is undefined, it will return null.
    // User exists, without post
    $user = User::find(1);
    $postId = $user->post->id; // PHP Error: Trying to get property of non-object
    // Fix with optional()
    $postId = optional($user->post)->id; // null

Microsoft Windows 10 is a widely used operating system in computers all over the world. If you have skills in Microsoft Windows 10 then you can get a Windows 10 Certification Exam from StudySection which can help you in getting hired. A beginner level certification exam for newbies and an advanced level certification exam for experts is available on StudySection.

Leave a Reply

Your email address will not be published. Required fields are marked *

fiteesports.com rivierarw.com cratosroyalbet betwoon grandpashabet grandpashabet giriş deneme bonusu veren siteler casino siteleri