Laravel Starter - PHP User Group (Myanmar)
Laravel Starter - PHP User Group (Myanmar)
Laravel Starter - PHP User Group (Myanmar)
You also want an ePaper? Increase the reach of your titles
YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.
<strong>Laravel</strong> <strong>Starter</strong><br />
}<br />
return;<br />
If($user->passport)<br />
{<br />
echo "The user's passport number is " . $user->passport->number;<br />
}<br />
else<br />
{<br />
echo "This user has no passport.";<br />
}<br />
In this example, we're dutifully checking to make sure that our user object was returned as<br />
expected. This is a necessary step that should not be overlooked. Then, we check whether or<br />
not the user has a passport record associated with it. If a passport record for this user exists, the<br />
related object will be returned. If it doesn't exist, $user->passport will return null. In the<br />
preceding example, we test for the existence of a record and return the appropriate response.<br />
One-to-many relationships<br />
One-to-many relationships are similar to one-to-one relationships. In this relationship type, one<br />
model has many of other relationships, which in turn belongs to the former. One example of a<br />
one-to-many relationship is a professional sports team's relationship to its players. One team<br />
has many players. In this example, each player can only belong to one team. The database tables<br />
have the same structure.<br />
Now, let's look at the code which describes this relationship.<br />
class Team extends Eloquent<br />
{<br />
public function players()<br />
{<br />
return $this->has_many('Player');<br />
}<br />
}<br />
30