【问题标题】:How can I delete a to do list item from the list in laravel?如何从 laravel 的列表中删除待办事项列表项?
【发布时间】:2019-10-31 05:11:56
【问题描述】:

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

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

谢谢。

【问题讨论】:

  • 你的 laravel 日志文件是怎么说的? APP_DEBUG=true 在你的 .env 中设置了吗?如果没有,请设置它并使用您发现的任何错误更新您的问题。

标签: php laravel laravel-form


【解决方案1】:

使用线

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

【讨论】:

    【解决方案2】:

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

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

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

    【讨论】:

      猜你喜欢
      • 2020-06-06
      • 1970-01-01
      • 1970-01-01
      • 2021-07-03
      • 2019-05-05
      • 2018-03-26
      • 2021-06-09
      • 2018-06-02
      • 1970-01-01
      相关资源
      最近更新 更多