2014-05-21 24 views
22

Tôi có 3 bảng DB và tôi không đặt bất kỳ mối quan hệ trên bất kỳ trong số họ. sau đó tôi đã viết đoạn mã sau:laravel: Phương pháp Mối quan hệ phải trả lại một đối tượng kiểu Illuminate Database hùng biện Quan hệ Relation

public function store($key) 
{ 
    $user_key = md5(Input::get('email') . "-" . strtotime('now')); 

    $user = new User; 

    $user->name = Input::get('name'); 
    $user->email = Input::get('email'); 
    $user->user_key = $user_key; 
    $user->password = Hash::make(Input::get('password')); 
    $user->apipass = md5(Input::get('password')); 
    $user->save(); 

    $newUid = $user->id; 

    //check key for share 
    $invited = DB::table('invites')->where('invite_key', Input::get('invite_key'))->where('status', '0')->count(); 

    if($invited == 1) { 
     $inviter_id = DB::table('invites')->where('invite_key', Input::get('invite_key'))->where('status', '0')->pluck('user_id'); 
     $read_only = DB::table('invites')->where('invite_key', Input::get('invite_key'))->where('status', '0')->pluck('read_only'); 

     $share = new RecordShare; 

     $share->user_id = $inviter_id; 
     $share->shared_with_id = $newUid; 
     $share->read_only = $read_only; 
     $share->save; 

    } 

    return Redirect::to('login'); 
} 

giả sử tạo người dùng mới, sau đó xem ai đã mời anh ta, sau đó tạo hồ sơ chia sẻ với người mời.

nhưng khi tôi thử nghiệm nó, tôi đã nhận lỗi:

LogicException

Relationship method must return an object of type Illuminate\Database\Eloquent\Relations\Relation

mở: /home/oneinfin/public_html/dialysis/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php

*/ 
protected function getRelationshipFromMethod($key, $camelKey) 
{ 
$relations = $this->$camelKey(); 
if (! $relations instanceof Relation) 
{ 
throw new LogicException('Relationship method must return an object of type ' 
. 'Illuminate\Database\Eloquent\Relations\Relation'); 
} 

tôi nghĩ có điều gì đó sai trái với dữ liệu của tôi và do đó đã cố gắng để tạo ra một kỷ lục trống

 $share = new RecordShare; 
     $share->save; 

nhưng điều này cũng không thành công với cùng một lỗi. Chức năng chỉ được vượt qua nếu tôi loại bỏ hoàn toàn phần này.

những gì có thể xảy ra? Tôi đã cố xóa bộ nhớ cache nhưng vẫn không hoạt động.

Trả lời

84

Tôi nghĩ rằng sự thay đổi

$share->save; 

để

$share->save(); 
+2

chỗ trên! Tôi không nhận ra nó đã mất tích. cảm ơn – dan

+10

thông báo lỗi không thực sự hữu ích tho ... – adrian7

+0

câu trả lời đúng! – dmSherazi

Các vấn đề liên quan