[英]How to select specific columns and get data from those columns and store them in different columns in another table? (Laravel)
hope you all are doing well.希望你们都做得很好。 I have two tables in my database purchase__request and purchase_order.
我的数据库 purchase__request 和 purchase_order 中有两个表。 So what I was trying to do is get the data from the columns item_name, description, item_qty, dep_name which there status ='Approved' from the purchase__request table.
所以我试图做的是从购买__request 表中的列 item_name、description、item_qty、dep_name 获取数据,其中 status ='Approved'。 And then store them in the purchase_order table with column names as follows: item_name, description, item_qty, dep_name;
然后将它们存储在purchase_order表中,列名如下:item_name、description、item_qty、dep_name; and still be able to fill in the other columns in this (purchase_order) table.
并且仍然能够填写此 (purchase_order) 表中的其他列。 So when I use the following code below it shows me this Error:
所以当我使用下面的代码时,它会显示这个错误:
Attempt to assign property "[{"item_name":"Paper","description":"A4 size","item_qty":15,"dep_name":"Accounting & Finance"},{"item_name":"d","description":"a","item_qty":4,"dep_name":"Accounting & Finance"}]" on null尝试分配属性 "[{"item_name":"Paper","description":"A4 size","item_qty":15,"dep_name":"Accounting & Finance"},{"item_name":"d", null 上的 "description":"a","item_qty":4,"dep_name":"Accounting & Finance"}]"
PO.blade.php: PO.blade.php:
<form action="{{url('/addPO')}}" method="POST">
@csrf
<div class="form-group row">
<label class="col-lg-4 col-form-label" for="po_date">Order Date <span class="text-danger">*</span>
</label>
<div class="col-lg-6">
<input type="date" class="po_date" id="po_date" name="po_date" required="">
</div>
</div>
<br>
<div class="table-responsive">
<table class="table text-start align-middle table-bordered table-hover mb-0">
<thead>
<tr class="text-dark">
<th scope="col">PR ID</th>
<th scope="col">Item Name</th>
<th scope="col">Description</th>
<th scope="col">Quantity</th>
<th scope="col">Department</th>
</tr>
</thead>
@foreach($PO as $PO)
<tbody>
<tr>
<td><input type="text"value="{{$PO->PR_id}}"hidden="">{{$PO->PR_id}}</td>
<td><input type="text"value="{{$PO->item_name}}"hidden="">{{$PO->item_name}}</td>
<td><input type="text"value="{{$PO->item_name}}"hidden="">{{$PO->description}}</td>
<td><input type="text"value="{{$PO->item_qty}}"hidden="">{{$PO->item_qty}}</td>
<td><input type="text"value="{{$PO->dep_name}}"hidden="">{{$PO->dep_name}}</td>
</tr>
</tbody>
@endforeach
</table>
</div>
<br>
<br>
<div class="form-group row" >
<label class="col-lg-4 col-form-label" for="sum" >Sum<span class="text-danger">*</span></label>
<div class="col-lg-6">
<input type="number" class="sum" id="sum" name="sum" required="">
</div>
</div>
<div class="form-group row" >
<label class="col-lg-4 col-form-label" for="vat" >Vat<span class="text-danger">*</span></label>
<div class="col-lg-6">
<input type="number" class="vat" id="vat" name="vat" required="">
</div>
</div>
<div class="form-group row" >
<label class="col-lg-4 col-form-label" for="approved_by">Approved By<span class="text-danger">*</span>
</label>
<div class="col-lg-6">
<input type="text" class="approved_by" id="approved_by" name="approved_by" required="">
</div>
</div>
<br><br>
<div class="form-group row">
<div class="">
<input type="submit" class="btn btn-success" name="submit" value="Submit" >
</div>
</div>
</form>
Routes:路线:
Route:: view('addPO', 'admin.PO');
Route::post('/addPO', [AdminController::class, 'addDataOrder']);
My function in AdminController:我在 AdminController 中的 function:
public function addDataOrder(Request $request)
{
$PO= new purchase_order();
$PO->po_date=$request->po_date ;
$PO->sum =$request->sum ;
$PO->vat =$request->vat;
$PO->approved_by =$request->approved_by ;
$PO=DB::table("purchase__requests")->select('item_name','description','item_qty','dep_name')->where('status', 'Approved')->get();
foreach($PO as $key->$PO){
DB::table("purchase_order")->insert(
[
'item_name' => $PO->item_name,
'description'=>$PO->description,
'item_qty '=>$PO->item_qty,
'dep_name '=>$PO->dep_name,
]);
}
$PO->save();
return redirect()->back();
}
Please help me I'm new to Laravel.请帮助我,我是 Laravel 的新手。
Laravel uses Models as associations with database tables. Laravel 使用模型作为与数据库表的关联。 You can read more here - https://laravel.com/docs/9.x/eloquent#introduction .
您可以在此处阅读更多信息 - https://laravel.com/docs/9.x/eloquent#introduction 。 Bookmark the Laravel docs, they are very useful.
收藏 Laravel 文档,它们非常有用。
From what I can see so far, you will need two new models - PurchaseRequest
and PurchaseOrder
从我目前所见,您将需要两个新模型 -
PurchaseRequest
和PurchaseOrder
As in Laravel's docs, you can create a model with the artisan command: php artisan make:model PurchaseRequest
在 Laravel 的文档中,您可以使用 artisan 命令创建 model:
php artisan make:model PurchaseRequest
Also, your purchase__requests
table should be renamed to purchase_requests
(single underline instead of two).此外,您的
purchase__requests
表应重命名为purchase_requests
(一个下划线而不是两个下划线)。
You really need to read at least some of the Laravel docs to be productive with Laravel.您确实需要至少阅读一些 Laravel 文档,才能使用 Laravel 来提高工作效率。 I hope you take your time to at least read the link I've sent you.
我希望您花时间至少阅读我发送给您的链接。
Anyway, after you create your models, you could then save the purchase order this way:无论如何,创建模型后,您可以通过以下方式保存采购订单:
$purchaseOrder = new PurchaseOrder();
$purchaseOrder->po_date = $request->po_date;
$purchaseOrder->sum = $request->sum;
// and so on for other fields, then we save the model and this will write the data to the database
$purchaseOrder->save();
You should also do some validation for the $request
variable to make sure that you don't save invalid stuff, such as the po_date
being a thousand years in the past, etc. You can read more about how to do validation with Laravel here - https://laravel.com/docs/9.x/validation#main-content您还应该对
$request
变量进行一些验证,以确保您不会保存无效的内容,例如po_date
是过去一千年等。您可以在此处阅读有关如何使用 Laravel 进行验证的更多信息 - https://laravel.com/docs/9.x/validation#main-content
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.