Mirza Chilman Mirza Chilman - 2 months ago 6
PHP Question

calling function from controller in view laravel

this is a simple thing actually but since im new in laravel it trouble me, i have this function

class HomeController extends Controller {
public $layout = 'layouts.master';
public function __construct()
{
$this->middleware('auth');
}
public function index()
{
return view('landing-page')
->with('title','Landing Page')
->with('users',User::member()->get());

}
<!-- HOW TO CALL getDate Function in my view -->
public function getDate(){
$mytime = Carbon::now()->format('f');
$response = array
(
'content' =>$mytime,
'status' =>'success',
);
return Response::json($response)->view('landing-page');
}
}


how to call it in my laravel view? i search all over the internet but i not quite understand since programming is new for me i already tried something like this in my view using routes
{{url('date')}}
,
{{$mytime}}
, but none working, well i can call a function if there's certain event happen like clicking button or else but if no certain event it's quite confusing me

<p>Month :{{url('date')}}</p>
<p>Month :{{$mytime()}}</P>


above are some ways i tried to call the function


UPDATE WHAT I'VE TRIED BASED on @tptcat answer and work


create
helpers.php
located under files `app\helpers.php

<?php
use Carbon\Carbon;
if (! function_exists('myGetDate')) {
function myGetDate()
{
$mytime = Carbon::now()->format('F');
return $mytime
}
}


composer.json


"autoload": {
"files":[
"App/helpers.php"
],


calling function in my view

{{myGetDate()}}

Answer

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.

Comments