Laravel,试图获得非对象的财产

时间:2015-12-13 11:55:42

标签: laravel eloquent

在我的ProjectController类中,我有两个方法:

private function checkUserProject($project){
    if(!(Auth::check())) return false;
    elseif($project->user_id != Auth::user()->id) return false;
    else return true;
}

public function getProject($project){
    $project = Project::where('unique_id',$project)->first();
    if(!$this->checkUserProject($project)) return Redirect::route('');
    return $this->process($project);
}

我收到了一个“尝试获取非对象属性”的错误。在checkUserProject()方法中。

我有return $project,它完全返回$project

修改

错误消息:

ErrorException (E_NOTICE)  
Trying to get property of non-object

<?php
class Print3dController extends \BaseController
{
private function checkUserProject($project){
    if(!(Auth::check())) return false;
    elseif($project->user_id != Auth::user()->id) return false;
    else return true;
}

dd($project)的输出是:

object(Project)#669 (20) {
    ["table":protected]=>
    string(8) "projects"
    ["connection":protected]=>
    NULL
    ["primaryKey":protected]=>
    string(2) "id"
    ["perPage":protected]=>
    int(15)
    ["incrementing"]=>
    bool(true)
    ["timestamps"]=>
    bool(true)
    ["attributes":protected]=>
    array(14) {
    ["id"]=>
    int(19)
    ["unique_id"]=>
    int(2665861)
    ["user_id"]=>
    int(1)
    ["status_id"]=>
    int(9)
    ["type_id"]=>
    int(1)
    ["admin_id"]=>
    NULL
    ["price1"]=>
    NULL
    ["price2"]=>
    NULL
    ["description"]=>
    string(0) ""
    ["response_id"]=>
    NULL
    ["file_has_problem"]=>
    int(0)
    ["problem"]=>
    string(0) ""
    ["created_at"]=>
    int(1449402767)
    ["updated_at"]=>
    int(1449991555)
    }
    ["original":protected]=>
    array(14) {
    ["id"]=>
    int(19)
    ["unique_id"]=>
    int(2665861)
    ["user_id"]=>
    int(1)
    ["status_id"]=>
    int(9)
    ["type_id"]=>
    int(1)
    ["admin_id"]=>
    NULL
    ["price1"]=>
    NULL
    ["price2"]=>
    NULL
    ["description"]=>
    string(0) ""
    ["response_id"]=>
    NULL
    ["file_has_problem"]=>
    int(0)
    ["problem"]=>
    string(0) ""
    ["created_at"]=>
    int(1449402767)
    ["updated_at"]=>
    int(1449991555)
    }
    ["relations":protected]=>
    array(0) {
    }
    ["hidden":protected]=>
    array(0) {
    }
    ["visible":protected]=>
    array(0) {
    }
    ["appends":protected]=>
    array(0) {
    }
    ["fillable":protected]=>
    array(0) {
    }
    ["guarded":protected]=>
    array(1) {
      [0]=>
      string(1) "*"
    }
    ["dates":protected]=>
    array(0) {
    }
    ["touches":protected]=>
    array(0) {
    }
    ["observables":protected]=>
    array(0) {
    }
    ["with":protected]=>
    array(0) {
    }
    ["morphClass":protected]=>
    NULL
    ["exists"]=>
     bool(true)
 }

1 个答案:

答案 0 :(得分:1)

我认为$project可能没有user_id变量,因此您必须首先为empty()添加isset()$project->user_id,然后进行验证。