php - 流明:使用助手获取错误:不在对象上下文中时使用 $this
问题描述
我在 Http 文件夹内的 Helpers 文件夹中创建了这个助手:
<?php
namespace App\Http\Helpers;
class CreateRandomId{
public static function get_id($my_table)
{
$id = mt_rand(1000000000, 9999999999);
if($this->check_id($id,$my_table)){
get_id($my_table);
}
return $id;
}
public static function check_id($id,$my_table)
{
$table=DB::table($my_table)->where('id',$id)->get();
if (count($table)==0){
return false;
}
return true;
}
}
当我在控制器中调用它时:
$new_user_id = CreateRandomId::get_id('users');
它给了我这个错误:Using $this when not in object context
我在函数中删除 $this 我得到了这个错误:Call to undefined function App\Http\Helpers\check_id()
解决方案
check_id 是一个静态函数,如果你想将它访问到其他函数中,那么你必须使用...
self::check_id($id,$my_table)
推荐阅读
- java - 无限滚动视图 firebase 实时数据库错误
- java - 使用 _barVisualizer.setAudioSessionId(audioSessionId); 时应用程序崩溃;
- django - RuntimeError:模型类 himalayadashbords.clients.models.Client 未声明显式 app_label 且不在 INSTALLED_APPS 中的应用程序中
- java - AlertDialog 不断崩溃
- python - Discord.py 成员具有特定角色
- api - Coinbase API 是否有结构化定义?
- java - 具有质量碰撞问题的二维粒子(JAVA)
- python - 需要为书店网络应用显示同一作者的书籍
- jquery - 在主页面显示/隐藏部分视图
- php - 如何在 WooCommerce 中分配和显示变体的产品属性