简体   繁体   English

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

[英]How to auto increment cart id in Laravel 5.6?

In Laravel 5.6, I installed shopping cart. 在Laravel 5.6中,我安装了购物车。 I want to put data as a session and then put a database. 我想将数据作为会话放置,然后再放置数据库。 I did something also but I could not get the id auto increment. 我也做了一些操作,但无法获得id自动递增。 It put in type. 它输入了。 When I input id then its work, but it is embarrassing to the user and not user-friendly. 当我输入id时,它就起作用了,但是它使用户感到尴尬,而且不友好。 I want to make it user-friendly 我想使其变得用户友好

Here's my code: 这是我的代码:

add.blade.php 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 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();
    }
}

Here route web.php 在这里路由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');

How can I achieve this? 我该如何实现?

migration: $table->increment('id'); 迁移: $table->increment('id'); id should auto-increment unless public $increments = false; id应该自动增加,除非public $increments = false; in your model 在您的模型中

or just add 或只是添加

MODEL 模型

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

i really do not understand the question, so it was worth a shot 我真的不明白这个问题,所以值得一试

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

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