Spatie images does not upload this is my code
<?php
namespace App\Models;
use Spatie\MediaLibrary\HasMedia;
use Spatie\MediaLibrary\InteractsWithMedia;
class Book extends Model implements HasMedia
{
use HasFactory,InteractsWithMedia;
protected $fillable = ['author_id','title'];
public function author()
{
return $this->BelongsTo(Author::class);
}
}
this is my controller
$book = Book::create($request->all());
if($request->hasFile('cover_image'))
{
$book->addMediaFromRequest('cover_image')->toMediaCollection('cover_images');
}
return redirect('books');
}
So make sure there is a cover image first
if($request->hasFile('cover_image'))
{
dd('cover found!!');
$book->addMediaFromRequest('cover_image')->toMediaCollection('cover_images');
}
@asadali007 so the problem is that you don't upload an image. Show the blade code
no i upload the image i show you my blade code of create.blade.php
<!DOCTYPE html>
<html lang="{{ str_replace('_', '-', app()->getLocale()) }}">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<!-- CSRF Token -->
<meta name="csrf-token" content="{{ csrf_token() }}">
<title>{{ config('app.name', 'Laravel') }}</title>
<!-- javascript -->
<script src="https://code.jquery.com/jquery-3.4.1.slim.min.js" integrity="sha384-J6qa4849blE2+poT4WnyKhv5vZF5SrPo0iEjwBvKU7imGFAV0wwj1yYfoRSJoZ+n" crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected] /dist/umd/popper.min.js" integrity="sha384-Q6E9RHvbIyZFJoft+2mJbHaEWldlvI9IOYy5n3zV9zzTtmI3UksdQRVvoxMfooAo" crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/js/bootstrap.min.js" integrity="sha384-wfSDF2E50Y2D1uUdj0O3uMBJnjuUD4Ih7YwaYd1iqfktj0Uod8GCExl3Og8ifwB6" crossorigin="anonymous"></script>
<!-- Fonts -->
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css" integrity="sha384-Vkoo8x4CGsO3+Hhxv8T/Q5PaXtkKtu6ug5TOeNV6gBiFeWPGFN9MuhOf23Q9Ifjh" crossorigin="anonymous">
</head>
<body>
<div class="container">
<div class="row justify-content-center">
<div class="col-md-8">
<div class="card">
<div class="card-header">Add new books</div>
<div class="card-body">
<form action="{{ route('books.store') }}" method="POST" enctype="POST">
@csrf
Author:
<select name="author_id" class="form-control">
@foreach ($authors as $author)
<option value="{{ $author->id }}">{{ $author->name }}</option>
@endforeach
</select>
Book titles,comma separated:
<input type="text" class="form-control" name="title"/>
<br/>
Cover image
<input type="file" name="cover_image">
<br/> <br/>
<input type="submit" value=" Save book " class="btn btn-primary" />
</form>
</div>
</div>
</div>
</div>
</div>
</body>
</html>
@asadali007 replace enctype on the form
enctype="multipart/form-data"
Please sign in or create an account to participate in this conversation.