laravel helpers

 laravel already have so many global helper functions

framework already usng so many heplers frameerok itself

array_merge -> one of the helper

helper categories

arrays and objects

paths

strings

fluent strings

URLs


default functions calling as hellpers


https://laravel.com/docs/8.x/helpers#main-content



Arr::collapse()

The Arr::collapse method collapses an array of arrays into a single array:

use Illuminate\Support\Arr;
 
$array = Arr::collapse([[1, 2, 3], [4, 5, 6], [7, 8, 9]]);
 
// [1, 2, 3, 4, 5, 6, 7, 8, 9]

array first with function

$first = Arr::first($array, function ($value, $key) {
return $value >= 150;
});

use Illuminate\Support\Arr;
 
$array = [100, 200, 300];
 
$first = Arr::first($array, function ($value, $key) {
return $value >= 150;
});
 
// 200




app_path-> defimes app_path
base_path();

config_path
database_patj

public_path('css/app.scsc')
resource_ppaht
storage_path

$filtered = Arr::where($array, function ($value, $key) {
return is_string($value);
});


Arrays & Objects

Paths

Strings

Fluent Strings


str::Ltrim




The ltrim method trims the left side of the string:

use Illuminate\Support\Str;
 
$string = Str::of(' Laravel ')->ltrim();
 
// 'Laravel '
 
$string = Str::of('/Laravel/')->ltrim('/');
 
// 'Laravel/'

markdown

The markdown method converts GitHub flavored Markdown into HTML:

use Illuminate\Support\Str;
 
$html = Str::of('# Laravel')->markdown();
 
// <h1>Laravel</h1>
 
$html = Str::of('# Taylor <b>Otwell</b>')->markdown([
'html_input' => 'strip',
]);
 
// <h1>Taylor Otwell</h1>

multiple string perforamcnec
str::of($str)->lower()->usfirst()->title();

contains(['vamsi','php')-> if avaliable in string it will show true, otherwise it will show false


URLs

action([homecontroller::class,'index']);

action()

The action function generates a URL for the given controller action:

use App\Http\Controllers\HomeController;
 
$url = action([HomeController::class, 'index']);

If the method accepts route parameters, you may pass them as the second argument to the method:

$url = action([UserController::class, 'profile'], ['id' => 1]);

asset()

The asset function generates a URL for an asset using the current scheme of the request (HTTP or HTTPS):

$url = asset('img/photo.jpg');

You can configure the asset URL host by setting the ASSET_URL variable in your .env file. This can be useful if you host your assets on an external service like Amazon S3 or another CDN:

// ASSET_URL=http://example.com/assets
 
$url = asset('img/photo.jpg'); // http://example.com/assets/img/photo.jpg


route()

The route function generates a URL for a given named route:

$url = route('route.name');

If the route accepts parameters, you may pass them as the second argument to the function:

$url = route('route.name', ['id' => 1]);

By default, the route function generates an absolute URL. If you wish to generate a relative URL, you may pass false as the third argument to the function:

$url = route('route.name', ['id' => 1], false);

secure_asset()

The secure_asset function generates a URL for an asset using HTTPS:

$url = secure_asset('img/photo.jpg');

secure_url()

The secure_url function generates a fully qualified HTTPS URL to the given path. Additional URL segments may be passed in the function's second argument:

$url = secure_url('user/profile');
 
$url = secure_url('user/profile', [1]);

url()

The url function generates a fully qualified URL to the given path:

$url = url('user/profile');
 
$url = url('user/profile', [1]);

If no path is provided, an Illuminate\Routing\UrlGenerator instance is returned:

$current = url()->current();
 
$full = url()->full();
 
$previous = url()->previous();

https://laravel.com/docs/8.x/helpers#paths




Miscellaneous

dd,dump,abort,

abort()

The abort function throws an HTTP exception which will be rendered by the exception handler:

abort(403);
env('app_key')
dump(true or false)
dd($filled);

$ifnfo=info('some');-laravel,lgo sotresin that

now->carbon-> now();
print_R(Now());
full object of date and time zone and timezont utc
from bove carbon method now();

bcrypt -> converst string to password
whether gvenve value is blank -> blank();

config('app.name');




localisation

echo --('ssds');
to translate to local language

class baase name
class_base_name('app/http/controllers/usercontroller'):

to return the class name of given class

double encode convert using e('</hhtml>');
lt;/html&gt;

preg_replace_array

The preg_replace_array function replaces a given pattern in the string sequentially using an array:


$string = 'The event will take place between :start and :end';
 
$replaced = preg_replace_array('/:[a-z_]+/', ['8:30', '9:00'], $string);
 
// The event will take place between 8:30 and 9:00


Str::afterLast()

The Str::afterLast method returns everything after the last occurrence of the given value in a string. The entire string will be returned if the value does not exist within the string:

use Illuminate\Support\Str;
 
$slice = Str::afterLast('App\Http\Controllers\Controller', '\\');
 
// 'Controller'
str::camel ->converts to camel case


kebby case


fluent string methods


laravelcustom helper function

app/helpers-> helper php file
<?php

fucntion formatdate($date,$format='')
{

}

?>

have to import formatdate-> s have to imprt
in composer.json
in autoload



public function customHelpers()
{
formatedate($date,$formate):



}
composer dump-autoload


  1. Which function returns the application's root directory path?

    Marked Answer : base_path()

    Correct Answer : base_path()

  2. The _____ function runs PHP's htmlspecialchars function.

    Marked Answer : e()

    Correct Answer : e()

  3. Choose the correct output for Str::after('This is my name', 'This is');

    Marked Answer : " my name"

    Correct Answer : " my name"

  4. Which method adds a single instance of the given value to a string if it does not already end with that value?

    Marked Answer : --

    Correct Answer : Str::finish()

  5. Which function generates an HTML hidden input field containing the value of the CSRF token?

    Marked Answer : csrf_token()

    Correct Answer : csrf_field()

  6. The ____ method invokes the given closure if a given condition is true.

    Marked Answer : contains()

    Correct Answer : when()

No comments:

Post a Comment

Event listening in react

 How we can listen to som eevents some envents fire like click or automatically user enters into input button , that is event on word type i...