即使它是一个Laravel5.3对象,也试图获得非对象的属性

时间:2017-06-28 05:27:32

标签: php laravel laravel-5.3

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Result extends Model
{
    public function user()
    {
        return $this->belongsTo('App\Student', 'id_user', 'id_user');
    }

    public static function score($s, $c, $level, $semester, $session)
    {
        $ca = self::where([
                ['id_user', '=', $s],
                ['id_registered_course', '=', $c],
                ['id_level', '=', $level],
                ['id_semester', '=', $semester],
                ['id_session', '=', $session]
            ])->first();
        die(var_dump($ca));

        $exam = self::where([
                ['id_user', '=', $s],
                ['id_registered_course', '=', $c],
                ['id_level', '=', $level],
                ['id_semester', '=', $semester],
                ['id_session', '=', $session]
            ])->first()->exam;

        // die(var_dump($result));
        $totalScore = $ca;
        return $totalScore;
    }
}

我有上面的代码。如果我尝试访问$ca->ca,它会说尝试获取非对象的属性。但是,如果我var_dump($ca),它表明它是一个对象。关于什么是错的任何线索?

1 个答案:

答案 0 :(得分:0)

我想帮忙

这是你的结果吗?

object(App\Result)#272 (25) {
["connection":protected]=> string(5) "mysql"
    ["table":protected]=> NULL 
    ["primaryKey":protected]=> string(2) "id" 
    ["keyType":protected]=> string(3) "int" 
    ["incrementing"]=> bool(true) 
    ["with":protected]=> array(0) { } 
    ["withCount":protected]=> array(0) { } 
    ["perPage":protected]=> int(15) 
    ["exists"]=> bool(true) 
    ["wasRecentlyCreated"]=> bool(false) 
    ["attributes":protected]=> array(10) {
        ["id"]=> int(1)
        ["id_user"]=> string(36) "77760bc0-3c89-11e7-8d90-49b73785a136"
        ["id_registered_course"]=> int(5)
        ["ca"]=> int(40)
        ["exam"]=> int(60)
        ["id_session"]=> int(2)
        ["id_semester"]=> int(1)
        ["id_level"]=> int(1)
        ["created_at"]=> string(19) "2017-06-09 20:04:59"
        ["updated_at"]=> string(19) "2017-06-13 16:49:11"
    }
    ["original":protected]=> array(10) {
        ["id"]=> int(1)
        ["id_user"]=> string(36) "77760bc0-3c89-11e7-8d90-49b73785a136"
        ["id_registered_course"]=> int(5)
        ["ca"]=> int(40)
        ["exam"]=> int(60)
        ["id_session"]=> int(2)
        ["id_semester"]=> int(1)
        ["id_level"]=> int(1)
        ["created_at"]=> string(19) "2017-06-09 20:04:59"
        ["updated_at"]=> string(19) "2017-06-13 16:49:11"
    }
    ["casts":protected]=> array(0) { }
    ["dates":protected]=> array(0) { }
    ["dateFormat":protected]=> NULL
    ["appends":protected]=> array(0) { }
    ["events":protected]=> array(0) { }
    ["observables":protected]=> array(0) { }
    ["relations":protected]=> array(0) { }
    ["touches":protected]=> array(0) { }
    ["timestamps"]=> bool(true)
    ["hidden":protected]=> array(0) { }
    ["visible":protected]=> array(0) { }
    ["fillable":protected]=> array(0) { }
    ["guarded":protected]=> array(1) {
        [0]=> string(1) "*" }
    }

由于您使用的是Laravel,我认为您可以使用$ca->__get('ca');$ca->getAttribute('ca');

如果不能,请尝试$ca->attributes['ca'];echo '<pre>'; print_r($ca->toArray());die;