2017-02-26 141 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();