Last active
November 10, 2017 11:52
-
-
Save VictorFursa/16084e85ed8c39460f03db0ff291086e to your computer and use it in GitHub Desktop.
This file contains hidden or 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
class Cupon extends Model | |
{ | |
/** | |
* Получить продукт где купон id связан. | |
*/ | |
public function product() | |
{ | |
return $this->belongsTo('App\Product'); | |
} | |
} |
This file contains hidden or 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
$cupon = App\Cupon::find(1); - найти купон по id = 1 | |
echo $cupon->product->title; | |
____________________________ | |
$product = App\Product::find(1); | |
foreach($product->cupons as $cupon) { | |
$cupon-id; | |
} | |
____________________________________ | |
$prodcuts = App\Product::findAll(); | |
foreach($products as $product) { | |
foreach($product->cupons as $cupon) { | |
$cupon->id | |
} | |
} | |
___________________________________________ | |
$cupons = App\Product::find(1)->cupons; | |
foreach($cupons as $cupon) | |
{ $cupon->id } |
This file contains hidden or 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
class Product extends Model | |
{ | |
/** | |
* Получить купон. | |
*/ | |
public function cupons() | |
{ | |
return $this->hasMany('App\Cupon'); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Спасибо)