简体   繁体   English

在Laravel中保存模型时出错。 “在非对象上调用成员函数getKey()”

[英]Error when I save model in Laravel. 'Call to a member function getKey() on a non-object'

I try save my object in DB by this code: 我尝试通过以下代码将对象保存在DB中:

$preview = new ProjectProductPreview();
$preview->user_id = $user_id;
$preview->path = $preview_url;
$preview->type = $type;
$preview->project_product_id = $product_id;
$preview->save();  

And I got strange error. 而且我得到了奇怪的错误。 This error occurs when call save() method. 调用save()方法时,会发生此错误。 I have checked my laravel logs, there is this error. 我检查了我的Laravel日志,出现此错误。 I can debug this precisely because in this case, error stack has only two positions 我可以精确地调试它,因为在这种情况下,错误堆栈只有两个位置

[2015-02-17 09:37:30] production.ERROR: exception 'Symfony\Component\Debug\Exception\FatalErrorException' with message 'Call to a member function getKey() on a non-object' in /var/www/erp/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Collection.php:133
    Stack trace:
    #0 [internal function]: Illuminate\Exception\Handler->handleShutdown()
    #1 {main} [] []

My model looks like: 我的模型如下:

class ProjectProductPreview extends BaseModel {

    protected $table = 'project_products_preview';


    public function user() {
        return $this->belongsTo('User');
    }

    public function project_product() {
        return $this->belongsTo('ProjectProduct', 'project_product_id');
    }

    public function projectProduct()
    {
        return $this->belongsTo('ProjectProduct', 'project_product_id');
    }

    public function message() {
        return $this->belongsToMany('Message');
    }

    public function getThumb() {
        $path = $this->path;
        $thumb_path = str_replace('.jpg', '_70.jpg', $path);
        return $thumb_path;
    }

    public function fullPath() {
        return public_path($this->path);
    }

    public function getPath()
    {
        return $this->path;
    }

    public function getUrlAttribute() {
        $product_id = $this->project_product_id;

        return '/product/' . $product_id . '#' . $this->id;
    }

    public function isAccepted()
    {
        if($this->accepted==1){
            return true;
        }

        return false;
    }

    public function accept()
    {
        ProjectProductPreview::where('project_product_id',$this->project_product_id)->update(array('accepted'=>0));
        $this->accepted=1;
        $this->save();

        Event::fire('project_product_preview.accepted', array($this));
    }

    public function acceptRemove()
    {
        $this->accepted=0;
        $this->save();

        Event::fire('project_product_preview.remove_accept', array($this));
    }

}

I am not quite sure about this, but I think this line 我对此不太确定,但我认为这条线

$preview->project_product_id = $product_id;

causes the issue. 导致问题。 You have a relationship to ProjectProduct so you might want to do sth. 您与ProjectProduct有关系,因此您可能想做某事。 like that actually: 像这样:

$preview->projectProduct()->associate(ProjectProduct::find($product_id));

see the laravel docs on inserting models . 有关插入模型的信息,请参阅laravel文档 Please also note that you have two identical realtionships to ProjectProduct which might cause unexpected behaviour as well. 另请注意,您对ProjectProduct有两个相同的所有权,这也可能导致意外行为。 At least one is superflous. 至少一个是多余的。

// one can be removed
//public function project_product() {
//    return $this->belongsTo('ProjectProduct', 'project_product_id');
//}

public function projectProduct()
{
    return $this->belongsTo('ProjectProduct', 'project_product_id');
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在非对象上调用成员函数save() - Call to a member function save() on a non-object Laravel 4错误:在非对象上调用成员函数pass() - Laravel 4 error: Call to a member function passes() on a non-object Laravel 邮件错误调用非对象上的成员函数 subject() - Laravel mail error Call to a member function subject() on a non-object 调用成员 function connection() 上 Laravel 5 - Call to a member function connection() on a non-object error on Laravel 5 Laravel 4将数据库查询从模型传递到控制器 - 对非对象的成员函数进行错误调用 - Laravel 4 passing database query from model to controller - error call to member function on non-object Laravel调用非对象上的成员函数 - Laravel Call to a member function on a non-object 使用mockery测试Laravel控制器时出错:在非对象上调用成员函数fetchMock() - Error when testing Laravel controller with mockery: Call to a member function fetchMock() on a non-object 在非对象Laravel 4文件上传上调用成员函数move() - Call to a member function move() on a non-object Laravel 4 file upload 在非对象上调用成员函数getClientOriginalName()-Laravel 4.2 - Call to a member function getClientOriginalName() on a non-object - Laravel 4.2 Laravel 5:[在非对象上调用成员函数getAction() - Laravel 5 : [Call to a member function getAction() on a non-object
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM