xử lý cả 2 trường hợp: chọn ảnh từ URL và upload file thật (ok)
🔹 1. Trong Form View
Bạn có thể cho phép chọn 1 trong 2:
Hoặc chọn từ File Manager (URL)
Hoặc upload file thật
{{-- Chọn ảnh từ File Manager --}}
<label>Hình đại diện (URL)</label>
<div class="input-group mb-3">
<input id="thumbnail" class="form-control" type="text" name="featured_image_url" value="{{ old('featured_image_url', $post->featured_image_url ?? '') }}">
<button type="button" class="btn btn-secondary" onclick="openLfm()">Chọn ảnh</button>
</div>
<img id="holder" src="{{ old('featured_image_url', $post->featured_image_url ?? '') }}" style="max-height: 100px">
{{-- Upload ảnh từ máy --}}
<label>Hoặc upload ảnh (file)</label>
<input type="file" name="featured_image_file" class="form-control">
2. Trong Controller
Bạn xử lý như sau để ưu tiên ảnh được upload, nếu không thì dùng URL:
public function store(Request $request) { $data = $request->validate([ 'featured_image_url' => 'nullable|url', 'featured_image_file' => 'nullable|image|mimes:jpg,jpeg,png,webp|max:2048', // các rule khác... ]);
Bạn chỉ cần 1 cột trong database: featured_image, chứa hoặc đường dẫn nội bộ (storage/posts/xxx.jpg) hoặc URL (https://...).
3. Trong View hiển thị ảnh (ví dụ show.blade.php)
Chú ý: Nếu featured_image bắt đầu bằng http, thì dùng trực tiếp. Nếu không, thì dùng asset() để dẫn tới storage/.
PreviousGộp toàn bộ phần khởi tạo CKEditor và xử lý ảnh vào 1 file JS riêngNextHiển thị ảnh có chứa https, http và không chứa https, http (ok)
Last updated
Was this helpful?