试图获取非对象的属性“品牌”(查看:D:\Laravel\Laravel Internship\Laravel file\resources\views\carrental\index.blade.p

Posted

技术标签:

【中文标题】试图获取非对象的属性“品牌”(查看:D:\\Laravel\\Laravel Internship\\Laravel file\\resources\\views\\carrental\\index.blade.php)【英文标题】:Trying to get property 'brand' of non-object (View: D:\Laravel\Laravel Internship\Laravel file\resources\views\carrental\index.blade.php)试图获取非对象的属性“品牌”(查看:D:\Laravel\Laravel Internship\Laravel file\resources\views\carrental\index.blade.php) 【发布时间】:2020-10-31 22:03:38 【问题描述】:

我有两个模型,并且都与 VehiclesBrand 的外键有一对多的关系,每当我想通过使用像 $vehicle->brand->BrandName 这样的车辆模型来获取 Tblbrand 的属性时,错误就会出现,当我在 dd() 函数的帮助下输出,它返回真实答案,当我输出 Tblvehicle::all() 时,它返回关系数组为空,我不知道为什么。

1.App\Models\Tblbrand

2.App\Models\Tblvehicle

型号 Tblbrand:

<?php

/**
 * Created by Reliese Model.
 */

namespace App\Models;

use Carbon\Carbon;
use Illuminate\Database\Eloquent\Model;

/**
 * Class Tblbrand
 */
class Tblbrand extends Model

    protected $table = 'tblbrands';
    public $timestamps = false;
    protected $primaryKey = 'id';
    protected $dates = [
        'CreationDate',
        'UpdationDate'
    ];

    protected $fillable = [
        'BrandName',
        'CreationDate',
        'UpdationDate'
    ];
    public function vehicle()
        return $this->hasMany('App\Models\Tblvehicle','VehiclesBrand');
    

模型车:

<?php

/**
 * Created by Reliese Model.
 */

namespace App\Models;

use Carbon\Carbon;
use Illuminate\Database\Eloquent\Model;

/**
 * Class Tblvehicle
 * @package App\Models
 */
class Tblvehicle extends Model

    protected $table = 'tblvehicles';
    public $timestamps = false;

    protected $casts = [
        'VehiclesBrand' => 'int',
        'PricePerDay' => 'int',
        'ModelYear' => 'int',
        'SeatingCapacity' => 'int',
        'AirConditioner' => 'int',
        'PowerDoorLocks' => 'int',
        'AntiLockBrakingSystem' => 'int',
        'BrakeAssist' => 'int',
        'PowerSteering' => 'int',
        'DriverAirbag' => 'int',
        'PassengerAirbag' => 'int',
        'PowerWindows' => 'int',
        'CDPlayer' => 'int',
        'CentralLocking' => 'int',
        'CrashSensor' => 'int',
        'LeatherSeats' => 'int'
    ];

    protected $dates = [
        'RegDate',
        'UpdationDate'
    ];

    protected $fillable = [
        'VehiclesTitle',
        'VehiclesBrand',
        'VehiclesOverview',
        'PricePerDay',
        'FuelType',
        'ModelYear',
        'SeatingCapacity',
        'Vimage1',
        'Vimage2',
        'Vimage3',
        'Vimage4',
        'Vimage5',
        'AirConditioner',
        'PowerDoorLocks',
        'AntiLockBrakingSystem',
        'BrakeAssist',
        'PowerSteering',
        'DriverAirbag',
        'PassengerAirbag',
        'PowerWindows',
        'CDPlayer',
        'CentralLocking',
        'CrashSensor',
        'LeatherSeats',
        'RegDate',
        'UpdationDate'
    ];

    public function brand()
        return $this->belongsTo('App\Models\Tblbrand','id');
    
    public function booking()
        return $this->hasMany('\App\Models\Tblbooking');
    

carrental.index.blade.php:

@foreach ($vehicles as $vehicle)
    <div class="col-list-3">
        <div class="recent-car-list">
            <div class="car-info-box"> 
                <a href="route('vehicles-details',$vehicle->id) ">
                    <img src="asset('admin/img/vehicleimages')$vehicle->Vimage1 " class="img-responsive" >
                </a>
                <ul>
                    <li>
                        <i class="fa fa-car" aria-hidden="true"></i> $vehicle->FuelType 
                    </li>
                    <li>
                        <i class="fa fa-calendar" aria-hidden="true"></i> $vehicle->ModelYear Model
                    </li>
                    <li>
                        <i class="fa fa-user" aria-hidden="true"></i> $vehicle->SeatingCapacity  seats
                    </li>
                </ul>
            </div>
            <div class="car-title-m">
                <h6><a href=" route('vehicles-details',$vehicle->id) " >$vehicle->brand->BrandName $vehicle->VehiclesTitle </a></h6>
                <span class="price">Rs  $vehicle->PricePerDay /Day</span>
            </div>
            <div class="inventory_info_m">
                <p> $vehicle->VehiclesOverview </p>
            </div>
        </div>
    </div>
@endforeach

【问题讨论】:

欢迎来到 Stack Overflow。请使用tour,了解on-topic 是什么,并阅读How to Ask a Good Question。此外,如果给定的答案回答了您的问题,请不要忘记投票并接受它。 【参考方案1】:

问题出在Tblvehicle 模型中的关系定义中。您将 id 作为关系定义中的外键引用传递,但 id 不是外键。似乎VehiclesBrand 是外键。所以在关系定义中传递它

public function brand()
    return $this->belongsTo('App\Models\Tblbrand','VehiclesBrand');

您可以将id 作为第三个参数传递,它引用相关模型的主键或其他自定义键。阅读 laravel 关系here

【讨论】:

以上是关于试图获取非对象的属性“品牌”(查看:D:\Laravel\Laravel Internship\Laravel file\resources\views\carrental\index.blade.p的主要内容,如果未能解决你的问题,请参考以下文章

Blade - 试图获取非对象的属性

Laravel Join:试图获取非对象的属性

试图在 laravel 5.5 中获取非对象的属性

试图在 laravel 5.4 上获取非对象错误的属性

如何处理试图在laravel中获取非对象错误的属性?

Laravel 5.5 - 试图获得非对象的属性