【问题标题】:Total, Quantity, Price in LaravelLaravel 中的总计、数量、价格
【发布时间】:2019-09-02 14:56:46
【问题描述】:

我有一个名为“产品”的表,其中包含 5 个字段(id、标题、价格、数量、总计)。

我的目标是通过 products.create 的形式计算总数,价格 * 数量。

数据库 - 产品

public function up()
    {
        Schema::create('products', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->string('title');
            $table->integer('quantity');
            $table->double('price');
            $table->double('total')->nullable();
            $table->timestamps();
        });
    }

模型 - 产品

protected  $fillable = ['title', 'quantity', 'price', 'total']; 

    public function setTotalAttribute()
    {
        $this->total = $this->quantity * $this->price; 
    }

    public function getTotalAttribute($value)
    {
        return $value;
    }

** 控制器 - ProductController**

public function index()
    {
        $products = Product::oldest()->paginate(5);
        return view('admin.products.index', compact('products'))
                  ->with('i', (request()->input('page', 1)-1)*5);
    }


    public function create()
    {

        $products = Product::all();
        return view('admin.products.create', compact('products'));
    }


    public function store(Request $request)
    {
        $request->validate([
                'title' => 'required',
                'quantity' => 'required',
                'price' => 'required',
                'total' => 'required'


        ]);

        Product::create($request->all());

        return redirect()->route('products.index')
                    ->with('success', 'save');
    }

我的问题是在我看来“products.create”当我对 3 个字段进行编码时,我有 3 个字段,没有任何反应???

Products.create

<form class="panel-body" action="{{route('products.store')}}" method="POST" novalidate>
              @csrf
              <fieldset class="form-group {{ $errors->has('title') ? 'has-error' : '' }}">
                <label for="form-group-input-1">Title</label>
                <input type="text" name="title" id="title" class="form-control" value="{{ old('title')}}"/>
                {!! $errors->first('title', '<span class="help-block">:message</span>') !!}
              </fieldset>

              <fieldset class="form-group {{ $errors->has('quantity') ? 'has-error' : '' }}">
                <label for="form-group-input-1">Quantity</label>
                <input type="text" name="quantity" id="quantity" class="form-control" value="{{ old('quantity')}}"/>
                {!! $errors->first('quantity', '<span class="help-block">:message</span>') !!}
              </fieldset>

              <fieldset class="form-group {{ $errors->has('price') ? 'has-error' : '' }}">
                <label for="form-group-input-1">Price</label>
                <input type="text" name="price" id="price" class="form-control" value="{{ old('price')}}"/>
                {!! $errors->first('price', '<span class="help-block">:message</span>') !!}
              </fieldset>

              <a href="{{route('products.index')}}" class="btn btn-primary pull-right">Back</a>
              <button type="submit" class="btn btn-sm btn-primary">Valider</button>

感谢您的帮助。

【问题讨论】:

  • 你想在哪里显示总数?在setTotalAttribute() 中,您可能应该有$this-&gt;attributes['total'] = $this-&gt;quantity * $this-&gt;price; Defining A Mutator.

标签: laravel


【解决方案1】:

首先你没有发送任何总价值......

 $request->validate([
                'title' => 'required',
                'quantity' => 'required',
                'price' => 'required',
        ]);

只需遵循 Eloquent ORM

$product = New Product();
$product-title = $request->title;
$product-quantity = $request->quantity;
$product-price = $request->price;
$product-total = $request->price * $request* quantity;
$product->save();

//重定向()

【讨论】:

    【解决方案2】:

    mutator 将接收在属性上设置的值,所以你的 mutator 方法应该是这样的 Product model

    public function setTotalAttribute()
    {
        $this->attributes['total'] = $this->quantity * $this->price;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-12-10
      • 2022-01-04
      • 1970-01-01
      • 1970-01-01
      • 2021-01-27
      • 2021-07-07
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多