I need help, I followed a laravel tutorial for a blog as I am new to it. As I slowly learn all of what I've used to create the project. The individual who created the blog tutorial uses a string system to pull URLs to fill the images for the blog post. Could someone help me convert it from string to file upload instead? I've created an images controller but it's pretty much empty.
BlogsController
>!<?php
declare(strict_types=1);
namespace App\Http\Controllers;
use App\Http\Requests\BlogsUpdateRequest; use App\Http\Requests\DatatablesRequest; use App\Modules\Blogs\BlogsService; use Exception; use Illuminate\Http\JsonResponse; use Illuminate\Http\Response;
class BlogsController extends Controller {
public function __construct( private readonly BlogsService $service ) {
}
public function index(DatatablesRequest $request): JsonResponse { try { return response()->json($this->service->index($request->data())); } catch (Exception $error) { return response()->json( [ "exception" => get_class($error), "errors" => $error->getMessage() ], Response::HTTP_BAD_REQUEST ); } }
public function get(int $id): JsonResponse { try { return response()->json($this->service->get($id)); } catch (Exception $error) { return response()->json( [ "exception" => get_class($error), "errors" => $error->getMessage() ], Response::HTTP_BAD_REQUEST ); } }
public function update(BlogsUpdateRequest $request): JsonResponse { try { return response()->json($this->service->update($request)); } catch (Exception $error) { return response()->json( [ "exception" => get_class($error), "errors" => $error->getMessage() ], Response::HTTP_BAD_REQUEST ); } }
public function delete(int $id): JsonResponse { try { $this->service->delete($id); return response()->json(null, Response::HTTP_NO_CONTENT); } catch (Exception $error) { return response()->json( [ "exception" => get_class($error), "errors" => $error->getMessage() ], Response::HTTP_BAD_REQUEST ); } }
}!<
BlogsUpdateRequest
>!<?php
declare(strict_types=1);
namespace App\Http\Requests;
use Illuminate\Foundation\Http\FormRequest;
class BlogsUpdateRequest extends FormRequest { public function rules(): array { return [ "id" => "nullable|numeric", "is_trending" => "required|boolean", "author" => "required|string", "author_image_url" => "required|string", "image_url_portrait" => "required|string", "image_url_landscape" => "required|string", "date" => "required|string", "title" => "required|string", "tags" => "required|string", "description" => "required|string", "content" => "required|string", ]; }
public function data(): array { $id = $this->input("id", null);
return \[
"id" => ($id === null) ? null : (int)$id, "is_trending" => $this->input("is_trending", 0), "author" => $this->input("author"), "author_image_url" => $this->input("author_image_url"), "image_url_portrait" => $this->input("image_url_portrait"), "image_url_landscape" => $this->input("image_url_landscape"), "date" => $this->input("date"), "url" => $this->generateUrl($this->input("title")), "title" => $this->input("title"), "tags" => array_map(function($row) { return trim($row); }, explode(",", $this->input("tags", []))), "description" => $this->input("description"), "content" => $this->input("content"), ]; }
private function generateUrl(string $title): string { $newUrl = trim(strtolower($title)); $newUrl = str_replace(" ", " ", $newUrl); $newUrl = str_replace(" ", "-", $newUrl); return preg_replace("/[^A-Za-z0-9\-]/", "", $newUrl); } } !<
Blogs model
>!<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class Blogs extends Model { protected $table = "blogs"; protected $fillable = [ "id", "is_trending", "author", "author_image_url", "image_url_portrait", "image_url_landscape", "title", "date", "description", "content", "created_at", "updated_at",
];
public function tags (){ return $this->hasMany(BlogTags::class); }
}!<
Images model
>!<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model; class Images extends Model { protected $table = "image"; protected $fillable = [ "id", "name", "location", "created_at", "updated_at", ]; }!<