2014-04-03 104 views
0

請在我的控制器中定義新功能時出現錯誤: 在我的控制器中,我有CRUD方法,如創建,更新,顯示($ id),編輯等。我的問題是:我想定義另一個函數來顯示特定的配置文件(imageprofiles),但它總是會生成一個錯誤,而不是使用imageprofiles()方法,它始終使用crud方法show($ id)。我所有的觀點都是正確的。任何人都可以幫我嗎?提前致謝!Laravel控制器

<?php 

class EncodingProfilesController extends \BaseController { 


    public function index() 
     { 
      ......} 
public function imageprofiles() 
    { 
     $Profiles = encodingprofiles::where('type', '=', 'Video')->get(); 
     // show the view and pass the profile to it 
     return View::make('encodingprofiles.sh') 
     ->with('encodingprofiles', $Profiles); 
} 
public function show($id) 
{ 
    // get the profile 
     $Profile = encodingprofiles::find($id)->first(); 

     // show the view and pass the profile to it 
     return View::make('encodingprofiles.show') it tells me that here is the problem Symfony \ Component \ Debug \ Exception \ FatalErrorException 
Call to a member function first() on a non-object 
      ->with('encodingprofiles', $Profile); 
} 

這裏是重定向我到此功能<a href="{{ URL::to('encodingprofiles/imageprofiles') }}">Type</a>

+0

你的'routes.php'看起來像什麼? – zwacky

+0

<?php Route :: controller('users','UsersController'); Route :: resource('encodingprofiles','EncodingProfilesController'); – user3481058

回答

0

你可以使用嵌套資源的鏈接。

Route::resource('encodingprofiles', 'EncodingProfilesController'); Route::resource('encodingprofiles.imageprofiles', 'ImageProfilesController');

你把imageprofiles作爲encodingprofiles內的新的資源,因此需要一個新的新的控制器。

+0

謝謝,但它沒有工作@zwacky :(仍然是同樣的問題,它重新指引我的功能顯示($ id) – user3481058

+0

的頁面,你還需要以不同的ID調用'encodingprofiles/ID/imageprofiles' ,因爲只有屬於某個encodingprofiles的imageprofiles,如果不是這樣,你可能不應該使用資源 – zwacky

+0

以使用命令'artisan routes'來更好地理解資源路由。 – zwacky

0

這個:$ Profile = encodingprofiles :: find($ id)只會得到一個對象。不要使用first()方法。只有在使用$ Profile = encodingprofiles :: where(condition) - > get() - > first();