view('my.page')->render()
Jun 5, 2016
8
Level 1
Laravel, Jquery, AJAX: How to return view on a Get request
Am new to Laravel, JQuery, Ajax. My requirement is the below:
Based on a selected Customer and Purchase Order in the Invoice index.blade , when the User clicks on Add Invoice button, the Invoice create.blade.php view should be rendered pre-populated with the Customer ID and Purchase Order Nbr that was selected in the 'index.blade.php` to the User.
Issue: The response view is not rendered.
Code components/snippets I have so far:
Invoice - index blade
@extends('layouts.crudMaster')
@section('crud.breadcrumb')
{!! Breadcrumbs::render('invoice') !!}
@stop
@section('crud.header')
<div class="row">
<div class="col-md-10">
<h1>Invoice</h1>
</div>
<div class="col-md-2">
{{--<a href="{{ route('invoice.create', '') }}" class="btn btn-md btn-block btn-primary my-btn-h1-allign ">
<i class="fa fa-plus"></i> Add Invoice</a>--}}
<button id="addNewInvoiceBtn" class="btn btn-md btn-block btn-primary my-btn-h1-allign "> Add Invoice</button>
</div>
</div>
@stop
Java script:
$('#addNewInvoiceBtn').on('click', function () {
//e.preventDefault();
var $custId = $('#selectCustomerForPOSelectList').val();
var $poId = $('#purchaseOrderSelectItems').val();
//alert("Customer ID: " +$customerId+ " po id: " +$purchaseOrderId );
$.get('customer/' + $custId + '/purchase_order/'+ $poId + '/invoice/create', function (data) {
//TODO - how do we show the response data
});
});
Routes.php:
Route::get('customer/{customer}/purchase_order/{purchase_order}/invoice/create', 'InvoiceController@createInvoiceByCustPO')
->name('customer.po.invoice.create');
InvoiceController.php
public function createInvoiceByCustPO(Request $request, $custId, $poId )
{
/*$input = $request->all();
dd($input);*/
$customerId = $custId;
$purchaseOrderId = $poId;
Log::info('New: Create Invoice for user: ' . $customerId . ' and PO ID: ' . $purchaseOrderId);
//Q: Can i return the view like this or do i need the $request->ajax() check here
return view('data_entry.invoice.create')->with('companyCode', 'Qualitech Industries')
->with('customerName', $customerId)
->with('purchaseOrder', $purchaseOrderId);
}
}
I verified the below while debugging
- The ajax call in the java script is invoking the appropriate route with the selected customer id and po id
- the response from the return view - when i use the console.log(data) in the jquery ajax function closure logs the HTML string equivalent of the view - however
the values of customer id/name and po idare not passed back in the HTML response string.
q's:
- Should I be using an AJAX Get in this case since the response would be shown in a Create view instead of the Index view?
- If I should not be using the AJAX call for the Add New Invoice, How should I go about capturing the values selected in the index.blade and setup up the route/controller call?
- What is the process to render a view with data when making jquery, ajax, get request in laravel?
Appreciate your help and direction. Thanks
Please or to participate in this conversation.