dongye6377 2016-08-31 10:35
浏览 79
已采纳

Laravel - 删除一行(表)

I'm using laravel 5.2 and I'm studying the manipulation of datas with Eloquent. I have a table in which I need to delete a row with a button.

Problem : The server returns a route error (NotFoundHttpException)

Guideline : I watched this video https://www.youtube.com/watch?v=1EyoZhfZ2TY in order to inspire me for the datas.

Here is the Route (routes.php)

Route::delete('players&/{id}', 'PlayersController@deletePlayer');

Here is the template (players.blade.php) Pay attention of the last table data (td)

     <tbody>
    <?php
        foreach($players as $player) {
    ?>
    <tr>
        <td><?php echo $player->PLA_ID?></td>
        <td><?php echo $player->PLA_Name?></td>
        <td><?php echo $player->PLA_Surname?></td>
        <td><?php echo $player->PLA_Pseudo?></td>
        <td><span class="glyphicon glyphicon-pencil"></span></td>
        <td><a href="delete&<?php echo $player->PLA_ID?>"><span class="glyphicon glyphicon-trash"></span></a></td>
    </tr>
    <?php }
    ?>
    </tbody>

Finally the function (PlayersController.php)

<?php

namespace App\Http\Controllers;
use App\Models\Player;
use App\Http\Requests;
use Illuminate\Support\Facades\Input;

class PlayersController extends Controller
{
public function show(){ // reçoit l'url http://monsite.fr/users avec le      verbe "get" et qui retourne le formulaire.

    $players = player::all();
    return view('players', ['players' => $players]);
}

public function insertPlayer(){
    $player = new Player;

    $player->PLA_Name = Input::get('nom', false);
    $player->PLA_Surname = Input::get('prenom', false);
    $player->PLA_Pseudo = Input::get('pseudo', false);

    $player->save();

    return redirect('players');
}

public function deletePlayer($id){

    Player::destroy($id);
    return redirect('players');
}

}

I will search on my side too and thank you for taking time on my problem =)

P.S. I need the best practices with this framework, so if you have some ideas that allow me to code better, I could help some beginners like me =)

  • 写回答

2条回答 默认 最新

  • drgbpq5930 2016-09-01 00:53
    关注

    Change route to:

    Route::get('delete/{id}', 'PlayersController@deletePlayer');
    

    Change anchor to:

    <a href="{{ url('delete', $player->PLA_ID) }}"><span class="glyphicon glyphicon-trash"></span></a>
    

    Anchor tags always make GET requests. Which is why the route needs to be a GET route. Hope this helps.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?