【发布时间】: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