2015-08-08 38 views
0

我不知道爲什麼或如何解決這個問題,但每當我使用Profile :: find($ id)時,它將返回所有數據而不是我想要的項目。Laravel 4 - 查找方法獲取所有數據

我找不到爲什麼會出現這種情況。

型號

class Profile extends Eloquent 
{ 
    public static $rules = array(
     'first_name'  => array('required', 'alpha', 'min:3'), 
     'last_name'   => array('required', 'alpha', 'min:3'), 
     'date_of_birth'  => array('date'), 
     'telephone'   => array('digits'), 
     'gender'   => array('required', 'alpha', 'in:Male,Female') 
    ); 

    protected $fillable = ['first_name', 'last_name', 'date_of_birth', 'gender']; 

    protected $guarded = ['id']; 

    public function user() 
    { 
     return $this->belongsTo('User'); 
    } 

    public function attributes() 
    { 
     return $this->belongsToMany('Attribute', 'profile_attributes', 'profile_id', 'attribute_id') 
        ->withPivot('attribute_value_id'); 
    } 

    public function values() 
    { 
     return $this->belongsToMany('AttributeValue', 'profile_attributes', 'profile_id', 'attribute_value_id') 
        ->withPivot('attribute_id'); 
    } 
} 

控制器

public function show($id) 
{ 
    $data['profile'] = Profile::find(11)->get()->toArray(); 
    return View::make('profile.show', $data); 
} 

結果

array (size=2) 
    0 => 
    array (size=14) 
     'id' => int 10 
     'user_id' => int 1 
     'first_name' => string 'Richard' (length=7) 
     'last_name' => string 'Skinner' (length=7) 
     'date_of_birth' => string '1980-01-01' (length=10) 
     'bio' => string 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Duis tempor pellentesque bibendum. Vestibulum congue est non imperdiet varius. Morbi a nunc nec nulla euismod sodales. Morbi id maximus sem. Proin tempor molestie feugiat. Ut pharetra felis quis erat sagittis volutpat. Interdum et malesuada fames ac ante ipsum primis in faucibus. Mauris fermentum vitae nisi vel varius. Proin sagittis varius placerat. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae; 

Donec tinci'... (length=2043) 
     'telephone' => null 
     'gender' => string 'male' (length=4) 
     'profile_picture' => string 'IMG_0005.JPG' (length=12) 
     'latitude' => null 
     'longitude' => null 
     'created_at' => string '2015-05-02 16:35:51' (length=19) 
     'updated_at' => string '2015-05-20 18:52:51' (length=19) 
     'deleted_at' => null 
    1 => 
    array (size=14) 
     'id' => int 11 
     'user_id' => int 2 
     'first_name' => string 'Sydney' (length=6) 
     'last_name' => string 'Nouch' (length=5) 
     'date_of_birth' => string '0000-00-00' (length=10) 
     'bio' => string 'I need a bio' (length=12) 
     'telephone' => null 
     'gender' => string 'female' (length=6) 
     'profile_picture' => string 'Sunflower.gif' (length=13) 
     'latitude' => null 
     'longitude' => null 
     'created_at' => string '2015-07-21 18:01:22' (length=19) 
     'updated_at' => string '2015-07-21 18:14:30' (length=19) 
     'deleted_at' => null 

感謝您的幫助。

+1

你可以粘貼你調用Profile :: find()的代碼嗎?如果將單個主鍵值作爲參數傳遞,則此方法無法返回多個結果 –

+0

同意,沒有足夠的代碼來查找問題。你可以發佈一個'Profile :: find($ id)'返回的例子嗎?或者至少它返回的對象類型是什麼? –

+0

@Ben不錯,我剛剛完成了發生的其他事情。我知道沒有辦法 - 或者不應該沒有辦法 - 該方法只返回ID,但由於某種原因,它正在這樣做。 –

回答

0

我想我找到了答案,但不知道爲什麼會發生這種行爲。但在這裏不用...

使用時發現你可以撥打

指定者

方法,將只得到所需要的結果。

這是我不明白,也許有些人可以澄清爲什麼。當調用

得到

方法它從模型中的所有數據,這就是我在做什麼錯。

所以我的代碼最後一塊是如下

$數據[ '輪廓'] =資料::與( '屬性') - >查找($ ID) - >指定者();

希望能幫助別人不浪費一天。