Reputation: 5772
I am trying to get all images that belong to a specific album in Laravel with Eloquent. These are my tables:
Table: albums
Rows: id, name, description, user_id
Table: album_images
Rows: id, file_name
Table: album_album_image
Rows: album_id, album_image_id
My Album model:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Album extends Model
{
public function albumImages(){
return $this->belongsToMany('App\AlbumImage', 'album_album_image','album_id','album_image_id');
}
}
My AlbumImage model:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class AlbumImage extends Model
{
public function album(){
return $this->belongsToMany('App\Album', 'album_album_image','album_image_id','album_id');
}
}
Function that sends the $images variable to my specificAlbum view:
public function specificAlbum($albumName){
$images =
return view ('specificAlbum', ['images' => $images]);
}
Now I need to have the $images variable get all the images that are associated with album with album name $albumName. Unfortunately, I can't seem to figure it out. Any help would be appreciated.
Upvotes: 1
Views: 1133
Reputation: 1
Route::get('/album/{kategori}', 'App\Http\Controllers\EmployeeController@album')->name('album');
Controller
public function album($kategori){
$defaultAlbumId = null;
switch($kategori) {
case 'sports':
$defaultAlbumId = 1;
break;
case 'aesthetic':
$defaultAlbumId = 2;
break;
case 'animasi':
$defaultAlbumId = 3;
break;
default:
break;
}
$albumImages = Employee::where('kategori', $kategori)->get();
return view('album', compact('albumImages'));
}
View
Route::get('/album/{kategori}', 'App\Http\Controllers\EmployeeController@album')->name('album');
Controller
public function album($kategori){
$defaultAlbumId = null;
switch($kategori) {
case 'sports':
$defaultAlbumId = 1;
break;
case 'aesthetic':
$defaultAlbumId = 2;
break;
case 'animasi':
$defaultAlbumId = 3;
break;
default:
break;
}
$albumImages = Employee::where('kategori', $kategori)->get();
return view('album', compact('albumImages'));
}
View
<body class="dark">
@include('navbar')
<center><h1>Album</h1></center>
<div class="container">
<div class="row">
@if($albumImages->isEmpty())
<div class="col-md-12">
<p>Tidak ada gambar yang ditemukan.</p>
</div>
@else
@foreach($albumImages as $image)
<div class="col-md-4">
<div class="card mb-4 shadow-sm">
<img src="{{ asset('foto/' . $image->gambar) }}" class="img-fluid" alt="{{ $image->judul }}">
<div class="card-body">
<p class="card-text">{{ $image->judul }}</p>
<p class="card-text">{{ $image->deskripsi }}</p>
</div>
</div>
</div>
@endforeach
@endif
</div>
</div>
</body>
Upvotes: 0
Reputation: 7489
Define the images relationship in Album model
class Album extends Model
{
public function images(){
return $this->belongsToMany('App\AlbumImage', 'album_album_image','album_id','album_image_id');
}
}
Then:
public function specificAlbum($albumName)
{
$album = Album::with('images')->where('name', '=', $albumName)->firstOrFail();
return view ('specificAlbum', ['images' => $album->images]);
}
Upvotes: 2