简体   繁体   English

如何在Laravel中将变量从一个控制器传递到另一个控制器

[英]How to pass variable from one controller to another controller in laravel

I have a view named index.blade.php which shows information about a vendor, which was passed through a Controller eg VendorController, on this view I have a button "View address", now I want to display the data of this vendor on button click. 我有一个名为index.blade.php的视图,该视图显示有关供应商的信息,该信息是通过控制器(例如VendorController)传递的,在此视图上,我有一个“查看地址”按钮,现在我想在按钮上显示该供应商的数据单击。 I also have another controller namely VendorAddressController which fetches the address of the vendor from the db. 我还有另一个控制器VendorAddressController,它从数据库中获取供应商的地址。 Code of index.blade.php file : index.blade.php文件的代码:

<td>
   <a href="{!! $vendor->slug !!}/vendor-address/" class="btn btn-primary">View Address</a>
</td>

in VendorAddressContoller 在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 1. 按会议

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: 2. 通过Cookies您的第一个控制者:

$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 : 您应该记住两件事:

  1. You do not call data from one controller to another. 您不会从一个控制器向另一个控制器调用数据。
  2. 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. 非常清楚的是,如果要划分工作以使控制器使用较少的类,模型和存储库(用于DB交互),并且可以充当帮助者,那么与供应商有关的所有过程都应放入一个VendorController中。

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 : 然后,您应该使用jquery,我为此添加了一些小代码段:

make a new route in route.php(for Laravel 5.2 and earlier) or web.php(for Laravel 5.3) 在route.php(适用于Laravel 5.2及更低版本)或web.php(适用于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. 创建一个js文件,将其命名为custom.js并将其放置在您的public或resources文件夹中,暂时将其保持公开状态。 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 请根据您的要求修改几个字段

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM