如何在Laravel 5.6中自动增加购物车ID?

时间:2018-03-25 06:12:08

标签: php laravel cart shopping-cart laravel-5.6

在Laravel 5.6中,我安装了购物车。我想将数据作为会话,然后放入数据库。我也做了一些事情,但我无法获得id自动增量。它投入了类型。当我输入id然后它的工作,但它是令用户尴尬的,而不是用户友好的。我想让它用户友好

这是我的代码:

add.blade.php

<form action="{{route('add_to_cart')}}" method="post">
    @csrf               
    <input type="text" name="hiddenId" value=""><br>
    Name <br>
    <input type="text" name="txtName"><br>
    Qty <br>
    <input type="text" name="txtQty"><br>
    Price <br>
    <input type="text" name="txtPrice">
    <input type="submit" value=" + ">
</form>

<hr>
<table border="2px">
    <tr>
        <td>SL</td>
        <td>Name</td>
        <td>Qty</td>
        <td>Unit Price</td>
        <td>Total</td>
        <td>Action</td>
    </tr>
    @foreach(Cart::content() as $row)
    <tr>
        <td>{{$row->id}}</td>
        <td>{{$row->name}}</td>
        <td>
            <form action="{{url('update')}}" method="post">
                {{csrf_field()}}
                <input type="text" name="uqty" value="{{$row->qty}}">
                <input type="hidden" name="rowId" value="{{$row->rowId}}">
                <input type="submit" value="Update">
            </form>
        </td>
        <td>{{$row->price}}</td>
        <td>{{$row->subtotal}}</td>
        <td>
            <a href="{{url('deletecart/'.$row->rowId)}}">delete</a>
        </td>
    </tr>
    @endforeach
    <tfoot style="text-align: right;">
            <tr>
                <td colspan="2">&nbsp;</td>
                <td>Subtotal</td>
                <td><?php echo Cart::subtotal(); ?></td>
            </tr>
            <tr>
                <td colspan="2">&nbsp;</td>
                <td>Tax</td>
                <td><?php echo Cart::tax(); ?></td>
            </tr>
            <tr>
                <td colspan="2">&nbsp;</td>
                <td>Total</td>
                <td><?php echo Cart::total(); ?></td>
            </tr>
        </tfoot>
</table>

CartController.php

namespace App\Http\Controllers;
use Illuminate\Http\Request;
use DB;
use Cart;
use App\CartItem;
use Illuminate\Support\Facades\Auth;
use App\Http\Requests;
use App\Http\Controllers\Controller;

class CartController extends Controller
{
    function index(){
        return view('add');
    }
    public function add_to_cart(Request $request){

        $qty = $request->txtQty;
        $id = $request->hiddenId;
        $name = $request->txtName;
        $price = $request->txtPrice;

        $data['qty']=$qty;
        $data['id']=$id;
        $data['name']=$name;
        $data['price']=$price;
        Cart::add($data);
        return redirect()->back();
    }

    public function update_cart(Request $request){
        $uqty = $request->uqty;
        $rowId = $request->rowId;

        Cart::update($rowId, $uqty);
        return redirect()->back();
    }

    public function delete_cart($rowId){
        Cart::remove($rowId);
        return redirect()->back();
    }
}

此处路由web.php

Route::get('/addproduct','CartController@index')->name('cart_index');
Route::post('/addcart','CartController@add_to_cart')->name('add_to_cart');
//Route::post('/showcart','CartController@show_cart')->name('show_cart');
Route::get('/deletecart/{id}','CartController@delete_cart')->name('delete_cart');
Route::post('update','CartController@update_cart')->name('update_cart');

我怎样才能做到这一点?

1 个答案:

答案 0 :(得分:0)

迁移:$table->increment('id'); id应该自动递增 除非你的模型中有public $increments = false;

或只是添加

MODEL

public $table = 'tablename';
public $fillable = ['your_stuff'];
public $increments = true;

我真的不明白这个问题,所以值得一试