passing data to different functions in laravel - laravel

I am trying to figure out how i can pass a variable from one function to another in controller but, now i am getting null in the second function.
In this function i am able to get the variables very well from a view.
public function getData(Request $request)
{
$user = $request->user;
$amount = $request->amount;
$phone = $request->phone;
$make = $request->make;
}
now I want to use $request->phone and make something like this in the second function :
public function passData(Request $request)
{
$phone = $request->phone;//but from getData
$make = $request->make;
}
Any help will be highly appreciated

You have to call function from where you pass variable. Like if you want pass data from getData function you call passData function.
public function getData(Request $request)
{
$user = $request->user;
$amount = $request->amount;
$phone = $request->phone;
$make = $request->make;
$this->passData($request);
}
and received data in pass function.
public function passData($request)
{
$phone = $request->phone;//but from getData
$make = $request->make;
}

If the phone is in a different controller, you have to import the controller and just use the variable phone by tappinh the Controller name and linked it with the phone variable. But if the phone var is in the same file, I guess you can just use the phone variable right away.

Related

How do I loop over json data

I am using FlutterWave as my payment gateway, I want to loop through a transaction whenever a customer finish making payment and check for the chargeamount which the user paid. the return data is in json format. Here is my code.
public function callback(Request $request)
{
$resp = $request->resp;
$body = json_decode($resp, true);
$txRef = $body['data']['data']['txRef'];
$data = Rave::verifyTransaction($txRef);
dd($data);
return redirect()->route('success');
}
I want to loop and check for the chargedamount but I couldn't. Thanks for your help
Seems like you are dumping an object. I don't think you need a loop to access the data you need. According to your image dump, to access the chargedamount attribute:
dd($data->data->chargedamount);
The data name here repeats, perhaps $payload in this case, its a better name.
public function callback(Request $request)
{
$resp = $request->resp;
$body = json_decode($resp, true);
$txRef = $body['data']['data']['txRef'];
$payload = Rave::verifyTransaction($txRef);
dd($payload->data->chargedamount);
return redirect()->route('success');
}
Some clean up:
public function callback(Request $request)
{
$json = json_decode($request->resp);
$payload = Rave::verifyTransaction($json->data->data->txRef);
dd($payload->data->chargedamount);
return redirect()->route('success');
}
Again that data->data repeating stuff, but in this case, nothing we can do, its in the API.

Where can I put paginate()?

I am trying put paginate(10) to add a pagination later, but when I added it it gives me this error:
Undefined property: Illuminate\Pagination\LengthAwarePaginator::$product
Here is my controller where I want add the pagination(10):
public function index()
{
$user_id = auth()->user()->id;
$user = User::find($user_id);
return view('your')->with('product', $user->product);
}
I did try this:
public function index()
{
$user_id = auth()->user()->id;
$user = User::find($user_id)->paginate(10);
return view('your')->with('product', $user->product);
}
How can I avoid the undefined property error?
Actually find function is used for getting only 1 result and you are applying pagination on that which is resulting into an error.
You might be wanting the products data to be paginated, For that do like this
public function index()
{
$user_id = auth()->user()->id;
$user = User::find($user_id)->product()->paginate(10);
return view('your')->with('product', $user);
}

Loading view inside a Library, issues with cached vars

I am trying to implement a widgets library using load->view. I know I can use include to call directly the file and avoid the vars cache issues but just wondering why it does not work.
Here is how I have structured my code:
My Controller:
class Page extends MY_Controller {
public $data = array();
public function __construct() {
parent::__construct();
...
$this->load->library('widgetmanager');
}
public function index($slug = '') {
echo $this->widgetmanager->show(2);
echo $this->widgetmanager->show(1);
}
}
My Library
class WidgetManager
{
private $CI;
public function __construct()
{
$this->CI = & get_instance();
}
public function show($widget_id) {
$data = array();
$widget_id = (int)$widget_id;
$this->CI->db->select('*');
$this->CI->db->from('widget');
$this->CI->db->where('id', $widget_id);
$query = $this->CI->db->get();
$item = $query->row_array();
$data['widget_title'] = $item['title'];
$data['widget_content'] = $item['content'];
$widget = $this->CI->load->view('widget/'.$item['source'], $data, TRUE);
$data['widget_title'] = '';
$data['widget_content'] = '';
$this->CI->load->view('widget/'.$item['source'], $data);
return $widget;
}
}
widget 1: Calls widget/content
widget 2: Calls widget/banner
What is happening is, the vars set on the first widget call (they are same name as second widget call), get cached, meaning values from the first call are passed to same call. It is weird because are different views.
I have tried:
Using clear_vars(): $this->CI->load->clear_vars(), before and after doing load->view on the library.
Calling load->view with empty array, null, etc
Tried to add a prefix with the widget slug to the vars (that works, but I have to send in some way the prefix to the view, so it is not possible due cache issue)
Any ideas?
Here is what should work.
(I took the liberty of simplifying your database call making it require much less processing.)
public function show($widget_id)
{
$data = array();
$widget_id = (int) $widget_id;
$item = $this->CI->db
->get_where('widget', array('id' => $widget_id))
->row_array();
$data['widget_title'] = $item['title'];
$data['widget_content'] = $item['content'];
$widget = $this->CI->load->view('widget/'.$item['source'], $data, TRUE);
//clear the cached variables so the next call to 'show()' is clean
$this->CI->load->clear_vars();
return $widget;
}
On further consideration The call $this->CI->load->clear_vars(); is probably pointless because each time WidgetManager::show() is called the $data var is recreated with exactly the same keys. When the $data var is passed to load->view the new values of $data['widget_title'] and $data['widget_content'] will replace the values in the cached vars using those keys.

Passing values from Controller to Controller

I am new to laravel and I'm trying to pass a value from one controller to another controller.. In this case.. I want to pass the order_id generated in Order Controller to /pizza/create in Pizza Controller..
this is my code in OrderController
public function store()
{
$user = User::find(Auth::user()->id);
$order= new Order;
$order->user()->associate($user);
$order->status = 'unconfirmed';
$order->save();
return Redirect::to('/pizza/create')
->with('order', $order->order_id);
}
this is my code in /pizza/create in PizzaController
public function create()
{
$id = Session::get('order');
$order = Order::find($id);
return View::make('pizza.create')
->with('order', $order);
}
this somehow works.. but the value (order_id) disappears when i change views/routes..
if you want to pass a value from controller to controller use session, when you are using redirect and with method it is creating session, when you are using view and with method it is creating variable which you can use in view to display that value.
this create a session
return Redirect::to('/pizza/create')
->with('order', $order->order_id);
this statement create a variable which you can use in view
return View::make('pizza.create')
->with('order', $order);
above variable you can use in blade or view file
{{ $order }}
so in create method you need to create a session keep variable alive and available in other controllers
public function create()
{
$id = Session::get('order');
$order = Order::find($id);
Session::put('order',$id);
return View::make('pizza.create')
->with('order', $order);
}

CodeIgniter Controller Method Parameters Issue

I'm using codeigniter 2.1 and I defined a function as follows.
public function reset($email, $hash) {
}
According to MVC architecture and OOPS concept, the function could not execute if I did not pass the parameters in the url. But in codeigniter this function gets executing, So how can i overcome this?. Please help me to find solutions.
Just you need to define null parametra like this:
public function reset($email = null, $hash = null) {
}
If you call function
(controller name)/reset/mail#mail.com/dsadasda
than $email = mail#mail.com & $hash = dsadasda
if you function
(controller name)/reset
than $email and $hash will be null.
Also you can declare default parametre like this.
public function reset($email = mail#mail.com, $hash = dsadasdas) {
}
Hope that I was clear.
If you want to execute function with or without parameters
you can set default values for it.
public function reset($email = '', $hash = '') {
}
This way when there are no parameters function can still execute.
You can use condition for code
public function reset($email = '', $hash = '') {
if(!empty($email) AND !empty($hash)){
//your code here
}
}

Resources