How to call helper function in laravel 5.5 - laravel

I am using laravel 5.5. I have created a helper.php in app\Http. I am calling this helper in my blade file by using
{!! Helper::functionName() !!}
this is working fine. but i want to hold this helper result in a variable like
{!! $Result=Helper::functionName() !!}
But currently this is printing this result. How to solve this. please help.
So that i can make any if condition on this $Result.
In my helpers.php
namespace App\Http\Helpers;
class Helper
{
public static function functionName()
{
return "mydata";
}
}

There is no point to use helper like this. You should run the helper in controller and pass calculated data into view. In most cases you shouldn't set any variables in views or make any calculations - those should be passed from controller to view and view should only use them.

In this case, you can use "<?php ?>".
So result:
<?php $Result=Helper::functionName(); ?>

may be this is not possible because in laravel "{{}}" this means echo "" so by default it will print the value. return the value from helper function and use in your blade

Related

What it is the solution for this problem Laravel Action

Action App\Http\Controllers\AdminController#dbTanger not defined.
And this is My AdminController
public function dbTanger() {
$data = Db::table('ressos')
->where('secteur','Services')
->get();
return view('backend.layouts.admin.typeFilterTanger',compact('data','pagi'));
}
And this is the view
<div class="Filter">
<p>Filter Using Ville</p>
Tanger-Asilah
</div>
so if anyone can help me please
and thank you all
Since Laravel 9, the default Controller namespace has not been included in the RouteServiceProvider so you need to be explicit about where to locate controllers (or add the default namespace to your RouteServiceProvider).
What you can do is the following:
{{ action('\App\Http\Controllers\AdminController#dbTanger') }}
However, I would recommend using the route helper in conjunction with named routes as this is easier to manage should files change or move location in the future.
Route::get('/admin/dbTanger', [AdminController::class, 'dbTanger'])->name('admin.dbTanger');
Then use it as follows:
{{ route('admin.dbTanger') }}
The outcome is the same, just easier to manage and maintain long-term.
That error would mean you didn't define a route to this action so there is nothing in the route collection to be found for that action.
Define a route for this action and you would be able to use that action helper to create a URL to a route that uses this action.

Laravel Blade Template - Put php built-function or maybe laravel helper inside yield

do you ever use PHP built-in function inside blade yield ?
For example can we do something like this :
// master layouts
#yield(ucwords('title'))
// view
#section('title', $title)
Note: $title is from controller
I've already try the first example, but it doesn't work. It doesn't output the $title on my view. Right now I am using this in all of my views
// master layouts
#yield('title')
// view 1
#section('title', ucwords($title))
// view 2
#section('title', ucwords($title))
// view 3
#section('title', ucwords($title))
But I think on second example, I'm not DRY my code because I always repeating the ucwords() on each my view. Can we using it on master layout right on yield declaration?
Thank you guys, have a nice work!
You can make your own blade directive for example if you want to make a #ucfirst() then do something like this in AppServiceProvider .
Blade::directive('ucfirst', function ($expression) {
return ucfirst($expression);
});
Past this into boot()
or on each section() you can extend the main layout #section('title', ucwords($title)) or make the helpers like i mentioned above
as you mentioned above you can use yield()
#yield('title',ucwords(strtolower('Your title')))

how to add common variable for laravel and pass this data to all view all views

I want to create a global variable and pass this into all view, so I can get this variable into all blade template
basically, my need is to pass my general setting controller value into my common blade view like header.blade.php
Thanks in advance
For that you will need to add some code in the App->Providers->AppServiceProvider.php like this:
public function boot()
{
view()->composer('*',function($view){
$settings = Settings::firstOrNew(['id' => '1']);
$view->with('settings', $settings );
});
}
According to Laravel documentation you can use view composer:
https://laravel.com/docs/5.8/views#view-composers
for using this feature in app > AppServiceProvider and in boot method you can use this approach for passing parameter to specific view If you have data that you want to be bound to a view each time that view is rendered. this approach meet your need perfectly. for example you want to pass a parameter named userName to header.
View()->composer('header', function ($view){
$userName= "username"
$view->with(['userName'=>$userName]);
});

Pass html into the second argument of #section

I have a master.blade.php which contains #yield('page_tagline')
I want to use it like so #section('page_tagline', __('pages.home.tagline'))
This will work if the translation does not contain any html, but it does.
So how can i use it like that without blade escaping it ?
One way to get around this would be to use the HtmlString class:
#section('page_tagline', new \Illuminate\Support\HtmlString( __('pages.home.tagline')))
You could then take this one step further and create a macro for the Str class or a global helper function.
Macro Example
In you AppServiceProvider (or any service provider you want) add the following to the boot method:
Str::macro('html', function ($string) {
return new HtmlString($string);
});
Don't forget to add the following use statements to the class:
use Illuminate\Support\HtmlString;
use Illuminate\Support\Str;
Then you #section would look something like:
#section('content', Str::html( __('pages.home.tagline')))
You should use
#section('page_tagline')
{!! __('pages.home.tagline') !!}
#endsection
This way you can put HTML inside the tagline

Get Laravel 5 controller name in view

Our old website CSS was set up so that the body tag had an id of the controller name and a class of the action name, using Zend Framework 1. Now we're switching to Laravel 5. I found a way to get the action name through the Route class, but can't find a method for the controller name. I don't see anything in the Laravel docs like this. Any ideas?
This is how you do with action. You inject the Route class, and then call:
$route->getActionName().
I'm looking for something similar for controllers. I've checked the entire route class and found nothing.
If your layout is a Blade template, you could create a view composer that injects those variables into your layout. In app/Providers/AppServiceProvider.php add something like this:
public function boot()
{
app('view')->composer('layouts.master', function ($view) {
$action = app('request')->route()->getAction();
$controller = class_basename($action['controller']);
list($controller, $action) = explode('#', $controller);
$view->with(compact('controller', 'action'));
});
}
You will then have two variables available in your layout template: $controller and $action.
I use a simple solution. You can test and use it in everywhere, also in your views:
{{ dd(request()->route()->getAction()) }}
I will simply use as bellow
$request->route()->getActionMethod()
To get something like PostController try following ...
preg_match('/([a-z]*)#/i', $request->route()->getActionName(), $matches);
$controllerName = $matches[1];
$matches[1] includes the first group while $matches[0] includes everything matched. So also the # which isn't desired.
use this
strtok(substr(strrchr($request->route()->getActionName(), '\\'), 1), '#')
To add to Martin Bean answer, using Route::view in your routes will cause the list function to throw an Undefined offset error when this code runs;
list($controller, $action) = explode('#', $controller);
Instead use this, which assigns null to $action if not present
list($controller, $action) = array_pad(explode('#', $controller), 2, null);
You can use this to just simply display in the title like "Customer - My Site" (laravel 9)
{{ str_replace('Controller', '', strtok(substr(strrchr(request()->route()->getActionName(), '\\'), 1), '#'))}} - {{ config('app.name') }}
You can add this (tested with Laravel v7+)
<?php
use Illuminate\Support\Facades\Route;
echo Route::getCurrentRoute()->getActionMethod();
?>
or
You can use helper function
<?php echo request()->route()->getActionMethod(); ?>
for example :-
Route::get('test', [\App\Http\Controllers\ExampleController::class, 'exampleTest'])->name('testExample');
Now If I request {app_url}/test then it will return exampleTest

Resources