I want to cache some data in my Laravel Application. What I am doing is :
use Illuminate\Support\Facades\Cache;
$put = Cache::put('your_product', array(1,2,3,4), 3000 );
If I do var_dump I got false and Its not caching the data.
echo '<pre>';
var_dump( $put );
echo '</pre>';
Is there another way to use the cache in laravel? Or I am doing wrong?
BTW, I am using latest laravel version.
Use get method.
Cache::put('your_product', array(1,2,3,4), 3000);
dd(Cache::get('your_product'));
Related
I am new to Laravel. I am using Laravel 5.4. For some purpose, I need the current route. For this, I am using the following code in my controller.
$name = Route::currentRouteName();
dd($name);
But, its always showing null. Any help will be appreciated.
I don't know what's problem with your code but you can get URL of current route with following way as well,
$name = URL::current();
To view value you can dump $name,
dd($name);
You can try with following:
$name = Route::current()->getName();
dd($name)
I have two laravel api projects,
1-used for mysql database and another used for mssql database
I called mysql project api service to get orders and some data related with order are exists in mssql and I have written a call of curl in 1st project to get from another information related with order from mssql project api (mssql service api url = localhost/laravelmssql/api/getorderInfo/OrderID)
mssql service call working fine from browser
but when I call it from 1st project with curl its getting wrong database which is configured in 1st project
Note:
This is adding correct database connection info in $_SERVER when I call it from browser
[DB_PORT] => 1433
[DB_DATABASE] => MSSQL DATABASE
[DB_USERNAME] => MSSQL_USERNAME
[DB_PASSWORD] => MSSQL_PASSWORD
but when I call it from curl the $_SERVER don't have these above variables
I have updated code
2nd-project controller for
class OrderController extends BaseController{
public function getOrderDetail(){
// echo env('DB_DATABASE').'-'.str_random(10); // ITS GETTING 1ST CONFIGURE DATABASE NAME
// echo '<pre>';print_r($_SERVER);echo '</pre>'; die;
//
// echo '<pre>';print_r($_SERVER);echo '</pre>';
return OrderDetailsModel::getOrderDetail(1);
}
}
1st-Project Call
//NOT WORKING
$curl_handle=curl_init();
curl_setopt($curl_handle, CURLOPT_URL,'http://apiproject/api/getOrderDetail/1');
curl_setopt($curl_handle, CURLOPT_CONNECTTIMEOUT, 2);
curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, 1);
$query = curl_exec($curl_handle);
curl_close($curl_handle);
echo $query;
//NOT WORKING
dd(file_get_contents('http://apiproject/api/getOrderDetail/1'));
//NOT WORKING
$response = Curl::to(env('MSSQLAPILINK').'get/getOrderDetail/'.$orderID)->withHeaders([
'DB_DATABASE:0000',
'DB_USERNAME:0000',
'DB_PASSWORD:0000',
'DB_PREFIX:000',
])->get();
dd($response);
I am currently working in codeigniter and I am new to this.
I was wondering how to retrieve the JSON values using API call .
Can anyone suggest me where should I start.
Many Thanks in advance
Pass your array of row to json_encode();example for method of controller is below
public function getUserList() {
header('Content-Type: application/json');
$query = $this->db->get('mytable');
if(count($query) > 0) {
$message = array('status' => 'true' , 'message' => 'Record get successfully' , 'data' => $return );
}else{
$message = array('status' => 'false' , 'message' => 'Record not found.' );
}
echo json_encode($message);
}
Codeigniter does not have an inbuilt HTTP method so you need to use other things in php to achieve this.
There are 2 ways, you can use cURL, but honestly... it's convoluted... but read this: http://php.net/manual/en/book.curl.php
Another method is using stream_context_create() http://php.net/manual/en/function.stream-context-create.php
I strongly suggest using this 2nd one as its much easier to work with (in context with curl..
Much of how you setup your request depends on the API you are referencing with and the kind of requests it allows: GET, POST ... and what kind of header information it requires you to send over as well do they require oAuth header?
There is no 1 bunch of code fits all, I had to create a full custom library to integrate codeigniter into Magento, it took many hours.
I'm using Laravel Lumen to build an API.
I've come to a point where I need to find out what SQL query is being generated by Eloquent. I know how to do this in Laravel 4 and Laravel 5 but i've tried the same code in Lumen and the query is blank?
$queries = DB::getQueryLog();
$last_query = end($queries);
echo 'Query<pre>';
print_r($last_query);
exit;
The above code, when run in Laravel works fine - in Lumen the query is blank?
To get the query log in Laravel Lumen working you need to enable it:
DB::connection()->enableQueryLog();
You can add that code in to your controller, middleware, etc then use:
$queries = DB::getQueryLog();
$lastQuery = end($queries);
dd($lastQuery)
To print your query.
You can also use the following with eloquent:
$myModel = Users::where('active', true);
dd($myModel->getSql(), $myModel->getBindings());
You must run the getSql() and getBindings() before you call ->first() or ->get(), etc
Just call this after the query to keep it quick and simple:
echo $query->toSql();
I'm working with Stripe's API and am trying to retrieve data. I have the code below:
$data = \Stripe_Invoice::all(array(
"customer" => $user->customer_id
));
If I set the AJAX response equal to $data, the response is shown as empty ( {} ). If I debug it in the backend, I get a huge list of all kinds of awesome properties to use. All I do is this:
debug($data); // returns huge data set
The trouble is that I can't access the variable in the frontend. I want to use:
console.log(response);
html += response.url;
And things to that effect, but the data is completely empty when the front end interprets it, for some reason.
In the same effect, I can't set it as a session either (I used to set session logs to debug instead of using the debug feature).
$data // can be accessed on the frontend if we use just php to set a variable
$_SESSION['log'] = $data; // empty
What's going on? I'm using the PHP framework CakePHP 3 (latest version of Beta). I think it has something to do with returning the data as serialized (maybe?) but that wouldn't explain the session logging. This happens right before we send the data back:
$this->set(compact('data', $data));
$this->set('_serialize', 'data');
If $data is not empty than you should just use the set method in the controller
$this->set(compact('data', $data));
Than you should have the corresponding view at /src/Template/ControllerName/json/methodName.ctp (Change ControllerName and methodName to what you have)
This file should be this.
<?php
print json_encode($data);
?>
That is all. You should have your data on your client side as a json object.
Turns out the answer was that the values could not be displayed while the array was protected. Calling Stripe's method __toArray() on the Stripe object made the data accessible, and setting worked past this point.
$data = \Stripe_Invoice::all(array(
"customer" => $user->customer_id
));
$data = $data->__toArray();
$this->set(compact('data', $data));
$this->set('_serialize', 'data');