Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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.

like image 667
dan Avatar asked May 21 '14 03:05

dan


People also ask

What is use of relationship in laravel?

One to one relationship provides the one-to-one relationship between the columns of different tables. For example, every user is associated with a single post or maybe multiple posts, but in this relationship, we will retrieve the single post of a user.

Has One through relationship laravel example?

Laravel introduces a relationship: “HasOneThrough”. This is new in Laravel Development but, other framework uses this like “Rails”. The “HasOneThrough” relationship links models through a single intermediate relation. For example, if each group will have users and users will have their users history.

What is polymorphic relation in laravel?

A one-to-one polymorphic relationship is a situation where one model can belong to more than one type of model but on only one association. A typical example of this is featured images on a post and an avatar for a user. The only thing that changes however is how we get the associated model by using morphOne instead.


1 Answers

I think change

$share->save; 

to

$share->save(); 
like image 163
Laurence Avatar answered Sep 18 '22 06:09

Laurence