2017-02-26 102 views
1

我的代码在我laravel控制器下面几行:错误:laravel未定义功能,即使功能是在控制器中定义

public function getTags() { 
    $tags = DB::table('tags')->get(); 
    /* convert Object to array */ 
    $tagsArray = array(); 
    foreach($tags as $tag) { 
     $tagsArray[$tag->tag] = $tag->tag; 
    } 
    return $tagsArray = json_decode(json_encode($tagsArray) , TRUE); 
} 

public function index() { 
    // $recentBlogPost = DB::table('Admin')->get(); 
    // Auth::logout(); 
    if (!(Auth::check())) { 
     return Redirect::to('login'); 
    }  

    $tagsArray = getTags(); 
    return view('admin.index')->with('tags' , $tagsArray); 
} 

现在我得到以下行我的代码中的错误:

$tagsArray = getTags(); 

我得到以下错误:

Call to undefined function App\Http\Controllers\getTags() 

现在我有在同一个控制器wher这个函数e这个函数被调用,那么为什么我会得到这个未定义函数的错误?

回答

2

正确的语法是:

$tagsArray = $this->getTags(); 

The pseudo-variable $this is available when a method is called from within an object context. $this is a reference to the calling object (usually the object to which the method belongs

http://php.net/manual/en/language.oop5.basic.php

1

试图

$tagsArray = $this->getTags();