我正在尝试从表中检索一些值,但Unhandled exception: Trying to get property of non-object
在尝试将结果作为 Fluent 返回的对象的属性进行访问时出现错误。
问题:尝试访问对象的属性有效,即使它抛出错误。这对我来说真的很令人费解。可能是什么问题?
PHP 代码
$sets = DB::table('sets')->where('user_id', '=', $user_id)->get();
// Get thumbnail of latest item added to set
foreach($sets as $set) {
$post_set = DB::table('posts_sets')
->where('set_id', '=', $set->id)
->order_by('created_at', 'desc')
->first();
print_r($post_set); // works fine
echo $post_set->id; // prints out value, BUT throws the unhandled exception error!
}
错误
未处理的异常
信息:
试图获取非对象的属性
地点:
/home/test/public_html/application/controllers/api.php 在第 47 行
堆栈跟踪:
#0 /home/test/public_html/laravel/laravel.php(40): Laravel\Error::native(8, '尝试获取 p...', '/home/test/pub...', 47 ) #1 /home/test/public_html/application/controllers/api.php(47): Laravel{closure}(8, '试图获取 p...', '/home/test/pub...', 47 , 大批) #2【内部函数】:Api_Controller->action_sets() #3 /home/test/public_html/laravel/routing/controller.php(323): call_user_func_array(Array, Array) #4 /home/test/public_html/laravel/routing/controller.php(283): Laravel\Routing\Controller->response('sets', Array) #5 /home/test/public_html/laravel/routing/controller.php(165): Laravel\Routing\Controller->execute('sets', Array) #6 /home/test/public_html/laravel/routing/route.php(153): Laravel\Routing\Controller::call('api@(:1)', Array) #7 /home/test/public_html/laravel/routing/route.php(124): Laravel\Routing\Route->response() #8 /home/test/public_html/laravel/laravel.php(125): Laravel\Routing\Route->call() #9 /home/test/public_html/public/index.php(34): require('/home/test/pub...') #10 {主要}