This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class Customer | |
{ | |
/** | |
* Customer name. | |
* | |
* @var string | |
*/ | |
protected $name; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class Address | |
{ | |
protected $street; | |
protected $number; | |
protected $city; | |
protected $state; | |
/** |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// Como vimos, para que possamos instanciar uma classe de clientes precisamos passar o | |
// nome do cliente como parametro, então vamos lá: | |
$customer = new Customer('Jack Sparrow'); | |
// Pronto! Agora temos uma instância da nossa classe de clientes agora, | |
// vamos adicionar endereços a este clientes: | |
$customer->address()->put('main_address', new Address('Street name', '12345', 'City name', 'My state')); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$main_address = $customer->address()->find('main_address')->getFullAddressText(); | |
// Street name, 12345 - City name - My state | |
// Quando utilizamos o método find para buscar um ítem, ele retornará o objeto presente neste índice, | |
// ou null caso seja um indíce não existente. A collection possui também o método findOrFail, funciona de | |
// forma semelhante ao find, porém caso tente buscar um índice não existente, retornará uma exceção do tipo | |
// CollectionItemNotFoundException |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$customer->address()->put('secondary_address', new Address('Highway name', 'km 12345', 'City name', 'My state')); | |
$customer->address()->put('business_address', new Address('Business Street name', '54321', 'City name', 'My state')); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$customer->address()->remove('business_address'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$numItems = $customer->address()->count(); | |
// 3 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$addressess = $customer->address()->all(); | |
/** | |
* [ | |
* 'main_address' => Address::object..., | |
* 'secondary_address' => Address::object..., | |
* 'business_address' => Address::object..., | |
* ] | |
*/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$customer->address()->remove('main_address'); | |
// true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// Each | |
$customer->address()->each(function ($address, $key) { | |
// do something | |
}); | |
// Map | |
$customer->address()->map(function ($address) { | |
// return something |
OlderNewer