簡體   English   中英

Laravel 8 和 Livewire 2.7 中的 Eloquent 構建器實例上不存在屬性 [id]

[英]Property [id] does not exist on the Eloquent builder instance in Laravel 8 and Livewire 2.7

car的數據庫中選擇時,我的users可以創建多個usercar 我需要獲取選定的car值,但是當我dd值時,我得到一個Property [id] does not exist on the Eloquent builder instance. 錯誤。 wire:model="car_id"是下面代碼中使用的 select 輸入值。

AddUserCar.php

...
class AddUserCar extends Component
{
    public $showAnotherModel = false;

    // Steps
    public $totalSteps = 8;
    public $step = 1;
    // User
    public $super;
    public $first_name;
    public $last_name;
    public $email;
    public $status = 1;
    public $role = 'Driver';
    // Usercar
    public $car_id;
    public $calc_date;
    public $year;
    public $model;
    // Form Function
    public $car_year;

    //public $cars;
    //public $modelId;

    ...

    public function moveAhead()
    {

        if($this->step == 1) {
            $newCar = Car::where('id', '=', $this->car_id)->get();
            dd($newCar->id); // This is where I get error

            $this->usercarCreated = Usercar::create([
                'year' => $newCar->start_year,
                'model' => $newCar->model,

                'car_id' => $this->car_id,
                'user_id' => Auth::user()->id,
                'tenant_id' => Auth::user()->tenant_id,
                'calc_date' => now()->format('m-d-Y'),
            ]);

            $this->resetErrorBag();
            $this->resetValidation();
        }

        ...

    public function render()
    {
        return view('livewire.add-user-car',
        ['pageTitle' => 'Add Driver Car']);
    }
}

使用->get()從表中檢索所有行:

$cars = Car::where('id', '=', $this->car_id)->get();

foreach($cars as $car){
    echo $car->id;
}

// ERROR, because $cars isn't a single row
dd($cars->id);

使用->first()從表中檢索單行/列。

$car = Car::where('id', '=', $this->car_id)->first();
// or 
$car = Car::find($this->car_id);

// WORK
dd($car->id);
$newCar = Car::where('id', '=', $this->car_id)->get();

這個語句沒有找到任何數據,所以$newcar是空的,一個空的object是沒有ID的

我認為這是因為$this->car_idCar model 上不存在,如果您可以訪問數據庫,您能否檢查Car model 的表中是否存在$this->car_id car_id ?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM