来自两个表格laravel的数据

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

If you have an instance of a $restaurant, you can get a collection of it's reviews like this:

$reviews = $restaurant->reviews;

Since this is a many to many relationship it returns a collection, and you can now @foreach over this collection as well:

@foreach(array_slice($restaurants, 0, 5) as $restaurants)

    {{ $restaurant[0]->restaurant_id }}

    @foreach($restaurant->reviews as $review)
        {{ $review->rating }}
    @endforeach

@endforeach

The reviews property exists on the restaurant model because you defined this method:

public function reviews()
{
    return $this->hasMany('restaurant_id');
}

Accessing related properties in this manner is similar to the first example given in the documentation. If you want to look at the source and see whats going on, here is the function that is called whenever you access a model property that isn't defined specifically in the class (i.e. not $fillable or $guarded, but any of the data attributes, relations accessed like attributes, etc).

If instead you had a Chef model and a database structure where a restaurant could have many chefs and you defined this method in your restaurant model:

public function chefs() 
{
    return $this->hasMany('Chef');
}

Then you could get a listing of all of the chefs at a restaurant like this:

$chefs = $restaurant->chefs;

You can also call the method directly, if for instance you wanted to add further restrictions to the query:

$chefsNamedBob = $restaurant->chefs()->where('name', '=', 'Bob')->get();

Notice that, when called this way, you will have to call ->get() or ->first() as well, which is different when attempting to access relations via a property of an instance rather than as a method on an instance. This is because when you call the method, the query has not been executed yet (that's what get() does). When you access the relationship as a property, by the time you receive the results the query is already executed (meaning there's no more time left for SQL to do the filtering for you).

Update

I also notice that you have an error in your relations. You define your restaurant to review relationship like this:

return $this->hasMany('restaurant_id');

The first argument to the hasMany function is not a column name. In general the function signature for a relationship function is this:

return $this->hasOne('ModelClassName', 'foreign_key', 'local_key');

See the documentation for more information. If you are following the eloquent conventions (plural table names, id as primary key, foreign key names are correct) then you can use a shortcut and just specify the model class name:

return $this->belongsToMany('ModelClassName');

Which is a great reason to follow convention.