php laravel:关系方法必须返回一个 Illuminate\Database\Eloquent\Relations\Relation 类型的对象

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23773467/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 16:55:39  来源:igfitidea点击:

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

phplaravel

提问by dan

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

我有 3 个数据库表,我没有对它们中的任何一个建立任何关系。然后我写了以下代码:

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

逻辑异常

关系方法必须返回一个 Illuminate\Database\Eloquent\Relations\Relation 类型的对象

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

打开:/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.

有什么问题?我试图清除缓存,但仍然不起作用。

回答by Laurence

I think change

我想改变

$share->save;

to

$share->save();