简体   繁体   English

Laravel路由传递变量到控制器不一样

[英]Laravel route pass variable to controller not same

I found a different error. 我发现了另一个错误。 I tried to delete book by id. 我试图按ID删除图书。 i have data for example 我有数据

--------------
| id |  name |
--------------
| 1  | book1 |
| 2  | book2 |
| 3  | book3 |

so, when I will delete books with id 2, then the controller always receives id 3. and when i tries to delete id 1, that remains the case, the controller gain id 3 (always the last record) 因此,当我删除I​​D为2的书时,控制器始终会收到ID3。当我尝试删除ID为1时,情况仍然如此,控制器会获得ID 3(总是最后一条记录)

this view blade 此视图刀片

@foreach($datas as $data)
<a href="# {{ $data->id }}"
   onclick="event.preventDefault();
   document.getElementById('remove-form').submit();"
  rel="tooltip" title="Hapus" class="btn btn-danger">
</a>

<form id="remove-form" action="{{ url('/dashboard/book/delete/'. $data->id) }}" method="post">
    {{ csrf_field() }}
    {{ method_field('DELETE') }}
</form>
@endforeach

this route 这条路线

Route::group(['namespace' => 'Backend'], function ()
{
   Route::resource('/dashboard/book', 'BookController'); //
   Route::delete('/dashboard/book/delete/{id}', 'BookController@destroy');
}

this controller 这个控制器

public function index()
    {
        $datas = Book::all();
        return view('backend.bookview', compact('datas'));
    }
public function destroy($id)
{
    Book::where('id', $id)->delete();
    return redirect('/dashboard/book')->with('ok', translate('back/book.destroyed'));
}

UPDATE UPDATE

You can't have multiple id remove-form on a single html page. 单个html页面上不能有多个id remove-form Instead use classes like this: 而是使用这样的类:

@foreach($datas as $data)
    <div class="delete-block">
        <a href="# {{ $data->id }}"
          rel="tooltip" title="Hapus" class="btn btn-danger delete-btn">
        </a>

        <form class="remove-form" action="{{ url('/dashboard/book/delete/'. $data->id) }}" method="post">
            {{ csrf_field() }}
            {{ method_field('DELETE') }}
        </form>
    </div>
@endforeach

and your script should go as: 并且您的脚本应为:

$(function() {

    $('.delete-btn').on('click', function(e) {
        $(this).closest('.delete-block').find('form').submit();
    });

});

 $(function() { $('.delete-btn').on('click', function(e) { e.preventDefault(); console.log($(this).closest('.delete-block').find('form').attr('action')); // $(this).closest('.delete-block').find('form').submit(); }); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="delete-block"> <a href="#1" rel="tooltip" title="Hapus" class="btn btn-danger delete-btn"> Delete 1 </a> <form id="remove-form" action="/dashboard/book/delete/1" method="post"> <input type="hidden"> </form> </div> <div class="delete-block"> <a href="#2" rel="tooltip" title="Hapus" class="btn btn-danger delete-btn"> Delete 2 </a> <form id="remove-form" action="/dashboard/book/delete/2" method="post"> <input type="hidden"> </form> </div> <div class="delete-block"> <a href="#3" rel="tooltip" title="Hapus" class="btn btn-danger delete-btn"> Delete 3 </a> <form id="remove-form" action="/dashboard/book/delete/3" method="post"> <input type="hidden"> </form> </div> 

Inside Controller 内部控制器

You should delete the book by fetching the single record by id from database, like this: 您应该通过从数据库中按ID提取单个记录来删除图书,如下所示:

public function destroy($id)
{
  $book = Book::find($id);
  if($book) {
    $book->delete();
    return redirect('/dashboard/book')->with('ok', translate('back/book.destroyed'));
  }
  // return error response - book deletion failed!
  return redirect('/dashboard/book')->with('Error', translate('back/book.destroyed.error'));
}

Use find() to fetch single record via primary key , from database. 使用find()通过primary key从数据库中获取单个记录。

However, if you know the primary key of the model, you may delete the model without retrieving it. 但是,如果您知道模型的主键,则可以删除模型而不检索它。 To do so, call the destroy method: 为此,请调用destroy方法:

Book::destroy(1); // Can pass single primary key
Book::destroy([1, 2, 3]); // Can pass an array of primary keys
Book::destroy(1, 2, 3); // Can pass multiple primary keys via arguments

See more about Deleting Models in Laravel 查看有关在Laravel中删除模型的更多信息

Hope this helps! 希望这可以帮助!

This is happening because document.getElementById('remove-form').submit() always selects the last form which has id => 3 . 发生这种情况是因为document.getElementById('remove-form').submit()总是选择id => 3的最后一个表单。

So change it to following code and it will work: 因此,将其更改为以下代码即可使用:

@foreach($datas as $data)
    <form method="POST" action="{{ url('/dashboard/book/delete/'. $data->id) }}"  style="display: inline-block;">
        {{ csrf_field() }}
        {{ method_field('DELETE') }}
        <button onclick="return confirm('Do you really want to delete this item?');" type="submit" class="btn btn-danger" data-original-title="Delete Item" data-toggle="tooltip" data-placement="top" title="">
            Delete
        </button>
    </form>
@endforeach

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

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