I want show gallery with parent and child. I have code in controller to call model like this.
$galery = GalleryModel::with(['gallerydetail'])->get();
And i have code for get table and column in model :
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class GalleryModel extends Model {
protected $table = 'tbl_gallery';
protected $primary_key = 'id_gallery';
protected $fillable = ['category_id','author_id','gallery_title','gallery_text','gallery_time','gallery_date','gallery_isdelete'];
public function gallerydetail() {
return $this->hasMany(GalleryDetail::class, 'gallery_id');
}
}
class GalleryDetail extends Model {
protected $table = 'tbl_gallery_detail';
protected $primaryKey = 'id_detail_gallery';
protected $fillable = ['gallery_id','image'];
}
Gallery Detail can't show, when i call use ::with. I test with used dd and i can't get items.
Before i create this, i try to create for menu and its work, i can get data like this. result menu
The code is same, but for gallery it's doesn't work. i don't know why, becase i can't get the problem or error. Please help me, thanks
My Data in Database Table Galery
ID Gallery | Gallery Title |
---|---|
231 | Tes |
Table Detail Gallery
Id Detail Gallery | gallery_id | image |
---|---|---|
1 | 231 | back.jpg |
2 | 231 | zone.jpg |
You don't have to add second argument in hasMany in gallerydetail() function, just add belongsTo function in your GalleryDetail Model.
GalleryModel:
public function gallerydetail() {
return $this->hasMany(GalleryDetail::class);
}
GalleryDetail:
public function gallerymodel() {
return $this->belongsTo(GalleryModel::class, 'gallery_id');
}