php – laravel: Relationship method must return an object of type Illuminate\Database\Eloquent\Relations\Relation-ThrowExceptions

Exception or error:

I have 3 DB tables and I did not put any relationship on any of them. then I wrote the following code:

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');
}

its supposed create a new user, then see who invited him, and then create a share record with the inviter.

but when I test it, I got the error:

LogicException

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

open: /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');
}

I thought there was something wrong with my data and so tried to create an empty record

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

but this also failed with the same error. The function only gets pass if I remove this part totally.

what could be wrong? I tried to clear the cache, but still don’t work.

How to solve:

I think change

$share->save;

to

$share->save();

Leave a Reply

Your email address will not be published. Required fields are marked *