I am currently building a project / learning laravel.
i have two databaes with one colomn in both tables with teh same unique value.
in my blade page i have a foreach which loops through the first table but i cannot figure out how to get it to correctly bring the second table data with it etc.
here is the code.
my resturants model.
class Restaurant extends Eloquent implements UserInterface, RemindableInterface {
use UserTrait, RemindableTrait;
protected $table = 'restaurants';
public function reviews()
{
return $this->hasMany('restaurant_id');
}
Reviews model
class Review extends Eloquent implements UserInterface, RemindableInterface {
use UserTrait, RemindableTrait;
/**
* The database table used by the model.
*
* @var string
*/
protected $table = 'reviews';
public $review;
public function reviews()
{
return $this->hasMany('restaurant_id');
}
blade foreach loop
@foreach(array_slice($restaurants, 0, 5) as $restaurants)
{{$restaurant[0]->restaurant_id}}}
{{$reviews= Review::find(1)->where('restaurant_id', '=', $restaurant[0]->restaurant_id)->get()}}
@emdforeach
the controller passes data fine so ill exclude that.
i am trying to get the relevant review to pass along with the restaurant object etc.
any ideas?
if you see any typos excuse as i had to retype on a laptop and desktop code is good :)
UPDATE
Thank you very much for your help, after your explanation i understand it so thanks :)
none the less when i try it i get Trying to get property of non-object.
Here is the updated code
Restaurant model
class Restaurant extends Eloquent implements UserInterface, RemindableInterface {
use UserTrait, RemindableTrait;
/**
* The database table used by the model.
*
* @var string
*/
protected $table = 'restaurants';
public $restaurant;
public function reviews()
{
return $this->hasOne('Review', 'restaurant_id', );
}
In my view
v {{ dd($restaurants->reviews) }}
this gives me Trying to get property of non-object.
$restaurants works perfectly just not with reviews? i can dd it and see the 5 objects in the array