Laravel“试图获取非对象的属性”使用数据库值设置数组


Laravel "Trying to get property of non-object" Setting Array with Database Values

我正在尝试解决我在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;
}

此代码更改 ClientsJobs 联结表中列的名称。

public function up()
{
    Schema::create('clients-jobs', function(Blueprint $table)
    {
        $table->increments('id')->unsigned(); 
        $table->integer('client_id')->unsigned();
        $table->foreign('client_id')->references('id')->on('clients');
        $table->integer('job_id')->unsigned();
        $table->foreign('job_id')->references('id')->on('jobs');
        $table->decimal('wage', 4, 2);
        $table->decimal('productivity', 5, 2); // 0.00 - 100.00 (PERCENT)
        $table->timestamps();
    });
}
"

作业"和"客户"表非常简单。

我在上面发布的 index() 函数中遇到了错误,它说

"试图获得非对象的属性"

从生产线开始

'client_name' => ClientsJobs::find( $i )->clients->fname,
对于

设置数组的其他部分,它也让我生气。

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

我试过:

- 使用 FindorFail

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

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

我的猜测是它与 PHP 的类型推导有关,我实际上只需要一个字符串数组,但仍然想使用名称映射,因为我将传递一个我用于其他一些东西的视图。代码实际上更早地工作,但我以某种方式破坏了它(添加新记录或运行作曲家更新?)无论如何,有一些严重的巫毒教正在进行。

提前感谢您的帮助,我正在为非营利组织免费从事这个项目。

附言我正在使用Laravel4.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模型。要使示例正常工作,它需要:

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

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

这同样适用于jobs

在每种情况下,最好确保首先设置所有内容。使用以下方法进行检查:

$clientJob = ClientJobs::find($i);
if (!$clientJob->clients || $clientJob->jobs) throw new 'RangeException("No client or job defined for ClientJob $i");

然后在您喜欢的任何级别捕获异常。

最佳方法

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();

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