【问题标题】:Total SUM Input Form Value With LaravelLaravel 的总 SUM 输入表单值
【发布时间】:2019-04-12 17:15:02
【问题描述】:

我想在 Laravel(仅)中创建一个包含 3 个字段的表单:title, price, quantity,页面名称为“create.blade.php”。

我的第一个问题是当我输入 3 个值时,什么也没有发生!页面 create.blade.php 卡住了!我没有错误信息

我的第二个问题是在我的页面 'index.blade.php' 上获取总金额

在我的表产品我有这个:

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();
        });
    }

在我的 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');
    }

在我的模型产品

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

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

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

在我的 create.blade.php 我有这个:

 <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">OK</button>

            </form>

路线

Route::resource('/products', 'ProductController');

文件index.blade.php

@foreach($products as $product)
                <tr>
                   <td> {{$product->title}}</td>
                   <td> {{$product->quantity}}</td>
                   <td> {{$product->price}}</td>
                   <td> {{$product->total}}</td>
                   <td>
                     <form method="POST" action="{{ route('products.destroy', $product) }} ">
                    <a class="btn btn-sm btn-warning" href="{{route('products.edit',$product->id)}}">Editer</a>
                    @csrf
                    @method('DELETE')
                    <button type="submit" class="btn btn-sm btn-danger">Deleter</button>
                    </form>
                    </td>
                </tr>
                @endforeach

如果我已经知道如何解决关于 create.blade.php 的第一个问题,我很高兴。

非常感谢您的帮助。

编辑:

我不知道总金额...

【问题讨论】:

  • 请告诉我们您的路线!
  • @Lucas Piazzi:你好 Piazzi,^^ 我已经编辑了我的信息。

标签: laravel


【解决方案1】:

第一个问题:

在您的productController 中,您正在验证'total' =&gt; 'required',但在您的create.blade.php 文件中,您没有提交任何关于total 的内容。结果,验证返回错误,但您甚至没有显示它。所以,你认为表单被卡住了,虽然它不是。实际上它正在重定向回来,需要提交total 的验证错误。

第二个问题

我不是 100% 确定这个解决方案,但你可以试试下面的 mutator。模型。

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

顺便说一句,您不需要在您的

中使用不必要的getTotalAttribute 方法

来自 cmets

您似乎在与 Eloquent 的 mutator 作斗争。所以,让我们从模型和控制器中删除 mutator setTotalAttribute 方法和访问器

替换以下行:

Product::create($request-&gt;all());

通过以下几行:

$data = $request->all(); 
$data['total'] = $request->price * $request->quantity; 
Product::create($data);

现在检查它是否有效。

【讨论】:

  • 谢谢你,我的第一个问题已经解决了。但是现在,我怎样才能得到总数呢?我的模型产品不正确???
  • 更新答案,试试看。
  • @Imrar:谢谢Imran,在我的index.blade.php 中我已经把这个&lt;td&gt; {{$product-&gt;setTotalAttribute-&gt;total}}&lt;/td&gt; 放在了App\Product::setTotalAttribute must return a relationship instance. 的错误消息中,我真的被卡住了...... :-(
  • 似乎您正在与 Eloquent 的 mutator 作斗争。所以,让我们从模型中删除setTotalAttribute 方法。在控制器$data = $request-&gt;all(); $data['total'] = $request-&gt;price * $request-&gt;quantity; Product::create($data);
  • 更新了包含上一条评论的答案。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-07-01
  • 1970-01-01
  • 2014-06-07
相关资源
最近更新 更多