Reputation:
I already know that the problem with this question that I will explain is similar to these other questions.
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
But my problem is not the exactly the same problem of those questions or a another similar question.
I have a website about Sale of Cars where I show a table with all customers and the number of cars that each customer has bought, when I click on the button 'See more details...' the web page redirects to another web page that show more details about the selected customer.
On that webpage I have another table that shows the number of cars, the date, the model and the car's brand, that the customer has purchased, and a button that you can add a new car that the customer has bought.
NOTE:I enter in this website as an ADMIN.
When I click on this button to add new cars. I get this error.
ErrorException (E_ERROR) Trying to get property 'id' of non-object (View: C:\xampp\htdocs\car- shopping\resources\views\CarShop\ShowCustomerCarsDetails.blade.php)
The line of code that this message of mistake emphasizes is:
<td>{{$customer->id}}</td>
This is the Table of customer details that appears after I clicked the button (says: see more details...) on the previous page. Before the page 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'.
And this is the body of the table car that the customer has bought.
<!--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.
And the button to add new cars in the same web page.
<div>
<a href="{{route('NewCars')}}">
<input type="button" name="AddCars" value="ADD NEW CAR">
</a>
</div>
And the route in web.php
Route::get('ListCustomersCar/NewCars',['as' => 'NewCars', 'uses' => 'CustomerCarController@createNewCars']);
The CustomerCarController method createNewCars()
:
public function createNewCars(){
return view('customer.NewCars');
}
For more details I add the tables and his relations:
What I'm doing wrong in my website? If I'm missing something, I will update this question. Thanks!
Upvotes: 2
Views: 528
Reputation: 374
If the error is in
ShowCustomerCarDetails.blade.php
then the problem is with the controller (let's call this controller as CustomerController.php
for now) and the function (as showCustomerCarDetails()
) which is returning the above view file.
So, the possible solution would be to check the controller CustomerController.php
and the function showCustomerCarDetails()
.
Please find below a checklist for debugging the problem,
$customer
variable properly in the showCustomerCarDetails
function, by dumping it dd($customer)
. Here, I am assuming that you know how to get the $customer
variable from the Customer model. It's simple like $customer=Customer::findOrFail($id)
, the $id
parameter is the one that was passed via the URL to the controller and the function.$customer
along with the view file using your preferred method,
like for example return view('CarShop.ShowCustomerCarsDetails', compact($customer))
I hope it solves the problem and in case of any doubt please don't hesitate to ask :)
Upvotes: 1
Reputation: 758
You have to pass to the view()
function an array with the variables that your view will use, e.g.:
return view('customer.newCars',
[
'customer' => $customer,
'car' => $car,
...
]
);
If you want to pass eloquent objects, try:
return view('customer.newCars', compact($customer, $car, ...));
Upvotes: 2