我正在尝试从管理面板上传图像,但该文件未上传或保存在数据库中。
我在我的项目中使用最新版本的 Laravel。为了供您参考,我在下面提供了我的
ProjectController.php
的完整代码。我非常感谢您为解决此问题提供的任何建议或解决方案。
谢谢!
class ProjectController extends Controller
{
public function index()
{
$projects = Project::all();
return view('projects.index', compact('projects'));
}
public function create()
{
return view('projects.create');
}
public function store(Request $request)
{
$request->validate([
'title' => 'required|string|max:255',
'content' => 'required|string',
'thumbnail' => 'required|image|mimes:jpeg,png,jpg,gif,svg|max:2048',
]);
$thumbnailPath = $request->file('thumbnail')->store('thumbnails', 'public');
return redirect()->route('admin.projects.index')->with('success', 'Proje başarıyla oluşturuldu.');
}
public function edit(Project $project)
{
return view('projects.edit', compact('project'));
}
public function update(Request $request, Project $project)
{
$request->validate([
'title' => 'required|string|max:255',
'content' => 'required|string',
'thumbnail' => 'nullable|image|mimes:jpeg,png,jpg,gif,svg|max:2048',
]);
$thumbnailPath = $project->thumbnail; // Eski görsel yolunu koruyun
if ($request->hasFile('thumbnail')) {
if ($thumbnailPath) {
Storage::disk('public')->delete($thumbnailPath); // Eski görseli sil
}
$thumbnailPath = $request->file('thumbnail')->store('thumbnails', 'public'); // Yeni görseli yükle
}
$project->update([
'title' => $request->title,
'content' => $request->content,
'thumbnail' => $thumbnailPath,
]);
return redirect()->route('admin.projects.index')->with('success', 'Proje başarıyla güncellendi.');
}
public function destroy(Project $project)
{
if ($project->thumbnail) {
Storage::disk('public')->delete($project->thumbnail); // Eski görseli sil
}
$project->delete();
return redirect()->route('admin.projects.index')->with('success', 'Proje başarıyla silindi.');
}
}
刀片:
@extends('layouts.admin')
@section('content')
<!-- Edit Project -->
<div class="max-w-[85rem] px-4 py-10 sm:px-6 lg:px-8 lg:py-14 mx-auto">
<div class="mx-auto max-w-8xl">
<div class="text-center">
<h2 class="text-xl text-gray-800 text-left font-bold sm:text-3xl dark:text-white">
Proje Düzenle
</h2>
</div>
<!-- Card -->
<div class="mt-5 p-4 relative z-10 bg-white border rounded-xl sm:mt-10 md:p-10 dark:bg-neutral-900 dark:border-neutral-700">
<form action="{{ route('admin.projects.update', $project->id) }}" method="POST" enctype="multipart/form-data">
@csrf
@method('PUT')
<div class="mb-4 sm:mb-8">
<label for="title" class="block mb-2 text-sm font-medium dark:text-white">Proje Adı</label>
<input type="text" id="title" name="title" value="{{ old('title', $project->title) }}" class="py-3 px-4 block w-full border-gray-200 rounded-lg text-sm focus:border-blue-500 focus:ring-blue-500 disabled:opacity-50 disabled:pointer-events-none dark:bg-neutral-900 dark:border-neutral-700 dark:text-neutral-400 dark:placeholder-neutral-500 dark:focus:ring-neutral-600" required>
</div>
<div class="mb-4 sm:mb-8">
<label for="content" class="block mb-2 text-sm font-medium dark:text-white">İçerik</label>
<textarea id="content" name="content" rows="5" class="py-3 px-4 block w-full border-gray-200 rounded-lg text-sm focus:border-blue-500 focus:ring-blue-500 disabled:opacity-50 disabled:pointer-events-none dark:bg-neutral-900 dark:border-neutral-700 dark:text-neutral-400 dark:placeholder-neutral-500 dark:focus:ring-neutral-600">{{ old('content', $project->content) }}</textarea>
</div>
<div class="mb-4 sm:mb-8">
<label for="thumbnail" class="block mb-2 text-sm font-medium dark:text-white">Thumbnail</label>
<input type="file" id="thumbnail" name="thumbnail" class="py-3 px-4 block w-full border-gray-200 rounded-lg text-sm focus:border-blue-500 focus:ring-blue-500 dark:bg-neutral-900 dark:border-neutral-700 dark:text-neutral-400 dark:placeholder-neutral-500 dark:focus:ring-neutral-600">
@if ($project->thumbnail)
<img src="{{ asset('images/' . $project->thumbnail) }}" alt="Thumbnail" class="mt-4 w-32 h-32 rounded-lg">
@endif
</div>
<div class="mt-6 grid">
<button type="submit" class="w-full py-3 px-4 inline-flex justify-center items-center gap-x-2 text-sm font-semibold rounded-lg border border-transparent bg-blue-600 text-white hover:bg-blue-700 disabled:opacity-50 disabled:pointer-events-none">Güncelle</button>
</div>
</form>
</div>
<!-- End Card -->
</div>
</div>
<!-- End Edit Project Form -->
<script src="https://cdn.ckeditor.com/4.16.0/standard/ckeditor.js"></script>
<script>
CKEDITOR.replace('content', {
filebrowserUploadUrl: "{{ route('admin.projects.upload', ['_token' => csrf_token() ]) }}",
filebrowserUploadMethod: 'form'
});
CKEDITOR.on('instanceReady', function(ev) {
// CKEditor instance'ını al
var editor = ev.editor;
// Beyaz metin ve koyu gri arka plan rengi için stil ekle
editor.document.$.head.insertAdjacentHTML('beforeend', `
<style>
body {
color: #ffffff !important; /* Metin rengini beyaz yapar */
background-color: #262626 !important; /* Arka plan rengini koyu gri yapar */
}
.cke_editable {
color: #ffffff !important; /* Editördeki metin rengini beyaz yapar */
background-color: #262626 !important; /* Editördeki arka plan rengini koyu gri yapar */
}
</style>
`);
});
</script>
@endsection
型号:
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Project extends Model
{
use HasFactory;
protected $fillable = [
'title',
'content',
'thumbnail',
];
}
我尝试过存储:链接但不起作用。
您正在验证表单数据,但并未在项目模型中创建新条目。
public function store(Request $request)
{
$request->validate([
'title' => 'required|string|max:255',
'content' => 'required|string',
'thumbnail' => 'required|image|mimes:jpeg,png,jpg,gif,svg|max:2048',
]);
$thumbnailPath = $request->file('thumbnail')->store('thumbnails', 'public');
// Create a new project
Project::create([
'title' => $request->title,
'content' => $request->content,
'thumbnail' => $thumbnailPath,
]);
return redirect()->route('admin.projects.index')->with('success', 'Proje başarıyla oluşturuldu.');
}