Reputation: 339
I have my model like this: Product has a relation with country
class Product extends Model
{
public function country()
{
return $this->belongsTo('App\Country','countrie_id');
}
}
I have this query in my controller:
$products = Product::where('name', 'LIKE', '%'.$request->search.'%')->get();
It returns:
"id": 1,
"categorie_id": 1,
"countrie_id": 1,
"solution_id": 1,
"line_id": 1,
"name": "COLAGEN PREMIUN",
"internal_code": "1321368478978",
"description": "FSDFSDF",
"composition": "colageno de acido citrico",
"benefits": "ayuda a al anemia",
"price": 250,
"price_sale": 500,
"active": 1
I'd like to know how to retrive the country name and not the country_id with that relation?
Upvotes: 0
Views: 110
Reputation: 1439
Use with on your Eloquent product model
$products = Product::with('country')->where('name', 'LIKE', '%'.$request->search.'%')->get();
In your view you now can use:
@foreach($products as $product)
{{$product->country->name}}
@endforeach
Upvotes: 1
Reputation: 1918
you can get country name via foreach loop in blade
@foreach($products as $product)
{{$product->country->name}}
@endforeach
Upvotes: 0