Hey @ignium
Thanks for your suggestion. Unfortunately my edit function is called after return view. I probably should have included the JS in my last post. Apologies.
So when the row edit button is clicked in the view, its taking the id of the row and calls the data via the JS to the edit function in the controller. The edit function then takes all the data where the ID matches and passes it back ti the view via JSON & populates the modal with the data. The bit i'm stuck on is how to pass the product data from Json into the view. I tried using an input field with an ID of products but nothing showed.
This makes sense as technically in the console i'm displaying an array. So the bit i'm stuck on is how to pass the json array into the modal. I know the products for that record are populating as i can see them in the console.
View:
<div class="row">
<div class="col-sm-4">
<label>Make</label>
<input type="text" readonly class="form-control" name="make" id="make">
</div><!-- end col -->
<div class="col-sm-3">
<label>Model</label>
<input type="text" readonly class="form-control capi" name="mdl" id="mdl">
</div><!-- end col -->
<div class="col-sm-2">
<label>Engine</label>
<input type="text" readonly class="form-control" name="engine" id="engine">
</div><!-- end col -->
<div class="col-sm-3">
<label>Year</label>
<input type="text" readonly class="form-control" name="mdl_year" id="mdl_year">
</div><!-- end col -->
</div><!-- end row -->
<div class="row">
<div class="col-sm-12">
<p>Products:</p>
</div><!-- end col -->
<div class="col-sm-3">
<!-- <div id="products" ></div> -->
</div>
</div><!-- end row -->
<script>
/* When click view open modal */
$('body').on('click', '#edit', function () {
var readAction_id = $(this).data('id');
$.get('readAction/' + readAction_id +'/edit', function (data) {
$('#edit-modal').modal('show');
$('#readAction_id').val(data.id);
$('#make').val(data.make);
$('#mdl').val(data.mdl);
$('#mdl_year').val(data.mdl_year);
$('#engine').val(data.engine);
$('#transmission').val(data.transmission);
$('#products').val(data.products);
console.log(data.products);
})
});
</script>
Route:
Route::resource('readAction', 'Customer\VehicleController');
Controller:
public function edit($id)
{
$where = array('id' => $id);
$readAction = Order::where($where)->first();
$readAction->load('products');
return Response::json($readAction);
}
The good news is i can see the products loaded for the record in the console but the difficulty i'm having is displaying that in the view. All of the other inputs are populated based on the ID of the input matching the data form the JS.
Console data when i open a record:
(3) [{…}, {…}, {…}]
0: {id: 1, product_name: "Product One", product_cost: "0", product_sale: "0", status: "Displayed", …}
1: {id: 3, product_name: "Product Two", product_cost: "0", product_sale: "10", status: "Displayed", …}
2: {id: 7, product_name: "Product Three", product_cost: "0", product_sale: "0", status: "Displayed", …}
length: 3
Thanks dude.