在视图中,IM正在尝试显示来自表‘类别’和‘供应商’类别的两行->CATEGORY_NAME和供应商->COMPANY。问题是,在显示的每一行数据中,我应该访问编辑、删除和更新按钮,该按钮请求每次购买的ID。目前一切正常,但我不能显示这两行。
函数show_Purcheses()负责执行此操作。
控制器
use Illuminate\Http\Request;
use App\Models\Suppliers;
use App\Models\Categories;
use App\Models\Purchases;
class Purchases_Controller extends Controller
{
public function show_purchases()
{
return view('bookmarks.purchases.show_purchases',
['purchases' => Purchases::paginate(10)]);
}
public function make_purchase()
{
$categories = Categories::get();
$suppliers = Suppliers::get();
return view('bookmarks.purchases.make_purchase',
compact('categories', 'suppliers'));
}
public function store_purchase_data(Request $request)
{
$this->validate($request, [
'purchased_product' => ['required', 'min:3', 'max:50'],
'categories' => 'required',
'purchase_price' => ['required', 'min:1'],
'quantity' => ['required', 'min:1'],
'expiration_date' => ['required'],
'suppliers' => 'required',
'total_purchase_price',
]);
$total = $request->quantity * $request->purchase_price;
Purchases::create([
'purchased_product' => $request->purchased_product,
'category_id' => $request->categories,
'supplier_id' => $request->suppliers,
'purchase_price' => $request->purchase_price,
'quantity' => $request->quantity,
'expiration_date' => $request->expiration_date,
'total_purchase_price' => $total,
]);
return redirect('/purchases/show_purchases')
->with('message', 'Purchase created successfully!');
}
// Show edit form
public function edit_purchase(Purchases $purchases)
{
$categories = Categories::get();
$suppliers = Suppliers::get();
return view('bookmarks.purchases.edit_purchase',
compact('categories', 'suppliers'),
['purchases' => $purchases]);
}
public function update_purchase(Request $request, Purchases $purchases)
{
$this->validate($request, [
'purchased_product' => ['required', 'min:3', 'max:50'],
'categories' => 'required',
'purchase_price' => ['required', 'min:1'],
'quantity' => ['required', 'min:1'],
'expiration_date' => ['required'],
'suppliers' => 'required',
]);
$purchases->update([
'purchased_product' => $request->purchased_product,
'category_id' => $request->categories,
'supplier_id' => $request->suppliers,
'purchase_price' => $request->purchase_price,
'quantity' => $request->quantity,
'expiration_date' => $request->expiration_date,
]);
return redirect('/purchases/show_purchases')
->with('message', 'Purchase updated successfully!');
}
public function delete_purchase(Purchases $purchases)
{
$purchases->delete();
return redirect('/purchases/show_purchases')
->with('message', 'Purchase deleted successfully');
}
public function auto_purchase()
{
return redirect('/suppliers/show_purchases')
->with('message', 'Product will be purchased again as scheduled');
}
}
查看/刀片
@extends('layout')
@section('content')
<div class="container p-4">
<div>
<table class="table">
<thead>
<tr>
<th scope="col">Medicine</th>
<th scope="col">Category</th>
<th scope="col">Supplier</th>
<th scope="col">Cost</th>
<th scope="col">Total Cost</th>
<th scope="col">Quantity</th>
<th scope="col">Expiration date</th>
<th scope="col">Edit</th>
<th scope="col">Delete</th>
<th scope="col">Auto-purchase</th>
</tr>
</thead>
<tbody class="table-group-divider">
@foreach($purchases as $purchase)
<tr>
<td>{{$purchase->purchased_product}}</td>
<td>{{$purchase->category_name}}</td>
<td>{{$purchase->company}}</td>
<td>{{$purchase->purchase_price}}</td>
<td>{{$purchase->total_purchase_price}}</td>
<td>{{$purchase->quantity}}</td>
<td>{{$purchase->expiration_date}}</td>
<td>
<a class="nav-link" href="/purchases/{{$purchase->id}}/edit">
<i class="bi bi-pencil-square"></i></a>
</td>
<td>
<form method="POST" action="/purchases/{{$purchase->id}}">
@csrf
@method('DELETE')
<button type="submit" class="btn btn-danger"><i class="bi bi-trash3-fill"></i></button>
</form>
</td>
<td>
<a class="nav-link" href="/purchases/{{$purchase->id}}/auto_purchase">
<i class="bi bi-arrow-repeat"></i></a>
</td>
</tr>
@endforeach
</tbody>
</table>
@if(count($purchases)==0)
<p>no purchases found</p>
@endif
<a class="btn btn-primary" href="/purchases/make_purchase" role="button">Add purchase</a>
</div>
</div>
@endsection
路由
Route::get('/purchases/show_purchases', [Purchases_Controller::class, 'show_purchases']);
Route::get('/purchases/make_purchase', [Purchases_Controller::class, 'make_purchase']);
Route::post('/purchases', [Purchases_Controller::class, 'store_purchase_data']);
Route::get('/purchases/{purchases}/edit', [Purchases_Controller::class, 'edit_purchase']);
Route::put('/purchases/{purchases}', [Purchases_Controller::class, 'update_purchase']);
Route::delete('/purchases/{purchases}', [Purchases_Controller::class, 'delete_purchase']);
Route::get('/purchases/auto_purchase', [Purchases_Controller::class, 'auto_purchase']);
机型
class Purchases extends Model
{
use HasFactory;
protected $fillable = [
'purchased_product','category_id','supplier_id',
'purchase_price','quantity','total_purchase_price' ,'expiration_date','image'
];
protected $table = "purchases";
public function suppliers(){
return $this->belongsTo(Suppliers::class);
}
public function categories(){
return $this->belongsTo(Categories::class);
}
}
2条答案
按热度按时间kadbb4591#
如果我正确理解了您的问题和您想要做的事情,那么正确的做法应该是使用“动态属性”检索采购的类别和供应商信息:
看看这个
mnemlml82#
我使用了查询,它解决了我的问题