繁体   English   中英

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

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

我正在尝试删除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

伙计们,如果你在其他地方看过这个问题,我很抱歉,但我真的需要帮助,而且我找不到任何有用的东西。

谢谢。

使用该行

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

如你所写,你有一个无效的资源路线Route::resource('', 'TodosController'); 这应该解释你的404错误。 尝试类似的东西:

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM