0

I wrote a controller in Laravel 4.2, to delete an image from the database. Every method is working so far, except for the destroy method. When I try to call the URI via cURL, I get a Laravel error page's HTML in my Terminal on Mac OS X.

<?php

class ImagesController extends \BaseController
{

    /**
     * Display a listing of the resource.
     * GET /images
     *
     * @return Response
     */
    public function index()
    {
        $images = Image::where('image_id', '>', 0)->get();

        return Response::json(array(
            'error' => false,
            'images' => $images->toArray()
        ), 200);
    }

    /**
     * Show the form for creating a new resource.
     * GET /images/create
     *
     * @return Response
     */
    public function create()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     * POST /images
     *
     * @return Response
     */
    public function store()
    {
        $image = new Image();
        $image->image_id = Request::get('id');
        $image->title = "Testtesttest";
        $image->alt = "Test";
        $image->filename = "Test";
        $image->filepath = "~/jbehrens";
        $image->width = 100;
        $image->height = 100;
        $image->created_at = "2014-07-28 11:00:00";
        $image->updated_at = "2014-07-29 13:00:00";
        $image->save();

        return Response::json(array(
                'error' => false,
                'images' => $image->toArray()),
            200
        );
    }

    /**
     * Display the specified resource.
     * GET /images/{id}
     *
     * @param  int $id
     * @return Response
     */
    public function show($id)
    {
        $image = Image::where('image_id', $id)
            ->take(1)
            ->get();


        return Response::json(array(
            'error' => false,
            'image' => $image->toArray()
        ), 200);
    }

    /**
     * Show the form for editing the specified resource.
     * GET /images/{id}/edit
     *
     * @param  int $id
     * @return Response
     */
    public function edit($id)
    {
        //

    }

    /**
     * Update the specified resource in storage.
     * PUT /images/{id}
     *
     * @param  int $id
     * @return Response
     */
    public function update($id)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     * DELETE /images/{id}
     *
     * @param  int $id
     * @return Response
     */
    public function destroy($id)
    {
        $image = Image::where('media_id', '>', 0)->find($id);
        $image->delete();

        return Response::json(array(
                'error' => false,
                'message' => 'url deleted'),
            200
        );
    }

}

The command I am issuing is the following curl -i -X DELETE http://link/portfolio/public/images/3 (example given).

user3710669
  • 541
  • 1
  • 4
  • 4

2 Answers2

2

You cant actually use DELETE. You need to POST, with a hidden field set to delete. That is because delete and put are not actually supported by web browsers, only get and post.

This post explains it with more detail: How does Laravel handle PUT requests from browsers?

Community
  • 1
  • 1
Laurence
  • 58,936
  • 21
  • 171
  • 212
  • I'm using `DELETE` successfully. It's also described at http://laravel.com/docs/controllers#resource-controllers. Or do you mean something else? – lowerends Jul 29 '14 at 15:30
  • Calling `delete` via CURL is not actually supported. You need to read the link I posted. You dont *actually* use the `delete` verb, you `post` to the page, and Laravel detects that you meant to use delete because of a hidden field – Laurence Jul 29 '14 at 15:31
  • Hmm, looking at the Chrome developer tools, this is printed: `Request Method:DELETE`. That looks like the `DELETE` verb is being used. Also, my routes are defined as `Route::delete(...)`. Am I missing something? – lowerends Jul 29 '14 at 15:36
  • See this as well - http://stackoverflow.com/questions/165779/are-the-put-delete-head-etc-methods-available-in-most-web-browsers – Laurence Jul 29 '14 at 15:39
  • Yes, but I'm not even using `Form::open()`. This may be off-topic, but how come Chrome then says the request method is `DELETE`? – lowerends Jul 29 '14 at 15:44
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/58264/discussion-between-lowerends-and-the-shift-exchange). – lowerends Jul 29 '14 at 15:46
0

Replace this:

$image = Image::where('media_id', '>', 0)->find($id);

with this:

$image = Image::find($id);

in yourdestroy function.

lowerends
  • 5,469
  • 2
  • 24
  • 36