I'm Trying to Preview images from database as im saving album in table albums and album images in table images and connecting with relation but i can't preview the images from table image so i'm adding relations between the 2 tables and trying to preview the images but its not working with me something went wrong but i cannot figure it
Blade.php
<tbody>
@foreach ($albums as $album)
<tr>
<th scope="row">{{$album->id}}</th>
<td>{{$album->name}}</td>
<td>{{$album->category_id}}</td>
<td><img style="width:5vw; hieght:5vh" src="{{$album->id}}" alt=""></td>
<td>
<a href="{{route('albums.show',$album->id)}}" >Show</a>
@if (Auth::user()->role_id==1)
<a href="{{route('albums.edit',$album->id)}}" >Edit</a>
<form action="{{route('albums.destroy',$album->id)}}" method="post" >
@csrf
@method('DELETE')
<input type="submit" value="Delete" />
</form>
@else
@endif
</td>
</tr>
@endforeach
</tbody>
AlbumsController
public function index()
{
$albums = Album::with('images')->get();
return view('admin.albums.all',compact('albums'));
}
/**
* Show the form for creating a new resource.
*
* @return \Illuminate\Http\Response
*/
public function create()
{
$albums=Album::all();
$categories=Category::all();
return view('admin.albums.create',compact('categories'));
}
/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function store(Request $request)
{
$album=Album::create([
'name'=>$request->name,
'price'=>$request->price,
'image'=>$request->image,
'category_id'=>$request->category_id,
]);
foreach($request->images as $image){
$filename = $image->store('album');
$images=Image::create([
'album_id' => $album->id,
'filename' => $filename
]);
}
$albums=Album::with('images')->get();
return view('admin.albums.all',compact('albums'));
}
Image Model
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Image extends Model
{
use HasFactory;
protected $fillable = ['album_id','filename'];
public function images(){
return $this->belongsTo(Album::class,'id');
}
}
Album Model
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Album extends Model
{
use HasFactory;
protected $fillable = [
'name',
'category_id',
];
public function categories(){
return $this->belongsTo(Category::class,'category_id','id');
}
public function images(){
return $this->HasMany(Image::class,'filename','id');
}
}
table images Migration
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('images', function (Blueprint $table) {
$table->id();
$table->biginteger('album_id')->unsigned();
$table->foreign('album_id')->references('id')->on('albums');
$table->string('filename');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('images');
}
};
Table albums Migration
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('albums', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->integer('category_id');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('albums');
}
};
CodePudding user response:
you have an invalid foreign key in your model Album. In yours code 'filename'
images(){
return $this->HasMany(Image::class,'filename','id');
}
However in migration for image table you set another foreign key
$table->foreign('album_id')->references('id')->on('albums');
CodePudding user response:
<img style="width:5vw; hieght:5vh" src="{{$album->id}}" alt="">
Do you have image with that id with no file extension at the end like {{ $album->id}}.png ?
Your
Album
model looks wrong
<?php
class Album extends Model
{
public function images(){
// Laravel can figure it out if you use the convention
return $this->hasMany(Image::class);
}
}