Dev
Dev

Reputation: 1073

How to get data from related table in Laravel (one to many)?

I have two tables: users, orders. I try to get all orders for current user.

Users    Orders
_____    ______
id | name  id | user_id

User model:

public function orders(){
     return $this->hasMany("App\Order");
}

Order model:

public function user(){
    return $this->hasOne("App\User", 'user_id', 'id');
}

Query in controller:

public function index()
{

    $orders = Order::where('user_id', Auth::guard('api')->id())->get();
    return response()->json(
        $orders->user
    );
}

I get NULL result, I do something wrong, because there are related rows in both tables.

Upvotes: 6

Views: 13708

Answers (4)

FUNNYYOU
FUNNYYOU

Reputation: 9

make sure all data is filled in, no data taken from that relation is null, just one data from the relation list contains null it will create an error stating that the variable is null.

model:

public function arviQrs()
    {
        return $this->hasMany(ArviQr::class);
    }

public function merchant()
    {
        return $this->belongsTo(Merchant::class);
    }

controller:

$qrs = ArviQr::orderBy('create_time','desc')->get();

blade:

@foreach ($qrs as $key => $item)
$item->merchant->name
@endforeach

in my database i have one data arviQrs.merchant_id is null, and then i change it to true id and voalaa its not error and running.

its error bcs arviQrs.merchant_id and merchant.id is angker, cannot be null

Upvotes: -1

jaysingkar
jaysingkar

Reputation: 4435

If you want to retrieve all the Orders belonging to the current user, try using the following function.

public function index()
{
    $orders = Auth::user()->with('Orders')->get()->toArray();//To get the output in array
    /*        ^               ^
     This will get the user | This will get all the Orders related to the user*/
     
    return response()->json($orders);
}

As pointed out by @Martin Heralecký, you would also need to change the hasOne() to belongsTo() in Order Model. See following (copied from @Martin Heralecký answer)

public function user(){
    return $this->belongsTo("App\User");// second and third arguments are unnecessary.
}

Why belongsTo():

has_one and belongs_to generally are the same in the sense that they point to the other related model. belongs_to make sure that this model has the foreign_key defined. has_one makes sure that the other model has_foreign key defined.

Your $orders array will look something like this:

User => [
 id => 'user id',
 name => 'user name'
 orders => [
  0 => [
         //order data
       ]
  1 => [
         //order data
       ]
       .
       .
       .
       .
   ]
]

Upvotes: 3

Rys
Rys

Reputation: 479

In User model you can use hasMany relationship, for example in:

App/User.php

Add

public function orders()
{
   return $this->hasMany("App\Order", "user_id", "id");
}

Now you can use this:

return User::find(1)->orders;

Upvotes: 2

Martin Heralecký
Martin Heralecký

Reputation: 5779

In Order model you need to use the belongsTo relationship:

public function user()
{
    return $this->belongsTo("App\User"); // second and third arguments are unnecessary.
}

Upvotes: 2

Related Questions