Support the ongoing development of Laravel.io →
posted 8 years ago
Eloquent

I have two tables: books and photos table. I stored all the information of books in the books table and the photos table contains the book_id which is a foreign key of the books to the photos. I have also setup the relationships between the two table which is

Book.php

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Book extends Model
{
	protected $table ='books';

	protected $fillable = ['book_name', author', 'price', 'isbn'];
		
	public function photo(){
		return $this->hasOne('App\Photo');
	}

}

and

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Photo extends Model
{
	protected $table = 'photos';
	protected $fillable = ['book_id','file_name','file_size','file_mime','file_path','created_by'];

	public function book(){
		return $this->belongsTo('App\Dosage');
	}
}

Now based on the relationships. maybe I can use some eloquent which i can do some like:

$photos = $this->book->photo

and pass it to a view and loop it through. How can i do it ? can you share some thoughts?

Last updated 3 years ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

finger finger Joined 23 Jan 2017

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2025 Laravel.io - All rights reserved.