2016-07-01 75 views
0

我使用10月建立在Laravel上的CMS,我遇到了一些奇怪的問題,我不知道如何解釋。調用一個函數內部的函數[PHP,Laravel,Eloquentl]

代碼示例1:工作正常(組件EstateList)

public function onRun() 
{ 
    $this->listEstates(); 
} 

/** 
* Pulls all the estates from the model 
* 
* @return $estateList 
*/ 
protected function listEstates() 
{ 
    $estateList = RealEstate::all(); 

    return $estateList; 
} 

代碼示例2:不工作(組件EstateDetails)

public function onRun() 
{ 
    $this->show(); 
} 

/** 
* Returns the slug and display individual Estate Object 
* 
* @return $pageDetails 
*/ 
protected function show() 
{ 
    $slug = $this->param('slug'); 
    $pageDetails = RealEstate::find($slug); 

    echo $slug; //returns slug as it should 
    echo $pageDetails; //empty 

    if ($pageDetails) { 
    return $pageDetails; 
    } else { 
    return \Response::make('Page not found', 404); 
    } 
} 

如果我只是把代碼的show()到函數onRun()它工作正常。爲什麼echo echo $pageDetails代碼示例2上返回空?如果它運行在一個單獨的功能show()

謝謝你的幫助。

回答

0

嘗試將其更改爲RealEstate::where('slug', '=', $slug)->firstOrFail();find位在列表中搜索ID表,而不是段塞。

0

你是不是「返回」從show()

響應嘗試改變$this->show();onRun()

-1

返回return $this->show();試着改變你的代碼,這

public function onRun() 
{ 
    return $this->show(); 
} 

/** 
* Returns the slug and display individual Estate Object 
* 
* @return $pageDetails 
*/ 
protected function show() 
{ 
    $slug = $this->param('slug'); 
    $pageDetails = RealEstate::where('slug', '=', $slug)->firstOrFail();; 

    echo $slug; //returns slug as it should 
    echo $pageDetails; //empty 

    if ($pageDetails) { 
    return $pageDetails; 
    } else { 
    return \Response::make('Page not found', 404); 
    } 
} 

希望這將解決你的問題