Robin
Robin

Reputation: 1587

Soft delete with laravel returns Method [destroy] does not exist

I'm trying to soft delete a row from my database, but it doesn't work...

So what I want to do is after an authentication from a user, they can manage serveral things. Also they are be able to 'delete' stuff. But in fact it's a soft delete.

What I have for view (called index.blade.php):

<tbody>

                            @foreach($events as $event)
                            @if($event->expire <= time())
                            <tr class="danger">
                            @else
                            <tr>
                            @endif

                            <td>{{ $event->rally_name }}</td>

                            <td>{{ $event->short_info }}</td>

                            <td>
                                @if($event->is_podium == 'true')
                                <i class="fa fa-check" style="color:rgb(46, 204, 113);"></i>
                                @else
                                <i class="fa fa-close" style="color:rgb(192, 57, 43);"></i>
                                @endif
                            </td>

                            <td>
                                @if($event->is_radio == 'true')
                                <i class="fa fa-check" style="color:rgb(46, 204, 113);"></i>
                                @else
                                <i class="fa fa-close" style="color:rgb(192, 57, 43);"></i>
                                @endif
                            </td>

                            <td><a href="{{ $event->website }}" target="_blank">{{ $event->website }}</a></td>

                            <td>
                            @if($event->expire <= time())
                            <p>Verlopen.</p>
                            @else
                            {{ date('d/m/Y', $event->start_date) }} - {{ date('d/m/Y', $event->expire) }}
                            @endif
                            </td>

                            <td><a href="#" class="btn btn-sm yellow">Bewerken <i class="fa fa-edit"></i></a></td>

                            <td>
                            {{ Form::open(['method' => 'delete', 'route' => ['calendar.destroy', $event->id]]) }}
                                {{ Form::button('Verwijderen <i class="fa fa-trash-o"></i>', array('type' => 'submit', 'class' => 'btn btn-sm red')) }}
                            {{ Form::close() }}

                            </td>

                            </tr>
                            @endforeach

                            </tbody>

This view is stored in

app/views/admin/calendar/index.blade.php

It works, but the delete doesn't... My router is this:

Route::group(array('before' => 'auth'), function()
{
        Route::resource('admin', 'AdminController');
        Route::resource('admin/news/create', 'AdminNewsController');
        //events
        Route::resource('admin/calendar/index', 'AdminCalendarController');
        Route::resource('admin/calendar/create', 'AdminCalendarController');
});

I just think it is possible to call admin/calendar in my router? But I don't know if that's possible...

But okay...

My AdminCalendarController.php looks like this:

<?php

class AdminCalendarController extends \BaseController {

    /**
     * Display a listing of the resource.
     *
     * @return Response
     */
    public function index()
    {
        $select_events = DB::table('event')->orderBy('expire', 'DESC')->whereNull('deleted_at')->get();

        foreach ($select_events as $events) {

            $events = array();
        }


        return View::make('admin.calendar.index')->with('events', $select_events);
    }


    /**
     * Show the form for creating a new resource.
     *
     * @return Response
     */
    public function create()
    {
        return View::make('admin.calendar.create');
    }


    /**
     * Store a newly created resource in storage.
     *
     * @return Response
     */
    public function store()
    {
        //
    }


    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return Response
     */
    public function show($id)
    {
        //
    }


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


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


    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return Response
     */
    public function destroy($id)
    {
        // delete
        $calendar = Calendar::find($id);
        $calendar->delete();

        // redirect
        Session::flash('message', 'Successfully deleted the thing!');
        return Redirect::to(Config::get('app.url').'/admin/calendar/index');
    }


}

And at last, the Calendar.php:

<?php

use Illuminate\Database\Eloquent\SoftDeletingTrait;

class Calendar extends Eloquent {

    protected $table = 'event';
    public $timestamps = false;

    use SoftDeletingTrait;

    protected $dates = ['deleted_at'];

}

My database looks like this:

database model

When I try to execute the 'delete', the only thing it does it the following:

error

Also, the url does remove the admin part.

Thanks for the help!

Kindest regards,

Upvotes: 0

Views: 1974

Answers (2)

Robin
Robin

Reputation: 1587

I found the problem, it was the URL, after viewing the source, I saw there was no admin inside the URL, so now it's fixed. :D

Upvotes: 1

Laurence
Laurence

Reputation: 60048

Your router looks very wrong to me.

It should be something like this:

Route::group(array('before' => 'auth', 'prefix' => 'admin'), function()
{
        Route::resource('news', 'AdminNewsController');
        Route::resource('calendar', 'AdminCalendarController');
});

Upvotes: 1

Related Questions