I have a view named index.blade.php which shows information about a vendor, which was passed through a Controller e.g. VendorController, on this view I have a button "View address", now I want to display the data of this vendor on button click. I also have another controller namely VendorAddressController which fetches the address of the vendor from the db. Code of index.blade.php file :
<td>
<a href="{!! $vendor->slug !!}/vendor-address/" class="btn btn-primary">View Address</a>
</td>
in VendorAddressContoller
public function index($id)
{
$vendor = Vendors::find($id);
$vendorsaddress = VendorAddress::where('vendor_id', $vendor->id)->paginate(15);
return view('admin.vendoraddress.index',['vendorsaddress'=>$vendorsaddress, 'vendor'=>$vendor, 'address'=>$address]);
}
But when i click on button nothing is coming
There is a multiple way to do that. You can choose one the many choice as per your requirement.
1. By Session
You can do this in the first controller
Session::put('key', 'value');
Then in the second
Session::get('key');
2. By Cookies
Your first controller:
$response = Response::make('Hello World');
return $response->withCookie(Cookie::make('name', 'value', $minutes));
And get it in the next request
$value = Cookie::get('name');
You can found more from here.
Two things you should keep in mind :
- You do not call data from one controller to another.
- You need not to create different controller for every thing for one entity. To be very clear all the process regarding a vendor should go into one single VendorController if you want to divide the job to make the controller small use classes,Models and Repositories(for DB interaction) and may be helpers.
So, back to your question, you want to display the data of this vendor upon button click(dynamically). Then you should make use of jquery, I am adding small code snippet for the same :
make a new route in route.php(for Laravel 5.2 and earlier) or web.php(for Laravel 5.3)
Route::get('vendor-address/{id}', 'VendorController@getAddress');
create a js file lets call it custom.js and place it in your public or resources folder, lets keep it in public for the time being. Content of the same :
$('#show-address').click(function()
{
//get the id of the vendor we want to fetch address of
var vendor_id = $(this).data('id');
//send a get request to our controller to get the data
$.get('/vendor-address/'+vendor_id, function(data)
{
//store the json data we got from the controller into a variable
var parsed = JSON.parse(data);
//remove all the content of this vendors address div so that the address doesn't repeats
$('.vendor_address_'+vendor_id).empty();
//append the address in this div
$('.vendor_address_'+vendor_id).append(parsed);
});
})
now in your view you should have :
<button class="btn btn-success" data-id="{{$vendor->id}}">Show address</button>
//and a empty div, our address will be appended here
<div class="vendor_address_{{$vendor->id}}"></div>
now in your controller :
public function getAddress($id)
{
$address = Vendor::where('id', $id)->first();
return json_encode($address->address);
}
Please modify few fields according to your requirement