Search code examples
phplaravellaravel-form

How can I delete a to do list item from the list in laravel?


I'm trying to delete an item in laravel. This is my code:

This is my route:

Route::resource('', 'TodosController');

This is my controller:

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Todo;

class TodosController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $todos = Todo::all();
        return view('pages.home')->with('todos', $todos);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {

        return view('pages.home');
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $this->validate($request, [
            'todo' => 'required'
        ]);


        // Create todo
        $todo = new Todo;
        $todo->todo = $request->input('todo');
        $todo->save();
        return redirect('/')->with('success', 'Todo created');
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        $todo = Todo::find($id);
        return view('pages.todo')->with('todo', $todo);
    }

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

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        $todo = Todo::find($id);

        return redirect('/')->with('success', 'Todo edited');

    }

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

        return redirect('/')->with('success', 'Todo deleted');
    }
}

This is my template with forms:

<li class="list-group-item mb-5">

    {{-- Edit button --}}
    {!! Form::open(['action' => ['TodosController@update', $todo->id], 'method' => 'POST', 'class' => 'pull-right']) !!}
        {{ Form::hidden('_method', 'PUT') }}
        {{ Form::submit('Edit', ['class' => 'btn btn-primary m-auto', 'style' => 'display:block;']) }}
    {!! Form::close() !!}


    <p style="text-align:center;text-transform:uppercase;font-weight:700;font-size:2rem;" class="m-0 mt-5 mb-5">
        <a href="{{ url('/'.$todo->id) }}">{{ $todo->todo }}</a>
    </p>

    {{-- Delete button --}}
    {!! Form::open(['action' => ['TodosController@destroy', $todo->id], 'method' => 'POST', 'class' => 'form-group']) !!}
        {{ Form::hidden('_method', 'DELETE') }}
        {{ Form::submit('&times;', ['class' => 'btn btn-danger m-auto', 'style' => 'display:block;']) }}
    {!! Form::close() !!}


</li>

I expect that it would delete the item and redirect to /, but actually it redirects to /$id and throws a 404 not found error.

Guys, I'm sorry if you've seen this question somewhere else, but I really need help and I didn't find anything useful.

Thank You.


Solution

  • Use the line

    use App\Http\Controllers\Controller;

    and use for delete

    in blade

                <td>
    
                    {!! Form::open(array('route' => array('products.destroy', $product->id), 'method' => 'delete')) !!}
    
                       <button type="submit" class="btn btn-danger" onclick="return confirm('Are you sure?')"><i class="fa fa-trash-o"></i></button>
    
                       {!! Form::close() !!}
               </td>