ifeanyicode's avatar

Pleas help I'm finding it difficult to display my image uploaded in the database to from

every other item im fetching is showing on the front end apart from image _path

Here is my controller ''' if ($request->hasfile('files')) { foreach ($request->file('files') as $file) { $name = $file->getClientOriginalName(); $file->move(public_path() . '/storage/postImages', $name); $imgData[] = $name; }

        $post = new Post();
        $post->title = $title;
        $post->user_id = auth()->id(); // add this line
        $post->body = $body;
        $post->location = $location;
        $post->category = $category;
        $post->name = json_encode($imgData); //Don't use json
        $post->image_path = json_encode($imgData); // Don't use json



        $post->save();

'''

0 likes
5 replies
ifeanyicode's avatar

''' my blade template

                    <div class="post__content">

                        <div class="post__medias">

                            <img class="post__media" src="{{ asset($post->image_path)}}" alt="images" /></a>


                        </div>
                    </div>

                    <div class="post__footer">
                        <div class="post__buttons">
                            <button class="post__button">
                                <div class="loveicon">
                                    <span id="icon"> <i class="fa-regular fa-heart"></i></span>

'''

ifeanyicode's avatar

''I'm calling the image from storage using

                        <img class="post__media" src="{{ asset($post->image_path)}}" alt="images" /></a>
Snapey's avatar

why are you json encoding file information?

Snapey's avatar
Snapey
Best Answer
Level 122

if you want multiple images, I would suggest a related images table rather than encoding multiple file paths into the one field.

Don't use the filename provided by the user.

Differentiate the images by user or post. At the moment you would not be able to have two posts with the same image name.

ifeanyicode's avatar

''' @Snapey please help im lost here here is my entire controller and I would have to remove the image name and allow unique ids for each image, how can I add the image table

''' <?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use Illuminate\Support\Facades\Auth; use App\Models\Post;

class CreateController extends Controller {

public function index()

{
    $posts = Post::all();
    return view('create.post', compact('posts'));
}
public function __construct()
{
    $this->middleware('auth');
}

public function store(Request $request)

{
    $request->validate([
        'title' => 'required',
        'body' => 'required|string|min:15|max:100',
        'location' => 'required',
        'category' => 'required',
        'files' => 'required', // checks length of array
        'files.*' => 'mimes:jpeg,jpg,png,gif,csv,txt,WebP,|max:10048'



    ]);

    $title = $request->input('title');
    $user_id = Auth::user()->id;
    $body = $request->input('body');
    $location = $request->input('location');
    $category = $request->input('category');


    if ($request->hasfile('files')) {
        foreach ($request->file('files') as $file) {
            $name = $file->getClientOriginalName();
            $file->move(public_path() . '/storage/postImages', $name);
            $imgData[] = $name;
        }



        $post = new Post();
        $post->title = $title;
        $post->user_id = auth()->id(); // add this line
        $post->body = $body;
        $post->location = $location;
        $post->category = $category;
        $post->name = json_encode($imgData); //Don't use json
        $post->image_path = json_encode($imgData); // Don't use json



        $post->save();


        return back()->with('success', ' Post created successfully! ');
    }
}

}

'''

Please or to participate in this conversation.