Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/288.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php “拉雷维尔”;试图获取非对象的属性“;使用数据库值设置数组_Php_Laravel - Fatal编程技术网

Php “拉雷维尔”;试图获取非对象的属性“;使用数据库值设置数组

Php “拉雷维尔”;试图获取非对象的属性“;使用数据库值设置数组,php,laravel,Php,Laravel,我正在试图解决我在使用PHP时遇到的这个错误,我对这种语言不太熟悉,所以如果你能帮我解决这个问题就好了,我无法解决这个错误 我这里有这个代码: public function index() { $counterino = ClientsJobs::all()->count(); $MasterArray = array(); /* Go Through All of the Records in the Client-Jobs Table and Reso

我正在试图解决我在使用PHP时遇到的这个错误,我对这种语言不太熟悉,所以如果你能帮我解决这个问题就好了,我无法解决这个错误

我这里有这个代码:

    public function index() {
    $counterino = ClientsJobs::all()->count();
    $MasterArray = array();
    /* Go Through All of the Records in the Client-Jobs Table and Resolve their columns to Desired Names */
    for ($i = 1; $i <= $counterino; $i++ ) {
        //Temporary Array for one Name-Resolved-Row of the Table.
        $tempArray = array(
            'id'          => ClientsJobs::find( $i )->id,              // id
            'client_name' => ClientsJobs::find( $i )->clients->fname , // get the first name ( based on fk )
            'job_name'    => ClientsJobs::find( $i )->jobs->name,      // get the name of the job ( based on fk )
            'wage'        => ClientsJobs::find( $i )->wage,            // wage for the job
            'productivity'=> ClientsJobs::find( $i )->producivity      // productivity level for the job
        );
        $MasterArray[] = $tempArray; //add the row
    }
    return $MasterArray;
}
作业和客户表非常简单

上面说,我在上面发布的index()函数中出现了错误

'正在尝试获取非对象的属性'

从网上开始

'client_name' => ClientsJobs::find( $i )->clients->fname,
它也对我设置阵列的其他部分感到愤怒

我已经测试了用于设置数组的各个函数,它们都可以工作,fname还应该返回一个字符串,我使用dd()来获取值

我试过:

-使用FindorFail

-设置不带for循环的数组并手动设置每个元素

-转储函数的多个部分以确保其工作(counterino,数组的所有函数,…)

我的猜测是,这与PHP的类型推断有关,我实际上只需要一个字符串数组,但仍然希望使用名称映射,因为我将向它传递一个用于其他内容的视图。这段代码实际上在更早的时候就开始工作了,但是我不知怎么把它弄坏了(添加了一张新唱片或者运行了一个作曲家的更新?),总之,有一些严重的巫毒正在发生

提前感谢你的帮助,我正在为一个非营利组织免费做这个项目


另外,我正在使用Laravel 4.2和平台2.0,首先,这是一个可怕的做法:

    $tempArray = array(
        'id'          => ClientsJobs::find( $i )->id,              // id
        'client_name' => ClientsJobs::find( $i )->clients->fname , // get the first name ( based on fk )
        'job_name'    => ClientsJobs::find( $i )->jobs->name,      // get the name of the job ( based on fk )
        'wage'        => ClientsJobs::find( $i )->wage,            // wage for the job
        'productivity'=> ClientsJobs::find( $i )->producivity      // productivity level for the job
    );
通过多次调用
ClientJobs::find($i)
,您可以多次执行相同的查找操作—可以是对数据库进行查找,也可以是对缓存层进行查找(如果已配置)

其次,你的问题的答案取决于你的
ClientJobs
模式。要使您的示例发挥作用,它需要:

  • 有效的
    客户关系,定义如下:

     public function clients()
     {
         return $this->hasOne(...);
     }
    
  • 客户端
    还需要是有效的1:1始终存在的关系。i、 e.必须始终有一个客户。如果没有,您很容易受到刚刚得到的错误的影响(因为“clients”魔术最终将为空)

这同样适用于
作业

在任何情况下,最好确保一切都先安排好。使用以下选项进行检查:

$clientJob = ClientJobs::find($i);
if (!$clientJob->clients || $clientJob->jobs) throw new \RangeException("No client or job defined for ClientJob $i");
然后在您喜欢的级别捕获异常

最佳方法
你的方法是非常错误的 如果要返回数组,可以这样做

$counterino = ClientsJobs::all()->toArray();

这将从表中获取所有行,toArray将对象转换为数组

我在客户机模型中使用belongTo我实际上对查找感到尴尬,感谢您指出,我在for循环开始时使用了findorfail,然后才对它们进行了设置,但我取消了设置that@user2837329:没问题。另外,
toArray
在这种情况下不解决任何问题。实际上,除非您100%确定至少有一个对象存在,否则在使用前请仔细检查。@user2837329:对于
项下的
,同样适用:您确定每个作业至少有一个客户端吗?数据库中的一个不一致可能会把这搞砸(提示:在不需要分片的关系数据库上,我倾向于使用FK约束来强制执行这一点)是的,每行只有1个人/作业,我会尝试Hasone我可以做到,但我想将列client_id和Job_id转换为作业和客户端的实际名称。我不确定该怎么做,因为ClientsJobs::all()提供了所有的行,我想在controllerDo中执行。您的表中的id=1。而且mysql查询非常昂贵,因为它会加载您的服务器。我有1、2和3个id作为示例数据
public function index() {
  $masterArray = array();
  ClientsJobs::with('clients', 'jobs')->chunk(200, function($records) use (&$masterArray) {
      foreach ($records as $record) {
        $masterArray[] = array(
          'id'          => $record->id,              // id
          'client_name' => !empty($record->clients) ? $record->clients->fname : null,
          'job_name'    => !empty($record->jobs) ? $record->jobs->name : null,
          'wage'        => $record->wage,
          'productivity'=> $record->productivity,
        );
      }
  });
  return $MasterArray;
}
$counterino = ClientsJobs::all()->toArray();