Rhercb
Rhercb

Reputation: 95

Call function inside other function

I cannot figure out how to call function inside another function. Does the snippet below makes sense?

public function createcat()
{
    $kat_id = kategorijas::find('id');
    $apakskat = apakskategorijas::where('kategorijas_id','=',$kat_id)->get();
    $kat = kategorijas::with('apakskategorija')->get(); 
    //$list = compact('kat','apakskat','apakskat_count');
}

public function mainpage()
{
    // $kat_id = kategorijas::find('id');
    // $apakskat = apakskategorijas::where('kategorijas_id','=',$kat_id)->get();
    // $kat = kategorijas::with('apakskategorija')->get(); 
    $list = $this->createcat();     
    return view ('views.home',compact('list'));
}

The thing is, that other views need the createcat function to work properly, otherwise it shows errors that view doesn't have variables that are in the createcat function.

Is this even a good practice? Or the createcat function should be created elsewhere and called from certain place?

UPDATE

So i went through this - Laravel 5 - Where to define functions and call them in views & controllers and created other class for those DB functions. And ended up with this.

Controller that holds main function that i want to use

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Database\Eloquent\Model;
use DB;
use App\Quotation;
use App\kategorijas;
use App\apakskategorijas;

class katcontroller extends Controller
{

    public static function createcat()
    {
        $kat_id = kategorijas::find('id');
        $apakskat = apakskategorijas::where('kategorijas_id','=',$kat_id)->get();
        $kat = kategorijas::with('apakskategorija')->get(); 
        return compact('kat','apakskat','apakskat_count');
    }

}

And now i want to try to call createcat function in my secound class item

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Database\Eloquent\Model;
use DB;
use App\Http\Controllers\katcontroller;



class routes extends katcontroller
{

    // public function createcat()
    // {
    //  $kat_id = kategorijas::find('id');
    //  $apakskat = apakskategorijas::where('kategorijas_id','=',$kat_id)->get();
    //  $kat = kategorijas::with('apakskategorija')->get(); 
    //  $list = compact('kat','apakskat','apakskat_count');

    // }
    public function mainpage()
    {
        // $kat_id = kategorijas::find('id');
        // $apakskat = apakskategorijas::where('kategorijas_id','=',$kat_id)->get();
        // $kat = kategorijas::with('apakskategorija')->get(); 
        $function = $this->createcat();     
        return view ('views.home',compact('function'));
    }

It still shows me the same error, that variables from createcat function are undefined.

Upvotes: 0

Views: 143

Answers (3)

Rhercb
Rhercb

Reputation: 95

So i ignored everything i did, and learner to use view composer so that my sidebar would work every time i include it in a view.

Upvotes: 0

yarwest
yarwest

Reputation: 980

In my experience I have found that putting the method specific actions in that same method is the clearest. But of course this is different if the code is needed in multiple places, in that case you are better of extracting it to a separate (private) method.

There is nothing wrong with either ways of working, just comes down to preference and convenience (readability/clarity too).

Regardless of method composition convention, if you want to get values from a method it should include a return statement that passes the values on to wherever the method was called.

Upvotes: 1

JonTroncoso
JonTroncoso

Reputation: 881

That should work (assuming your in a class), except createcat() isn't returning anything. so when you do $list = $this->createcat(); then $list is null. I believe you want to do this:

public function createcat()
{
    $kat_id = kategorijas::find('id');
    $apakskat = apakskategorijas::where('kategorijas_id','=',$kat_id)->get();
    $kat = kategorijas::with('apakskategorija')->get(); 
    return compact('kat','apakskat','apakskat_count');
}

Upvotes: 1

Related Questions