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
感謝您的幫助。
你可以粘貼你調用Profile :: find()的代碼嗎?如果將單個主鍵值作爲參數傳遞,則此方法無法返回多個結果 –
同意,沒有足夠的代碼來查找問題。你可以發佈一個'Profile :: find($ id)'返回的例子嗎?或者至少它返回的對象類型是什麼? –
@Ben不錯,我剛剛完成了發生的其他事情。我知道沒有辦法 - 或者不應該沒有辦法 - 該方法只返回ID,但由於某種原因,它正在這樣做。 –