从laravel中的数据库中删除特定记录

时间:2015-12-29 13:03:29

标签: php laravel-5 laravel-5.1

大家好我想在我的代码中创建一个简单的删除功能,这是我的控制器:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use Input;
use Auth;
use App\User;
use App\Product;
use DB;

class listController extends Controller
{
    //
    public function getIndex(){

        $list=DB::table('wishlist')->get(['id','product_id','user_id']);
        $product=Product::get(['name','id','salary','image_name']);
        $user=DB::table('users')->get(['id']);
        return view('contents.wishlist')->with('list',$list)
                                        ->with('product',$product)
                                        ->with('user',$user);
    }
    public function postIndex(Request $request){
        $id=$request->input('id');
        $user_id=Auth::user()->id;
        $product_id=Input::get('product_id');


        DB::table('wishlist')->insert(['id'=>$id,'user_id'=>$user_id,'product_id'=>$product_id]);
        return redirect()->action('listController@postCreate');
    }
    public function deleteProduct(Request $request,$id){
        $pro=$request->get('id');
        DB::table('wishlist')->where('id',$pro)->delete();
        return redirect('wishlist/'.$pro);
    }
}

我的观点是这样的:

@extends('master')
@section('content')

<div class="wrapper-breadcrums">
    <div class="container">
        <div class="row">
            <div class="col-sm-24">
                <div class="breadcrumbs">
                    <ul>
                        <li class="home"> <a href="http://localhost/shopsite/public/home" title="Go to Home Page"><span >Home</span></a> <span class="separator">/ </span>
                        </li>
                        <li class="cms_page"> <strong>My Account</strong>
                        </li>
                    </ul>
                </div>
            </div>
        </div>
    </div>
</div><!-- /.wrapper-breadcrums -->

<div class="em-wrapper-main">
    <div class="container container-main">
        <div class="em-inner-main">
            <div class="em-wrapper-area02"></div>
            <div class="em-main-container em-col2-left-layout">
                <div class="row">
                    <div class="col-sm-18 col-sm-push-6 em-col-main">
                        <div class="em-wrapper-area03"></div>
                        <div class="my-account">
                            <div class="my-wishlist">
                                <div class="page-title title-buttons">
                                    <h1>My Wishlist</h1>
                                </div>
                                    <fieldset>

                                        <table class="data-table" id="wishlist-table">
                                            <thead>
                                                <tr>
                                                    <th></th>
                                                    <th>Product Details and Comment</th>
                                                    <th>Add to Cart</th>
                                                    <th></th>
                                                </tr>
                                            </thead>
                                            <tbody>
                                                @foreach($product as $p)
                                                @foreach($list as $l)
                                                @if($p->id == $l->product_id)
                                                @if($l->user_id == Auth::user()->id)
                                                <tr id="item_6">
                                                    <td>
                                                        <a class="product-image" href="product_detail" title=" {{$p->name}} "> <img src="{{asset('images/')}}/{{$p->image_name}}" width="113" height="113" alt=" {{$p->name}} " /> </a>
                                                    </td>
                                                    <td>

                                                        <h3 class="product-name"><a href="product_detail" title=" {{$p->name}} "> {{$p->name}} </a></h3>

                                                        <div class="description std">
                                                            <div class="inner">The must-have tee for his third birthday!</div>
                                                        </div>
                                                        <textarea name="description[6]" rows="3" cols="5" title="Comment" placeholder="Please, enter your comments..."></textarea>
                                                    </td>
                                                    <td>
                                                        <div class="cart-cell">

                                                            <div class="price-box" itemscope itemtype="http://schema.org/Product"> <span class="regular-price" id="product-price-258"> <span class="price"  content="{{$p->salary}}">${{$p->salary}}</span> </span>
                                                            </div>

                                                            <div class="add-to-cart-alt">
                                                                <input type="text" class="input-text qty validate-not-negative-number" name="qty[6]" value="1" />
                                                                <button type="button" title="Add to Cart" onclick="addWItemToCart(6);" class="button btn-cart"><span><span>Add to Cart</span></span>
                                                                </button>
                                                            </div>
                                                            <p><a class="link-edit" href="#">Edit</a>
                                                            </p>
                                                        </div>
                                                    </td>
                                                    <td>
                                                    <form method="DELETE" action="{{action('listController@deleteProduct','id='.$l->id)}}" enctype="multipart/form-code">
                                                    {!! csrf_field() !!}
                                                    <input type="hidden" name="id" value="{{$l->id}}">
                                                    <button type="submit" class="btn btn-danger">Remove item</button>
                                                    </form>
                                                    </td>
                                                </tr>
                                                @endif
                                                @endif
                                                @endforeach
                                                @endforeach
                                            </tbody>
                                        </table>
                                        <div class="buttons-set buttons-set2">
                                            <button type="submit" name="save_and_share" title="Share Wishlist" class="button btn-share"><span><span>Share Wishlist</span></span>
                                            </button>
                                            <button type="button" title="Add All to Cart" class="button btn-add"><span><span>Add All to Cart</span></span>
                                            </button>
                                            <button type="submit" name="do" title="Update Wishlist" class="button btn-update"><span><span>Update Wishlist</span></span>
                                            </button>
                                        </div>
                                    </fieldset>
                                </form>
                            </div>
                            <div class="buttons-set">
                                <p class="back-link"><a href="home"><small>&laquo; </small>Back</a>
                                </p>
                            </div>
                        </div>
                    </div><!-- /.em-col-main -->
                    <div class="col-sm-6 col-sm-pull-18 em-col-left em-sidebar">
                        <div class="em-wrapper-area02"></div>
                        <div class="em-line-01 block block-account">
                            <div class="block-title em-block-title"> <strong><span>My Account</span></strong>
                            </div>
                            <div class="block-content">
                                <ul>
                                    <li><a href="#">Account Dashboard</a>
                                    </li>
                                    <li><a href="#it/">Account Information</a>
                                    </li>
                                    <li><a href="#">Address Book</a>
                                    </li>
                                    <li><a href="#">My Orders</a>
                                    </li>
                                    <li><a href="#">Billing Agreements</a>
                                    </li>
                                    <li><a href="#">Recurring Profiles</a>
                                    </li>
                                    <li><a href="#">My Product Reviews</a>
                                    </li>
                                    <li><a href="#">My Tags</a>
                                    </li>
                                    <li class="current"><strong>My Wishlist</strong>
                                    </li>
                                    <li><a href="#">My Applications</a>
                                    </li>
                                    <li><a href="#">Newsletter Subscriptions</a>
                                    </li>
                                    <li class="last"><a href="#">My Downloadable Products</a>
                                    </li>
                                </ul>
                            </div>
                        </div><!-- /.em-line-01 -->

                    </div><!-- /.em-sidebar -->
                </div>
            </div>
        </div>
    </div>
</div><!-- /.em-wrapper-main -->


@stop

这是我的路线:

<?php

/*
|--------------------------------------------------------------------------
| Application Routes
|--------------------------------------------------------------------------
|
| Here is where you can register all of the routes for an application.
| It's a breeze. Simply tell Laravel the URIs it should respond to
| and give it the controller to call when that URI is requested.
|
*/
Route::get('/home',function(){
    if (Auth::guest()) {
        return Redirect::to('auth/login');
    }else{
        $slide=DB::table('slideshow')->get();
        $category=DB::table('categories')->get(['name','id']);
        $sub=DB::table('subcategory')->get(['id','name','category_id']);
        $in=DB::table('incategory')->get(['name','id','sub_id']);
        $product=DB::table('products')->get();
        $product=DB::table('products')->get(['id','name','sale','p_salary','n_salary','salary','image_name','new','best']);
        $product=DB::table('products')->get(['id','name','sale','p_salary','n_salary','salary','image_name','new','best']);

        return view('home')->with('slide',$slide)
                            ->with('category',$category)
                            ->with('sub',$sub)
                            ->with('in',$in)
                            ->with('product',$product)
                            ->with('product',$product)
                            ->with('product',$product);
    }

});
Route::controllers(["wishlist"=>"listController"]);
Route::controllers(["product_detail"=>"productdetailController"]);
Route::controllers(["products"=>"productController"]);
Route::controllers(["slider"=>"slideController"]);
Route::controllers(["left"=>"leftController"]);
Route::controllers(["newarrivals"=>"newarrivalsController"]);
Route::controllers(["bestandsale"=>"bestandsaleController"]);


// Authentication routes...
Route::get('auth/login', 'Auth\AuthController@getLogin');
Route::post('auth/login', 'Auth\AuthController@postLogin');
Route::get('auth/logout', 'Auth\AuthController@getLogout');

// Registration routes...
Route::get('auth/register', 'Auth\AuthController@getRegister');
Route::post('auth/register', 'Auth\AuthController@postRegister');

我的代码存在问题,它会重新生成视图并正确传递ID,但它没有删除数据库中所需的行,任何人都可以帮忙吗?

1 个答案:

答案 0 :(得分:2)

您需要传递隐藏的<cms_index_index> <reference name="head"> <action method="addJs"><script>js_name</script></action> </reference> </cms_index_index> 输入以删除产品,并且我还假设您调用_method的路线类似

deleteMethod

Implicit Routing documentation 5.1

更新

另外,请尝试避免使用隐式路由,因为它在5.2版中已弃用。