如何在发布到数据库之前实时重载laravel ex:单元格1+单元格2=单元格3然后发布



am在编辑页面中,我想插入价格,然后插入库存,然后在过帐之前,总价将在总价单元格中自动计算,任何正文都有帮助吗?

**

控制器页面是

**

adminController.php

<?php
namespace AppHttpControllers;
use IlluminateHttpRequest;
use DB;
use AppUser;
use AppProduct;
class adminController extends Controller
{
public function users()
{
$users=User::all();
$users=User::paginate(5);
return view('adminVeiw.user', compact('users'));
}
public function products()
{
$products=Product::all();
$products=Product::paginate(5);
return view('adminVeiw.product', compact('products'));
}

/////////////////////////////////Users//////////////////////////////////////////////
public function addUser(Request $request){
if($request->isMethod('post')){
$this->validate($request,[
'id' => 'required | max:5 |unique:users' ,
///  'price' => 'required |min:2 |max:8'
]);
$users = new User();
$users->id=$request->input('id');
$users->name=$request->input('name');
$users->email=$request->input('email');
$users->password=$request->input('password');
$users->phone=$request->input('phone');
$users->save();
}
return view('adminVeiw.userAdd');

}

public function EditUser(Request $request,$id){
if($request->isMethod('post')){
$users= User::find($id);
$users->id=$request->input('id');
$users->name=$request->input('name');
$users->email=$request->input('email');
$users->password=$request->input('password');
$users->phone=$request->input('phone');
$users->save();
return redirect("users");
}else{
$users=User::find($id);
$arr=Array('users'=>$users);
return view('adminVeiw.userEdit',$arr);
}
}

///////////////////////////////Product////////////////////////////////////////////////

public function AddProduct(Request $request){
if($request->isMethod('post')){
$this->validate($request,[
'id' => 'required | max:5 |unique:products' ,
///  'price' => 'required |min:2 |max:8'
]);
$products = new Product();
$products->id=$request->input('id');
$products->name=$request->input('name');
$products->barcode=$request->input('barcode');
$products->price=$request->input('price');
$products->instock=$request->input('instock');
$products->tax=$request->input('tax');
$products->taxval=$request->input('taxval');
$products->discount=$request->input('discount');
$products->discountval=$request->input('discountval');
$products->description=$request->input('description');
$products->totalPrice=$request->input('totalPrice');
$products->file=$request->input('file');
$products->save();
}
return view('adminVeiw.productAdd');

}

public function EditProduct(Request $request,$id){
if($request->isMethod('post')){
$products= Product::find($id);
$products->id=$request->input('id');
$products->name=$request->input('name');
$products->barcode=$request->input('barcode');
$products->price=$request->input('price');
$products->instock=$request->input('instock');
$products->tax=$request->input('tax');
$products->taxval=$request->input('taxval');
$products->discount=$request->input('discount');
$products->discountval=$request->input('discountval');
$products->description=$request->input('description');
$products->totalPrice=$request->input('totalPrice') ;
$products->file=$request->input('file');
$products->save();
return redirect("products");
//  return view('adminVeiw.productEdit');


}else{
$products=Product::find($id);
$arr=Array('products'=>$products);
return view('adminVeiw.productEdit',$arr);
}
}
///////////////////////////////////////////search function
public function searchproduct(Request $request)
{
$products = $request->get('products');
$products = DB::table('products')->where('name', 'like', '%'.$products.'%')->paginate(5);
return view('adminVeiw.product', ['products' => $products]);
}
///////////////////////////////////////////////////////////////////////

}
?>

productEditblade.php

@extends('layouts.app')
@section('content')
<div class="container">
<div class="card "  style="width: 50rem;">
<div class="card-header ">
<h1> EDIT PRODUCT </h1>
</div>
<div class="card-body">
<form action="/productEdit/{{$products->id}}" method="POST">
{{ csrf_field() }}
<div class="form-group{{$errors -> has('id') ? 'has-error' : ''}}">
<h5>ID</h5>
<input type="lable" name="id" value="{{$products->id}}" class="form-control" placeholder="ID" />
</div>
</br>
<div class="form-group{{$errors -> has('barcode') ? 'has-error' : ''}}">
<h5>BARCODE</h5>
<input type="text" name="barcode" value="{{$products->barcode}}" class="form-control" placeholder="Barcode" />
</div>
</br>
<div class="form-group{{$errors -> has('name') ? 'has-error' : ''}}">
<h5>NAME</h5>
<input type="text" name="name" value="{{$products->name}}" class="form-control" placeholder="name" />
</div>
</br>
<div class="form-group{{$errors -> has('description') ? 'has-error' : ''}}">
<h5>DESCRIPTION</h5>
<input type="text" name="description" value="{{$products->description}}" class="form-control" placeholder="Description" />
</div>
</br>
<div class="form-group{{$errors -> has('file') ? 'has-error' : ''}}">
<h5>FILE</h5>
<input type="text" name="file" value="{{$products->file}}" class="form-control" placeholder="IMG/URL" />
</div>
</br>
<div class="form-group{{$errors -> has('instock') ? 'has-error' : ''}}">
<h5>IN STOCK</h5>
<input type="text" name="instock" value="{{$products->instock}}" class="form-control" placeholder="In Stock" />
</div>
</br>
<div class="form-group{{$errors -> has('price') ? 'has-error' : ''}}">
<h5>PRICE</h5>
<input type="text" name="price" value="{{$products->price}}" class="form-control" placeholder="PRICE" />
</div>
</br>
<div class="form-group{{$errors -> has('discount') ? 'has-error' : ''}}">
<h5>DISCOUNT</h5>
<input type="text" name="discount" value="{{$products->discount}}" class="form-control" placeholder="Discount" />
</div>
</br>
<div class="form-group{{$errors -> has('discountval') ? 'has-error' : ''}}">
<h5>DISCOUNT VALUE</h5>
<input type="text" name="discountval" value="{{$products->discountval}}" class="form-control" placeholder="Discount Value" />
</div>
</br>
<div class="form-group{{$errors -> has('tax') ? 'has-error' : ''}}">
<h5>TAXES</h5>
<input type="text" name="tax" value="{{$products->tax}}" class="form-control" placeholder="Taxes" />
</div>
</br>
<div class="form-group{{$errors -> has('taxval') ? 'has-error' : ''}}">
<h5>TAXES VALUE</h5>
<input type="text" name="taxval" value="{{$products->taxval}}" class="form-control" placeholder="Taxes Value" />
</div>
</br>
<div class="form-group{{$errors -> has('totalPrice') ? 'has-error' : ''}}">
<h5>TOTAL PRICE</h5>
<input type="text" name="totalPrice" value=" {{$products->totalPrice = $products->price + $products->instock }}" class="form-control" placeholder="Total Price" />
</div>
</br>

<a  class="btn btn-warning" href="/products"> Returne without Saving  </a>

<input class="btn btn-success"  type="submit" value="Confirm changes">
</form>

</div>
</div>
</div>
@endsection('content')

这是路由web.php

Route::get('/products', 'adminController@products');
Route::get('productAdd/{id}',function ($id){
$products=Product::find($id);
$products->delete();
return redirect("products");
});
Route::post('productAdd',"adminController@AddProduct");
Route::get('productAdd',"adminController@AddProduct");
Route::get('productEdit/{id}',"adminController@EditProduct");
Route::post('productEdit/{id}',"adminController@EditProduct");

即使是初学者也可以提出任何想法

一种方法:创建一个新的控制器product.blade.php的作业,但带有按钮(保存确认等(

@extends('layouts.app')
@section('content')
<div class="row justify-content-center">
<div class="col-md-12">
<div class="card">
<div class="card-header">
<h3> <p style="text-align:center;">Products</p> </h3>
</div>
</br>
<div class="row justify-content-center">
<h4>Veiw as a seller</h4>
</div>
<!---the form ------------------------->
<div class="card-body">
<a  class="btn  btn-outline-secondary btn-lg btn-block" href="/home">Go Home   </a>
</div>
<div class="card-body">
<a  class="btn btn-outline-success btn-lg btn-block" href="/productAdd">Add new product   </a>
</div>
<!---search icon------------------------------>
<form  action="/search" method="get">
<div class="input-group">
<input type="search" name="products" class="form-control">
<span class="input-group-prepend">
<button type="submit" class="btn btn-primary">Search</button>
</span>
</div>
</form>
<table class="table table-striped">
<thead>
<tr>
<th scope="col">ID</th>
<th scope="col">Barcode</th>
<th scope="col">Name</th>
<th scope="col">Description</th>
<th scope="col">Image</th>
<th scope="col">In Stock</th>
<th scope="col">Unit Price</th>
<th scope="col">Unit Discount</th>
<th scope="col">Unit Discount Value</th>
<th scope="col">Tax</th>
<th scope="col">Tax Value</th>
<th scope="col">Total Price</th>
<th scope="col">Save</th>
<th scope="col">Delete</th>
<th scope="col">Edit</th>
</tr>
</thead>
<tbody>
@foreach($products as $c)
<tr>
<th scope="row">{{$c->id}}</th>
<td> {{$c->barcode}}</td>
<td> {{$c->name}}</td>
<td> {{$c->description}}</td>
<td><img src="{{$c->file}}" alt="" style="width: 7rem;"></td>
<td> {{$c->instock}}</td>
<td>{{$c->price}}</td>
<td> {{$c->discount}}</td>
<td> {{$c->discountval}}</td>
<td> {{$c->tax}}</td>
<td> {{$c->taxval}}</td>
<td>  {{$c->totalPrice = $c->price + $c->instock }} </td>
<td>
<a class="btn btn-outline-success" href="productSave/{{$c->id}}">save</a>
</td>
<td>
<a class="btn btn-outline-primary" href="productEdit/{{$c->id}}">EDIT</a>
</td>
<td>
<a class="btn btn-outline-danger" href="productAdd/{{$c->id}}">Delete</a>
</td>
</tr>
@endforeach
</tbody>
</table>
<div class="paginations col-lg-12">
{!! $products->render() !!}
</div>
</div>
</div>
</div>
</div>
</div>
@endsection

这将做与edit.blade.php相同的事情,但现在它将在获取之前检查值,然后这次提交将返回,什么都不做,但你得到了的想法

>  @extends('layouts.app')
>     @section('content')
> 
>     <div class="container ">
>     <div class="card "  style="width: 50rem;">
>       <div class="card-header ">
>       <h1>Are you shure you want to save ?</h1>
>       </div>
>       <div class="card-body ">
> 
>     <form action="/productSave/{{$products->id}}" method="POST">
>     {{ csrf_field() }}
> 
> 
>         <a  class="btn btn-warning" href="/productSaved"> NO  </a>
> 
>         <input class="btn btn-success"  type="submit" value="yes" >
>      <div class="form-group{{$errors -> has('id') ? 'has-error' : ''}}">
> 
> 
>      <h5>ID</h5>
>     <input type="lable" name="id" value="{{$products->id}}" class="form-control" placeholder="ID" />
>     </div>
>     </br>
>     <div class="form-group{{$errors -> has('barcode') ? 'has-error' : ''}}">
>     <h5>BARCODE</h5>
>      <input type="text" name="barcode" value="{{$products->barcode}}" class="form-control" placeholder="Barcode" />
>     </div>
>     </br>
>     <div class="form-group{{$errors -> has('name') ? 'has-error' : ''}}">
>     <h5>NAME</h5>
>      <input type="text" name="name" value="{{$products->name}}" class="form-control" placeholder="name" />
>     </div>
>     </br>
>     <div class="form-group{{$errors -> has('description') ? 'has-error' : ''}}">
>     <h5>DESCRIPTION</h5>
>     <input type="text" name="description" value="{{$products->description}}" class="form-control"
> placeholder="Description" />
>     </div>
>     </br>
>     <div class="form-group{{$errors -> has('file') ? 'has-error' : ''}}">
>     <h5>FILE</h5>
>     <input type="text" name="file" value="{{$products->file}}" class="form-control" placeholder="IMG/URL" />
>     </div>
>     </br>
>     <div class="form-group{{$errors -> has('instock') ? 'has-error' : ''}}">
>     <h5>IN STOCK</h5>
>     <input type="text" name="instock" value="{{$products->instock}}" class="form-control" placeholder="In Stock" />
>     </div>
>     </br>
>     <div class="form-group{{$errors -> has('price') ? 'has-error' : ''}}">
>     <h5>PRICE</h5>
>     <input type="text" name="price" value="{{$products->price}}" class="form-control" placeholder="PRICE" />
>     </div>
>     </br>
>     <div class="form-group{{$errors -> has('discount') ? 'has-error' : ''}}">
>     <h5>DISCOUNT</h5>
>     <input type="text" name="discount" value="{{$products->discount}}" class="form-control" placeholder="Discount" />
>     </div>
>     </br>
>     <div class="form-group{{$errors -> has('discountval') ? 'has-error' : ''}}">
>     <h5>DISCOUNT VALUE</h5>
>     <input type="text" name="discountval" value="{{$products->discountval}}" class="form-control"
> placeholder="Discount Value" />
>     </div>
>     </br>
>     <div class="form-group{{$errors -> has('tax') ? 'has-error' : ''}}">
>     <h5>TAXES</h5>
>     <input type="text" name="tax" value="{{$products->tax}}" class="form-control" placeholder="Taxes" />
>     </div>
>     </br>
>     <div class="form-group{{$errors -> has('taxval') ? 'has-error' : ''}}">
>     <h5>TAXES VALUE</h5>
>     <input type="text" name="taxval" value="{{$products->taxval}}" class="form-control" placeholder="Taxes Value" />
>     </div>
>     </br>
>     <div class="form-group{{$errors -> has('totalPrice') ? 'has-error' : ''}}">
>     <h5>TOTAL PRICE</h5>
>     <input type="text" name="totalPrice" value=" {{$products->totalPrice = $products->price + $products->instock }}"
> class="form-control" placeholder="Total Price" /> </form>
>     </div>
>     </br> </div> </div>
> 
> </div>
> 
> 
> @endsection('content')

顺便说一句,新控制器将类似于savedController.php:

<?php

namespace AppHttpControllers;
use IlluminateHttpRequest;
use DB;
use AppUser;
use AppProduct;

class savedController extends Controller
{
public function products()
{
$products=Product::all();
$products=Product::paginate(5);
return view('adminVeiw.productSaved', compact('products'));
}

/////////////////////////////////save editing ///////////////////////////

public function EditProduct(Request $request,$id){
if($request->isMethod('post')){
$products= Product::find($id);
$products->id=$request->input('id');
$products->name=$request->input('name');
$products->barcode=$request->input('barcode');
$products->price=$request->input('price');
$products->instock=$request->input('instock');
$products->tax=$request->input('tax');
$products->taxval=$request->input('taxval');
$products->discount=$request->input('discount');
$products->discountval=$request->input('discountval');
$products->description=$request->input('description');
$products->totalPrice=$request->input('totalPrice') ;
$products->file=$request->input('file');
$products->save();
return redirect("productSaved");

}else{
$products=Product::find($id);
$arr=Array('products'=>$products);
return view('adminVeiw.productSave',$arr);
}
}
}

相关内容

最新更新