Support the ongoing development of Laravel.io →
posted 10 years ago
Eloquent
Last updated 2 years ago.
0

Have you read through the documentation? It's outlined clearly there.

<?php

class Player extends Eloquent
{
	public function rank()
	{
		return $this->hasOne('Rank');
	}
}

class Rank extends Eloquent
{
	public function player()
	{
		return $this->belongsTo('Player');
	}
}
Last updated 2 years ago.
0

Thanks for suggestion @crhayes, I have made a edit in question, that not all players are ranked I should have made it clear first hand only sorry.

Last updated 2 years ago.
0

If not all the players are ranked then the ones that dont have a rank, the relationship should not return anything... You can check for this.

Last updated 2 years ago.
0

manishk3008 said:

Thanks for suggestion @crhayes, I have made a edit in question, that not all players are ranked I should have made it clear first hand only sorry.

You can do one of two things:

// Get all of the ranks with the associated player
$ranks = Rank::with('player')->get();
// Get all players that have a rank
$players = Player::has('rank')->get();
Last updated 2 years ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.