将变量传递给查询会产生错误“使用Laravel试图获取非对象的属性'name'”

问题描述 投票:0回答:1

我正在使用Laravel6。我正在尝试使用表单创建会议来创建验证系统。当用户与已经被另一个会议占用的参与者创建会议时,视图中应显示一条消息,其中已被占用的参与者的名称。由于某种原因,应该找到参与者名称的功能无法正常工作。我在foreach循环中传递了一个id,但运行表单时出现以下消息:“试图获取非对象的属性'name'”。奇怪的是,传递给该函数的id可以,但是如果我在查询中以数字(例如“ 8”)代替$ id则在视图中正确显示了名称“ Chris”。表会议中“ id_participants”列的格式为以下“ 23; 7; 6”。

<?php

namespace App\Rules;

use Illuminate\Contracts\Validation\Rule;
use DB;
use App\User;



class CheckParticipant implements Rule
{
    protected $participants_occupied = array();


    /**
     * Create a new rule instance.
     *
     * @return void
     */
    public function __construct()
    {
        //
    }

    /**
     * Determine if the validation rule passes.
     *
     * @param  string  $attribute
     * @param  mixed  $value
     * @return bool
     */
    public function passes($attribute, $value)
    {
        $participants = request('participants');

        foreach($participants as $participant) {

            $meetings = DB::table('meetings')
            ->where('is_active', '1')
            ->where('date', request('date_meeting'))
            ->where(function ($query) {
                $query->where(function($sub_q) {
                        $sub_q->where('start_hour', '>=', request('start'))
                                ->where('start_hour', '<', request('end'));
                    })
                    ->orWhere(function($sub_q) {
                        $sub_q->where('start_hour', '<', request('start'))
                                ->where('end_hour', '>=', request('end'));
                    })
                    ->orWhere(function($sub_q) {
                        $sub_q->where('end_hour', '>', request('start'))
                                ->where('end_hour', '<=', request('end'));
                    });
            })
            ->where(function ($query) use($participant) {
                $query->where('id_participants', $participant)
                    ->orWhere('id_participants', 'like', '%;'.$participant)
                    ->orWhere('id_participants', 'like', $participant.';%')
                    ->orWhere('id_participants', 'like', '%;'.$participant.';%');
            })
            ->get();

            if(count($meetings) > 0) {
                array_push($this->participants_occupied, $participant);
            }
        }

        if(count($this->participants_occupied) > 0) {
            return false;
        } else {
            return true;
        }
    }

    /**
     * Get the validation error message.
     *
     * @return string
     */
    public function message()
    {
        for($i = 0; $i < count($this->participants_occupied); $i++) {
            $this->participants_occupied[$i] = $this->getNameSurnameById($this->participants_occupied[$i]);
        }
        return 'The participants are already occupied at that time: ' . implode(',', $this->participants_occupied);

    }

    public function getNameSurnameById($id)
    {
        $users = User::all()->where('id', 18)->first(); //if I write a number in place of $id everything works

        return $users->name;
    }

}

我希望该程序可以动态运行。我想在查询中变量$ id有问题。有人可以帮助我吗?

更新:

我解决了如下修改消息功能的问题:

public function message()
{
    $arr_names = array(); //I created this array

    for($i = 0; $i < count($this->participants_occupied); $i++) {
        array_push($arr_names, $this->getNameSurnameById($this->participants_occupied[$i]));
    }

    return 'The following participants are already occupied at that time: ' . implode(', ', $arr_names);


}

我想问题在于,我给具有整数值(参与者的ID)的数组提供了一个字符串值(参与者的名称)。我解决了创建一个新的空数组的问题,并将名称推到了新数组中。

php laravel validation eloquent
1个回答
1
投票

您可能会发现,根据某种类型的Laravel对象而不是数组来获取id要容易得多。我怀疑在循环中的某个时刻,数组在id的索引处具有不正确的值(不是$i)。而且,正如@CristóbalRamos Merino的评论所指出的那样,您正在尝试将可能的id传递给getNameSurnameById()方法的同时,将变量设置为潜在字符串(用户名)。 。

我将获取从表单传递的所有id,对User进行数据库查询以查看谁已经被占用,然后从结果集合中提取名称。

类似:

$allFormUsers = User::whereIn('id', $formIds)->get();

然后在此循环以获取那些被占用者的名称:

$occupiedNames = [];
foreach($AllFormUsers->where('occupied', 1) as $u){
     $occupiedNames[] = $u->name;
}

我不知道您如何跟踪占用的-因此上面的代码只不过是伪代码,但希望能给您一个在没有数组/并发的情况下如何做到这一点的想法。由于您只有一个查询,而不是每次都循环访问单个查询,因此在数据库上的工作也要少一些。您甚至可以首先拉动所有用户,以便存储它们,然后根据需要对集合进行where('occupied', 1),如上述循环所示。 (假设这是您跟踪占用情况的方式)

© www.soinside.com 2019 - 2024. All rights reserved.