如何从Laravel列表中删除待办事项列表项?

时间:2019-06-17 15:13:55

标签: php laravel laravel-form

我正在尝试删除laravel中的项目。这是我的代码:

这是我的路线:

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

这是我的控制者:

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');
    }
}

这是我的表单模板:

<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>

我希望它将删除该项目并重定向到/,但实际上它重定向到/$id并抛出404 not found error

伙计们,很抱歉,如果您在其他地方看到过此问题,但我确实需要帮助,但没有发现任何有用的信息。

谢谢。

2 个答案:

答案 0 :(得分:0)

使用线路

使用App \ Http \ Controllers \ Controller;

并用于删除

在刀片中

            <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>

答案 1 :(得分:0)

按照书面规定,您的资源路由Route::resource('', 'TodosController');无效,应该可以解释404错误。尝试类似的东西:

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

,然后使用HTTP GET重新测试/ todo,以查看您的TodosController@index()方法是否按预期工作。