semicolon24's avatar

How do I add an update controller for image uploads?

I'm making crud with ckeditor and uploading images, I'm having trouble with the controller update part How do I add an update controller for image uploads? on my controller

this my Controller

Create & Store

   public function create()
{
    return view('mypost.create', [
        'categories' => Category::all()
    ]);
       }


       public function store(Request $request)
{
    
    $request->validate([
        'title' => 'required',
        'slug' => 'required|unique:posts',
        'category_id' => 'required',
        'image' => 'image|file|mimes:jpg,png',
        'body' => 'required',
    ]);

    $input = $request->all();

    if ($request->hasFile('image')) {
        $image = $request->file('image');
        $imageName = date('d-m-Y') . '.' . $image->getClientOriginalName();
        $path = public_path('/uploads/post');
        $image->move($path, $imageName);
        $input['image'] = $imageName;
    }

    $input['user_id'] = auth()->user()->id;
    $input['excerpt'] = Str::limit(strip_tags($request->body));
    $input['body'] = $request->body;

    Post::create($input);

    return redirect('/mypost')->with('success', 'Post success save!!');
       }

Edit

   public function edit(Post $post, $id)
{
    $post = Post::findorfail($id);
    return view('mypost.edit', [
        'post' => $post,
        'categories' => Category::all()
    ])->with('post', $post);
       }

Update

       public function update(Request $request, $id)
{
    $post = Post::find($id);

    $input['title'] = ($request->title);
    $input['slug'] = ($request->slug);
    $input['category_id'] = ($request->category_id);
    $input['user_id'] = auth()->user()->id;
    $input['excerpt'] = Str::limit(strip_tags($request->body));
    $input['body'] = ($request->body);

    $post->update($input);

    return redirect('/mypost')->with('success', 'Post Sucess updated!!');
       }
0 likes
9 replies
DhPandya's avatar

@semicolon24 Will you please elaborate more about your question?

How do I add an update controller for image uploads? on my controller

semicolon24's avatar

@DhPandya

How do I add a post image to my update controller using the CRUD pattern in my controller above.? can you help me..?

semicolon24's avatar

@DhPandya

when I added this it wasn't updated

 if ($request->hasFile('image')) {
    $image = $request->file('image');
    $imageName = date('d-m-Y') . '.' . $image->getClientOriginalName();
    $path = public_path('/uploads/post');
    $image->move($path, $imageName);
    $input['image'] = $imageName;
     }
DhPandya's avatar

@semicolon24 First dd() your $request parameters and check whether you're getting uploaded files or not.

semicolon24's avatar

@DhPandya

I have already done "dd($request->all()); " but the data still doesn't enter either the code or the database

Snapey's avatar

you will need to check the docs, but I think the image upload has to use its own endpoint which returns a unique id for the image and a url for the image so that the editor can display the image just uploaded

semicolon24's avatar

@Snapey

even though when I dd() it was displayed, when I clicked the update button it turned out it wasn't updated

PandaxWth's avatar

First of all check that your edit form has

enctype="multipart/form-data"

Then use validation in both your store and update functions (check also form requests in the documentation).

And in your code DO NOT use $request->all(). Always use $request->validated() after validation, so that malicious fields won't be passed to the model. (again check the documentation). ;)

Lastly I would use model binding for your edit and update functions (I repeat: check the documentation).

EDIT: I almost forgot...if you pass empty $input['image'] you can erase the image field, so if the user doesn't pass a new image, delete the key 'image' in $input.

unset($input['image'])

Please or to participate in this conversation.