Laravel试图获取非对象的属性错误

时间:2016-06-07 06:41:03

标签: php laravel-5

我有两张像这样的表

empPf

id
emp_id
emp_name
uan
kyc

empyc

id
emp_id
doc_type
doc_number

现在我有两个模特

empPf

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class empPf extends Model
{
    protected $table = 'empPf';
    protected $primaryKey = 'emp_id';

     public function kyc(){

            return $this->hasMany('App\empKyc','emp_id');

        }
}

empKyc

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class empKyc extends Model
{
    protected $table = 'empKyc';
    //protected $primaryKey = 'emp_id';

     public function pf(){

            return $this->belongsTo('App\empPf','emp_id');

        }
}

我正在尝试在我的控制器中执行以下代码

public function getPf(){
        Session::put('activemenu', 'emp_ss');

        $pfs = empPf::with(array('kyc'))->get();

        foreach ($pfs as  $value) {
              dd($value->kyc[0]->emp_id);
        }

    }

我收到错误

  

尝试获取非对象的属性

任何帮助将不胜感激

STack trace

Stack trace:
#0 {main}  
[2016-06-07 06:52:36] local.ERROR: exception 'ErrorException' with message 'Trying to get property of non-object' in D:\iclock-production\app\Http\Controllers\HrController.php:314
Stack trace:
#0 D:\iclock-production\app\Http\Controllers\HrController.php(314): Illuminate\Foundation\Bootstrap\HandleExceptions->handleError(8, 'Trying to get p...', 'D:\\iclock-produ...', 314, Array)
#1 [internal function]: App\Http\Controllers\HrController->getPf()
#2 D:\iclock-production\vendor\laravel\framework\src\Illuminate\Routing\Controller.php(256): call_user_func_array(Array, Array)
#3 D:\iclock-production\vendor\laravel\framework\src\Illuminate\Routing\ControllerDispatcher.php(164): Illuminate\Routing\Controller->callAction('getPf', Array)
#4 D:\iclock-production\vendor\laravel\framework\src\Illuminate\Routing\ControllerDispatcher.php(112): Illuminate\Routing\ControllerDispatcher->call(Object(App\Http\Controllers\HrController), Object(Illuminate\Routing\Route), 'getPf')
#5 [internal function]: Illuminate\Routing\ControllerDispatcher->Illuminate\Routing\{closure}(Object(Illuminate\Http\Request))
#6 D:\iclock-production\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php(139): call_user_func(Object(Closure), Object(Illuminate\Http\Request))
#7 D:\iclock-production\app\Http\Middleware\Role.php(38): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
#8 [internal function]: App\Http\Middleware\Role->handle(Object(Illuminate\Http\Request), Object(Closure), 'hr')
#9 D:\iclock-production\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php(124): call_user_func_array(Array, Array)
#10 D:\iclock-production\app\Http\Middleware\Authenticate.php(49): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
#11 [internal function]: App\Http\Middleware\Authenticate->handle(Object(Illuminate\Http\Request), Object(Closure))
#12 D:\iclock-production\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php(124): call_user_func_array(Array, Array)
#13 [internal function]: Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
#14 D:\iclock-production\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php(103): call_user_func(Object(Closure), Object(Illuminate\Http\Request))
#15 D:\iclock-production\vendor\laravel\framework\src\Illuminate\Routing\ControllerDispatcher.php(114): Illuminate\Pipeline\Pipeline->then(Object(Closure))
#16 D:\iclock-production\vendor\laravel\framework\src\Illuminate\Routing\ControllerDispatcher.php(69): Illuminate\Routing\ControllerDispatcher->callWithinStack(Object(App\Http\Controllers\HrController), Object(Illuminate\Routing\Route), Object(Illuminate\Http\Request), 'getPf')
#17 D:\iclock-production\vendor\laravel\framework\src\Illuminate\Routing\Route.php(203): Illuminate\Routing\ControllerDispatcher->dispatch(Object(Illuminate\Routing\Route), Object(Illuminate\Http\Request), 'App\\Http\\Contro...', 'getPf')
#18 D:\iclock-production\vendor\laravel\framework\src\Illuminate\Routing\Route.php(134): Illuminate\Routing\Route->runWithCustomDispatcher(Object(Illuminate\Http\Request))
#19 D:\iclock-production\vendor\laravel\framework\src\Illuminate\Routing\Router.php(708): Illuminate\Routing\Route->run(Object(Illuminate\Http\Request))
#20 [internal function]: Illuminate\Routing\Router->Illuminate\Routing\{closure}(Object(Illuminate\Http\Request))
#21 D:\iclock-production\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php(139): call_user_func(Object(Closure), Object(Illuminate\Http\Request))
#22 [internal function]: Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
#23 D:\iclock-production\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php(103): call_user_func(Object(Closure), Object(Illuminate\Http\Request))
#24 D:\iclock-production\vendor\laravel\framework\src\Illuminate\Routing\Router.php(710): Illuminate\Pipeline\Pipeline->then(Object(Closure))
#25 D:\iclock-production\vendor\laravel\framework\src\Illuminate\Routing\Router.php(675): Illuminate\Routing\Router->runRouteWithinStack(Object(Illuminate\Routing\Route), Object(Illuminate\Http\Request))
#26 D:\iclock-production\vendor\laravel\framework\src\Illuminate\Routing\Router.php(635): Illuminate\Routing\Router->dispatchToRoute(Object(Illuminate\Http\Request))
#27 D:\iclock-production\vendor\laravel\framework\src\Illuminate\Foundation\Http\Kernel.php(236): Illuminate\Routing\Router->dispatch(Object(Illuminate\Http\Request))
#28 [internal function]: Illuminate\Foundation\Http\Kernel->Illuminate\Foundation\Http\{closure}(Object(Illuminate\Http\Request))
#29 D:\iclock-production\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php(139): call_user_func(Object(Closure), Object(Illuminate\Http\Request))
#30 D:\iclock-production\vendor\lucadegasperi\oauth2-server-laravel\src\Middleware\OAuthExceptionHandlerMiddleware.php(36): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
#31 [internal function]: LucaDegasperi\OAuth2Server\Middleware\OAuthExceptionHandlerMiddleware->handle(Object(Illuminate\Http\Request), Object(Closure))
#32 D:\iclock-production\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php(124): call_user_func_array(Array, Array)
#33 D:\iclock-production\vendor\laravel\framework\src\Illuminate\View\Middleware\ShareErrorsFromSession.php(49): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
#34 [internal function]: Illuminate\View\Middleware\ShareErrorsFromSession->handle(Object(Illuminate\Http\Request), Object(Closure))
#35 D:\iclock-production\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php(124): call_user_func_array(Array, Array)
#36 D:\iclock-production\vendor\laravel\framework\src\Illuminate\Session\Middleware\StartSession.php(62): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
#37 [internal function]: Illuminate\Session\Middleware\StartSession->handle(Object(Illuminate\Http\Request), Object(Closure))
#38 D:\iclock-production\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php(124): call_user_func_array(Array, Array)
#39 D:\iclock-production\vendor\laravel\framework\src\Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse.php(37): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
#40 [internal function]: Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse->handle(Object(Illuminate\Http\Request), Object(Closure))
#41 D:\iclock-production\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php(124): call_user_func_array(Array, Array)
#42 D:\iclock-production\vendor\laravel\framework\src\Illuminate\Cookie\Middleware\EncryptCookies.php(59): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
#43 [internal function]: Illuminate\Cookie\Middleware\EncryptCookies->handle(Object(Illuminate\Http\Request), Object(Closure))
#44 D:\iclock-production\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php(124): call_user_func_array(Array, Array)
#45 D:\iclock-production\vendor\laravel\framework\src\Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode.php(44): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
#46 [internal function]: Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode->handle(Object(Illuminate\Http\Request), Object(Closure))
#47 D:\iclock-production\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php(124): call_user_func_array(Array, Array)
#48 [internal function]: Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
#49 D:\iclock-production\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php(103): call_user_func(Object(Closure), Object(Illuminate\Http\Request))
#50 D:\iclock-production\vendor\laravel\framework\src\Illuminate\Foundation\Http\Kernel.php(122): Illuminate\Pipeline\Pipeline->then(Object(Closure))
#51 D:\iclock-production\vendor\laravel\framework\src\Illuminate\Foundation\Http\Kernel.php(87): Illuminate\Foundation\Http\Kernel->sendRequestThroughRouter(Object(Illuminate\Http\Request))
#52 D:\iclock-production\public\index.php(54): Illuminate\Foundation\Http\Kernel->handle(Object(Illuminate\Http\Request))
#53 D:\iclock-production\server.php(21): require_once('D:\\iclock-produ...')
#54 {main}  

由于

1 个答案:

答案 0 :(得分:0)

您必须首先检查 pfs 模型是否有任何相关的 kyc

foreach ($pfs as  $value) {
    if ( $value->kyc->count() ) {
        dd( $value->kyc[0]->emp_id );
    }
}