Most Useful Laravel String Functions(helpers)

Today, We want to share with you Most Useful Laravel String Functions(helpers).In this post we will show you Most Popular PHP Laravel String Helpers, hear for 7 little known but super useful PHP Laravel functions we will give you demo and example for implement.In this post, we will learn about List of Most Commonly Used PHP Laravel Functions with an example.

Most Useful Laravel String Functions(helpers)

There are the Following The simple About Most Useful Laravel String Functions(helpers) Full Information With Example and source code.

As I will cover this Post with live Working example to develop The Top 5: Useful PHP Laravel helpers, so the some Top 5: Useful PHP Laravel helpers for this example is following below.

Also Read :  Custom Global Helpers in Laravel 5.8

PHP Laravel String Helpers : data_get()

$array = ['articles' => ['tag' => ['count' => 75]]];

$count = data_get($array, 'articles.tag.count'); // 75
$avgCost = data_get($array, 'articles.tag.avg_cost', 0); // 0

$object->articles->tag->count = 75;

$count = data_get($object, 'articles.tag.count'); // 75
$avgCost = data_get($object, 'articles.tag.avg_cost', 0); // 0

 
$array = ['articles' => ['tag' => ['count' => 75], 'punk' => ['count' => 12]]];
$counts = data_get($array, 'articles.*.count'); // [75, 12]

str_slug() : Laravel String Helpers

$urltitle = 'Top 5: Useful Laravel Helpers';
$slug = str_slug($urltitle);
echo $slug;

// top-5-useful-laravel-helpers

str_random()

str_random(45);

// nIEIODPSBRJjnt7nYJDKoLovPQZBXkoGJFILOIDGYzot

Laravel now()

$time = now();
$time;
date_format($time, "g:i a - jS M Y");

// 2019-03-04 11:16:14
// 11:16 am - 03rd Mar 2019

blank() & filled()

$name = '  ';
blank($name);
filled($name);

// true
// false

$name = 'rhys';
blank($name);
filled($name);

// false
// true

str_plural()

str_plural('boat'); // boats
str_plural('river'); // rivers

str_plural('boat', 2); // boats
str_plural('river', 1); // river

str_plural('tooth'); // teeth
str_plural('person'); // people
str_plural('aircraft'); // aircraft
str_plural('species', 2); // species

Example 2: str_plural()

$things = ['car', 'boat', 'moose', 'matrix', 'goose', 'octopus', 'foot'];
foreach($things as $thing) {
    str_plural($thing);
}

// cars
// boats
// moose
// matrices
// gooses
// octopuses
// feet
// oxen

Laravel route() helper

Route::get('products', '[email protected]')->name('products');
route('products'); // http://websitedomain.com/products
route('products', ['order_by' => 'price']); // http://websitedomain.com/products?order_by=price

Route::get('products/{id}', '[email protected]')->name('products.show');
route('products.show', 1); // http://websitedomain.com/products/1
route('products.show', ['id' => 1]); // http://websitedomain.com/products/1

Route::get('members/{id}/{name}', '[email protected]')->name('members.show');
route('members.show', [5, 'mobile']); // http://websitedomain.com/members/5/mobile
route('members.show', ['id' => 5, 'name' => 'mobile']); // http://websitedomain.com/members/5/mobile
route('members.show', ['id' => 5, 'name' => 'mobile', 'hide' => 'email']); // http://websitedomain.com/members/5/mobile?hide=email

route('products.show', 1, false); // /products/1

Route::domain('{loriverion}.websitedomain.com')->group(function () {
    Route::get('members/{id}/{name}', '[email protected]')->name('members.show');
});

route('members.show', ['loriverion' => 'raleigh', 'id' => 5, 'name' => 'mobile']); // http://raleigh.websitedomain.com/members/5/mobile

//Eloquent model directly
route('products.show', Burger::find(1)); // http://websitedomain.com/products/1

abort_if() Laravel Helper

//abort_if()

abort_if(! Auth::user()->isAdmin(), 403);
abort_if(! Auth::user()->isAdmin(), 403, 'Sorry, Try Again you are not an admin');
abort_if(Auth::user()->isCustomer(), 403);


// In "admin" specific controller
public function index()
{
    if (! Auth::user()->isAdmin()) {
        abort(403, 'Sorry, you are not an admin');
    }
}

// better!
public function index()
{
    abort_if(! Auth::user()->isAdmin(), 403);
}

//optional()
// User 1 exists, with account
$member1 = User::find(1);
$memberapp_Id = $member1->account->id; // 123

// User 2 exists, without account
$member2 = User::find(2);
$memberapp_Id = $member2->account->id; // PHP Error: Trying to get property of non-object

// Fix without optional()
$memberapp_Id = $member2->account ? $member2->account->id : null; // null
$memberapp_Id = $member2->account->id ?? null; // null

// Fix with optional()
$memberapp_Id = optional($member2->account)->id; // null

Angular 6 CRUD Operations Application Tutorials

Read :

Also Read :  GET Facebook friends list email mobile profile url using Facebook Graph API

Summary

You can also read about AngularJS, ASP.NET, VueJs, PHP.

I hope you get an idea about Most Useful Laravel String Functions(helpers).
I would like to have feedback on my Pakainfo.com blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.

Leave a Reply

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