How to Call a Controller Function Inside a View in Laravel 5

How to call a controller function inside a view in laravel 5

If you have a function which is being used at multiple places you should define it in helpers file, to do so create one (may be) in app/Http/Helpers folder and name it helpers.php, mention this file in the autoload block of your composer.json in following way :

"autoload": {
"classmap": [
"database"
],
"psr-4": {
"App\\": "app/"
},
"files": [
"app/Http/Helpers/helpers.php"
]
},

run composer dump-autoload, and then you may call this function from anywhere, let it be controller view or model.

or if you don't need to put in the helpers. You can just simply call it from it's controller. Just make it a static function.
Create.

public static function funtion_name($args) {}

Call.

\App\Http\Controllers\ControllerName::function_name($args)

If you don't like the very long code, you can just make it

ControllerName::function_name($args)

but don't forget to call it from the top of the view page.

use \App\Http\Controllers\ControllerName;

How to call a controller from view in laravel 5

So I just tried this on one of my Laravel setups. I'm not sure if it's the best approach but it works.

Declare your function as

public static function my($args)
{
echo $args."I am called on view.";
}

which you already did. then call it from the view as {{App\Http\Controllers\Admin\ReportController::my($args)}}

Call a function from view Laravel 5

You can reference the class by its fully qualified name, including namespaces. If AdsController is in App\Http\Controllers (might be different for you), then:

{{\App\Http\Controllers\AdsController::get_user_img($ad->id)}}

(The leading slash indicates to start at the global namespace.)

Laravel 5.5 call Controller function from blade view with parameter

you can't use use keyword inside method

you can write the full class path , like this

 <span class="text-bold">
@php
echo App\Http\Controllers\ServiceProvider::getLowestPrice($product_cat->id);
@endphp
</span>

What is the proper way to access the controller function inside the blade view - Laravel

Calling controller from blade is bad practice
move your getCtgProducts() method logic to Category Model:

public function getCtgProducts(){
$ctgProducts = DB::table('products')
->where('ctg_id', $this->id)->get();
return $ctgProducts;
}

Blade file:

@foreach ($ctgs as $ctg)
<h1> $ctg->name </h1> // -- It is working. Showing the correct ctgs names
<div class=container>
@foreach ($ctg->getCtgProducts() as $product)
<h1> $product->name </h1>
@endforeach
</div>
@endforeach

Better way:
Since there's one-to-many relationship
in Category Model you should have a relationship method:

public function products() {
return $this->hasMany(Product::class);
}

Blade:

@foreach ($ctgs as $ctg)
<h1> $ctg->name </h1> // -- It
is working. Showing the correct ctgs
names
<div class=container>
@foreach ($ctg->products as
$product)
<h1> $product->name </h1>
@endforeach
</div>
@endforeach

calling function from controller in view laravel

This isn't a hard and fast rule, but part of using a framework is to somewhat buy into its conventions and use them to your advantages.

Generally speaking, your Controllers are for working with HTTP (GET, POST, PUT, etc.). They're not designed to be indiscriminate ways to call methods from your Views.

I would recommend doing something like this instead:

// app/Utilities.php
<?php

class Utilities
{
public static function getDate()
{
// your code
}
}

then in your view:

{{ Utilities::getDate() }}

or:

// app/helpers.php
<?php

if (! function_exists('myGetDate')) {
function myGetDate()
{
// your code
}
}

then in your view:

{{ myGetDate() }}

and then in composer.json autoload whichever file you create:

"autoload": {
"files": [
"app/Utilities.php"
]
}

or...

"autoload": {
"files": [
"app/helpers.php"
]
}

and then run composer dump-autoload.

Another way to approach this could be using Blade Service Injection (introduced in Laravel 5.1). This technically can be done with your controller:

// In your blade template
@inject('service', 'App\Http\Controllers\HomeController')

{{ $service->getDate() }}

But I'd still recommend not having a method in your controller in charge of returning this data if it's going to be called as a method from a Blade template. Using some type of service class would be more appropriate:

// app/Utilities.php
<?php

namespace App;

class Utilities
{
public function getDate()
{
// your code
}
}

// In your blade template
@inject('service', 'App\Utilities')

{{ $service->getDate() }}

and in this case you wouldn't need to add it to the files autoload array in composer.json.

For what it's worth, not knowing anything else about your project, I would choose one of the first two options, and more likely the helpers.php option.



Related Topics



Leave a reply



Submit