php - 我在尝试获取 Laravel 中列的属性时遇到问题

标签 php laravel phpmyadmin

我已经知道我将解释的这个问题的问题与其他问题类似。

Trying to get property of non-object

Trying to get property of non-object in laravel 5.4

Trying to get property of non-object in laravel 5.5

但我的问题与这些问题或另一个类似的问题并不完全相同。

我有一个关于汽车销售的网站,其中显示了包含所有客户以及每个客户购买的汽车数量的表格,当我单击“查看更多详细信息...”按钮时,网页会重定向到另一个页面显示有关所选客户的更多详细信息的网页。

在该网页上,我有另一个表格,显示客户购买的汽车数量、日期、型号和汽车品牌,以及一个可以添加客户购买的新车的按钮。

NOTE:I enter in this website as an ADMIN.

当我单击此按钮添加新车时。我收到这个错误。

ErrorException (E_ERROR) Trying to get property 'id' of non-object (View: C:\xampp\htdocs\car- shopping\resources\views\CarShop\ShowCustomerCarsDetails.blade.php)

该错误消息强调的代码行是:

<td>{{$customer->id}}</td>

这是我单击上一页上的按钮(显示:查看更多详细信息...)后出现的客户详细信息表。在页面 ShowCustomerCarDetails.blade.php 之前。

<!--ShowCustomerCarDetails.blade.php-->

<tbody> 
  <tr>


    <td>{{$customer->id}}</td> <!--THIS LINE-->


    <td>{{$customer->name}}</td>
    <td>{{$customer->address}}</td>
    <td>{{$customer->phoneNumber}}</td>

        @if($car->id == $customer->car_id)
            <td>{{$car->model}}</td>
        @endif           
    </tr>
</tbody>

NOTE: The previous page of 'ShowCustomerCarDetails.blade.php' is called, 'ShowCustomersAndCars_index.blade.php' On this webpage I have a table that shows all customers and the number of cars that they have bought, is in this page that I have a button called 'See more details...' that redirect to 'ShowCustomerCarDetails.blade.php'.

这就是客户购买的 table 车的车身。

<!--At the end of the website 
ShowCustomerCarDetails.blade.php -->

<thead>
  <tr>
     <th>CAR NAME</th>
     <th>CAR MODEL</th>
     <th>CAR PRICE</th>
     <th>CAR YEAR</th>
     <th>PURCHASE DATE</th>
     <th>OWNER</th>
    </tr>

</thead>

<tbody>
    @foreach($date as $carDateBuy)
        @if($customer->id == $carDateBuy->customer_id)
            <tr>
                <td>{{$car->name}}</td>             
                <td>{{$car->model}}</td>
                <td>{{$car->price}}</td>
                <td>{{$car->year}}</td>
                <td>{{$carDateBuy->date}}</td>
                <td>{{$carDateBuy->customer_name}}</td>
            </tr>
        @endif
    @endforeach
</tbody>

NOTE: The Table dates, it fills automatically with customer's information when a customer buys a car.

以及在同一网页中添加新车的按钮。

<div>
  <a href="{{route('NewCars')}}">
    <input type="button" name="AddCars" value="ADD NEW CAR">
  </a>
</div>

以及web.php中的路由

Route::get('ListCustomersCar/NewCars',['as' => 'NewCars', 'uses' => 'CustomerCarController@createNewCars']); 

CustomerCarController方法createNewCars():

public function createNewCars(){

  return view('customer.NewCars');
}

有关更多详细信息,我添加了表及其关系:

Picture1_Car-Customer

Picture2_Customer-Date1

Picture3_Customer-Date2

我的网站做错了什么?如果我遗漏了什么,我会更新这个问题。谢谢!

最佳答案

您必须向 view() 函数传递一个包含 View 将使用的变量的数组,例如:

return view('customer.newCars', 
    [
        'customer' => $customer,
        'car' => $car,
        ...
    ]
);

如果你想传递 Eloquent 对象,请尝试:

return view('customer.newCars', compact($customer, $car, ...));

关于php - 我在尝试获取 Laravel 中列的属性时遇到问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52544637/

相关文章:

php - 用户交互式刷新动态菜单

javascript - 根据链接 url 禁用悬停在图像上

phpMyAdmin 参数错误

mysql - 将 .sql 文件插入 MySQL 数据库

javascript - 我的 anchor 标记没有采用 javascript 函数和 php 中的内联样式表

PHP PDO类编程:Fatal error: Call to a member function fetchAll() on boolean

php - 使用 Mailgun 在 Laravel 5.4 中发送邮件得到错误代码 "401 UNAUTHORIZED` 响应 : Forbidden "

php - 使用 laravel 向 api 发出多个请求

javascript - 如何使用 Javascript 调用路由并销毁数据库中的数据?

mysql - 如何横向查询显示数据?