属性 [id] 在 Laravel 8 和 Livewire 2.7 中的 Eloquent 构建器实例中不存在

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

我的 users 可以在 select 从 car 的数据库中创建多个 usercar。我需要获取 selected 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']);
    }
}
$newCar = Car::where('id', '=', $this->car_id)->get();

这条语句没有找到任何数据,所以$newcar是空的,一个空对象没有ID

使用 ->get() 从 table 中检索所有行:

$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() 从 table 中检索单个行/列。

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

// WORK
dd($car->id);