Не добавляется информация в ларавел 9. Ошибка - Column not found: 1054 Unknown column 'image' in 'field list'
web.php:
Route::get('/admin/add-product', [PageController::class, 'addpage'])->name('addpage');
Route::post('/add-product', [PostController::class, 'addproduct'])->name('addproduct');
PostController:
function addproduct(Request $request){
$this->validate($request,[
'photo'=>'image|mimes:jpeg,png,gif|max:2048' ,
'title'=>'required|max:80',
]);
$image_product = $request->file('photo');
$new_image = rand()."_".$image_product->getClientOriginalName();
$image_product->move(public_path('project/images'),$new_image);
$post = new Products();
$post->image = $new_image;
$post->title = $request['title'];
if($request->user()->post()->save($post)){
$message = 'Success!';
}
return redirect()->back()->with([
'message'=>$message
]);
}
table:
Schema::create('products', function (Blueprint $table) {
$table->id();
$table->string('photo');
$table->string('name', 50);
$table->timestamps();
});
view:
<form action="{{route('addproduct')}}" method="post" enctype="multipart/form-data">
@csrf
<div class="form-group">
<input type="text" class="form-control" placeholder="Title" name="title">
</div>
<div class="form-group">
<label for="">Product_img</label>
<input type="file" class="form-control" class="form-control" placeholder="photo" name="photo">
</div>
<div class="form-group">
<button type="submit" class="btn btn-success">
Post Add
</button>
</div>
</form>
В БД тоже "photo" а не "image". Помогите пожалуйста могу скинуть PageController если нужно.
Ответы (1 шт):
Автор решения: Жора Хзмалян
→ Ссылка
function addproduct(Request $request){
$this->validate($request,[
'photo'=>'image|mimes:jpeg,png,gif|max:2048' ,
'title'=>'required|max:80',
]);
$image_product = $request->file('photo');
$new_image = rand()."_".$image_product->getClientOriginalName();
$image_product->move(public_path('project/images'),$new_image);
$post = new Products();
$post->image = $new_image; //Там Ваш ошибка не $post->image а $post->photo
$post->title = $request['title'];
if($request->user()->post()->save($post)){
$message = 'Success!';
}
return redirect()->back()->with([
'message'=>$message
]);
}