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?
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community