Reputation: 69
I have a localhost database named as app which has two tables named as customers and orders, I am using laravel relations to fetch results from the database, the results should be the orders and the customer against each order who has ordered that, for that I have written this code but this is throwing an error kindly help.
routes.php
Route::get('orders', function(){
$orders = App\Orders::all(); //here App=folder, Orders=file or class
foreach($orders as $order){
echo $order->detail." ordered by ".$order->customer->name."<br/>";
}
});
Orders.php
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Orders extends Model{
public function customer() {
return $this->belongsTo('App\Customer');
}
}
Customers.php
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Customer extends Model{
}
error
Whoops, looks like something went wrong.
1/1
ErrorException in routes.php line 24:
Trying to get property of non-object
in routes.php line 24
at HandleExceptions->handleError('8', 'Trying to get property of non-object', 'C:\xampp\htdocs\blog\app\Http\routes.php', '24', array('orders' => object(Collection), 'order' => object(Orders))) in routes.php line 24
at RouteServiceProvider->{closure}()
at call_user_func_array(object(Closure), array()) in compiled.php line 7298
at Route->runCallable(object(Request)) in compiled.php line 7285
at Route->run(object(Request)) in compiled.php line 6954
at Router->Illuminate\Routing\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in compiled.php line 8952
at Pipeline->Illuminate\Pipeline\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in compiled.php line 8935
at Pipeline->then(object(Closure)) in compiled.php line 6955
at Router->runRouteWithinStack(object(Route), object(Request)) in compiled.php line 6944
at Router->dispatchToRoute(object(Request)) in compiled.php line 6929
at Router->dispatch(object(Request)) in compiled.php line 1935
at Kernel->Illuminate\Foundation\Http\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in compiled.php line 8952
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in compiled.php line 2438
at VerifyCsrfToken->handle(object(Request), object(Closure)) in VerifyCsrfToken.php line 17
at VerifyCsrfToken->handle(object(Request), object(Closure)) in compiled.php line 8944
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in compiled.php line 12083
at ShareErrorsFromSession->handle(object(Request), object(Closure)) in compiled.php line 8944
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in compiled.php line 10785
at StartSession->handle(object(Request), object(Closure)) in compiled.php line 8944
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in compiled.php line 11789
at AddQueuedCookiesToResponse->handle(object(Request), object(Closure)) in compiled.php line 8944
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in compiled.php line 11738
at EncryptCookies->handle(object(Request), object(Closure)) in compiled.php line 8944
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in compiled.php line 2478
at CheckForMaintenanceMode->handle(object(Request), object(Closure)) in compiled.php line 8944
at Pipeline->Illuminate\Pipeline\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in compiled.php line 8935
at Pipeline->then(object(Closure)) in compiled.php line 1891
at Kernel->sendRequestThroughRouter(object(Request)) in compiled.php line 1880
at Kernel->handle(object(Request)) in index.php line 53
Upvotes: 0
Views: 482
Reputation: 62278
The only thing I can see in the code provided that would cause this error is $order->customer->name
. If there is no customer associated with the order, then $order->customer
will be null, and you will get an error when trying to access the name
attribute on a null object.
Upvotes: 1