【问题标题】:After click approve button, automatically delete the old data点击批准按钮后,自动删除旧数据
【发布时间】:2017-05-16 14:09:15
【问题描述】:

我使用 Laravel 创建了一个会议室预订系统。所以,我有 2 张桌子,这是 Auth::guest 预订房间的预订,以及 Auth::user 批准预订房间的活动。

如果 auth::user 为真,我有一个视图列出来宾 (/books) 进行的所有预订,在视图中,有一个批准按钮,它将带您进入另一个视图,即 (events /create) 与 (/books/create) 视图具有相同的形式。

当你点击批准按钮时,预订的所有数据都会出现在(events/create)中的表格中,因为我有预订数据的$id,所以我可以做Book::findOrfail($id ),当点击应用提交按钮时,数据将被保存到事件表中。

那么,我的问题是如何在我点击(事件/创建)视图上的提交按钮后自动从书表中删除预订数据?

有什么想法吗?

在事件控制器上创建事件

 public function create_by_booking($id)
{
    $event = Book::findOrFail($id);
    $event->start_time =  $this->change_date_format_fullcalendar($event->start_time);
    $event->end_time =  $this->change_date_format_fullcalendar($event->end_time);

    $data = [
        'page_title' => 'Add new event',
        'event'         => $event,
        'count'  => Book::count(),
    ];

    return view('event/create', $data);
}

从书籍控制器创建书籍

 public function create()
{
    $data = [
        'page_title' => 'Book Meeting Room',
        'count'  => Book::count(),
    ];

    return view('book/create', $data);
}

查看列表/书籍

<table class="table table-striped">
                <thead>
                <tr>
                    <th>#</th>
                    <th>Booking's Title</th>
                    <th>Email</th>
                    <th>Room</th>
                    <th>Start</th>
                    <th>End</th>
                    <th></th>
                </tr>
                </thead>
                <tbody>
                <?php $i = 1;?>
                @foreach($books as $book)
                    <tr>
                        <th scope="row">{{ $i++ }}</th>
                        <td><a href="{{ url('books/' . $book->id) }}">{{ $book->title }}</a> <small>by {{ $book->name }}</small></td>
                        <td>{{ $book->email }}</td>
                        <td>{{ $book->room }}</td>
                        <td>{{ date("g:ia\, jS M Y", strtotime($book->start_time)) }}</td>
                        <td>{{date("g:ia\, jS M Y", strtotime($book->end_time)) }}</td>
                        @if (Auth::guest())

                        @else
                        <td>
                            <a class="btn btn-primary btn-xs" href="{{ url('/events/create', $book->id) }}">
                                <span class="glyphicon glyphicon-plus"></span> Add</a>
                            <form action="{{ url('books/' . $book->id) }}" style="display:inline" method="POST">
                                <input type="hidden" name="_method" value="DELETE" />
                                {{ csrf_field() }}
                                <button class="btn btn-danger btn-xs" type="submit"><span class="glyphicon glyphicon-trash"></span></button>
                            </form>

                        </td>
                        @endif
                    </tr>
                @endforeach
                </tbody>
            </table>

谢谢。

【问题讨论】:

  • 到目前为止,您是如何处理事件/创建视图的? - 我的意思是,控制器..
  • 分享你的代码
  • 已更新代码。
  • 只传递一个记录的ID来创建事件,然后用这个ID删除它

标签: laravel


【解决方案1】:

您已经为您的视图传递了$event 变量,该变量具有您搜索的图书的对象,所以我认为您的$event 现在有一个您可以访问的id 属性。

在您的事件/创建视图中,您可以编辑到 store 方法的路由以接受 id,如下所示:

<form action="{{ url('/events/store', $data['event']->id) }}" method="POST">
 {{ csrf_field() }}
<button class="btn btn-danger btn-xs" type="submit"></button>
</form>

由于您没有发布您的 store() 方法,我假设它看起来像这样:

public function store($id) {  //of course edit your route to accept this variable

// here is where you process your events as you want.

$book = Book::find($id); //here is where you just find the book and delete it as you wanted.
$book->delete();

return redirect()->back();

}

【讨论】:

    【解决方案2】:

    我假设您没有向 Laravel 中的技术人员询问如何删除一行..

    因此,一旦函数(事件/创建)成功,您应该调用该函数(例如 delete_booking($id))。

    但我的建议是,您不需要删除预订数据,而是在该行设置一个标志,表明预订已经进入下一步。

    如果您正在考虑节省数据库大小,可以像每周一次那样批量删除,以清理预订表。恕我直言,它比自动一个一个地做更有效。

    【讨论】:

    • 我想做 Book::find($id)->delete();商店功能成功后。如何将 $id 传递给存储函数,以便我可以执行 Book::find($id)->delete(); ?
    猜你喜欢
    • 1970-01-01
    • 2016-01-04
    • 2015-01-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多