hope you all are doing well. I have two tables in my database purchase__request and 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. And then store them in the purchase_order table with column names as follows: item_name, description, item_qty, dep_name; and still be able to fill in the other columns in this (purchase_order) table. 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
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:
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 uses Models as associations with database tables. You can read more here - https://laravel.com/docs/9.x/eloquent#introduction . Bookmark the Laravel docs, they are very useful.
From what I can see so far, you will need two new models - PurchaseRequest
and PurchaseOrder
As in Laravel's docs, you can create a model with the artisan command: php artisan make:model PurchaseRequest
Also, your purchase__requests
table should be renamed to purchase_requests
(single underline instead of two).
You really need to read at least some of the Laravel docs to be productive with 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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.