How do i get php object from json? - google-api

this case Google api v3 calendar
$array = $client->getAccessToken();
dump $array
$array = { ["access_token"]=>"i81bIkE" ["token_type"]=>"Bearer" ["expires_in"]=>3600 ["refresh_token"]=>"G3LQL_cQ" ["created"]=> 1495019807 }
i want to get ["refresh_token"] value.
$refresh_token = $array -> refresh_token;
i try this code ,but error
$token_array = json_decode($array);
error message
Warning: json_decode() expects parameter 1 to be string, array given in...
plese help ...

#Andy, as this tell you, you can't use this with array.
did you use:
echo $array['refresh_token'];

json_decode is a PHP method with string param. You try to pass an array.
refresh_token is a string. You can do :
$refresh_token = $array['refresh_token'];
to get it.

Related

How to turn a collection into an array in laravel

I'm not sure if my title is correctly done, but what I'm trying to do is get all the notifications that a user didn't read.
I have 2 tables the first table is notifications and the second one is read_notifications.
Here is my code in User.php model
$read = DB::table('read_notifications')->select('notification_id')->where('user_id', $this->id)->get();
$unread = Notification::whereNotIn('id', $read)->get();
Here I'm getting all the notification IDs in the read_notifications table and I want to put them in the $unread statement.
The error I get when I do this is
Object of class stdClass could not be converted to string
Notification::whereNotIn('id', $read)->get();
$read is an object and id should be a string (or integer). You should use an id field from your $read object like $read[$i]->id.
Also whereNotIn needs an array as a second argument, so you need to collect your IDs from $read to an array like [1,2,3].
Thanks for helping. I found what I needed, I needed to use the pluck method.
Here is the updated code
$read = DB::table('read_notifications')->select('notification_id')->where('user_id', $this->id)->pluck('notification_id');
$unread = Notification::whereNotIn('id', $read)->get();
for whereNotIn to work $read should be an array.
$read = DB::table('read_notifications')->select('notification_id')->where('user_id', $this->id)->get();
foreach($read as $re)
$ot[] = $re->notification_id;
$unread = Notification::whereNotIn('id', $ot)->get();
OR you can use pluck
$read = DB::table('read_notifications')->where('user_id', $this->id)->pluck('notification_id');
$unread = Notification::whereNotIn('id', $read)->get();

Get query string in laravel 6

I have a problem here I send the date via url, so my url becomes like this
localhost:8000/info/transaksi/89?start_date=2019-09-20&end_date=2019-12-20
I want to take my time to be a variable containing something like this
$var = ?start_date=2019-09-20&end_date=2019-12-20
can it be posible?
or can I replace my web.php url request on the controller?
localhost:8000/info/transaksi/89?start_date=2019-09-20&end_date=2019-12-20
be like this
sandbox/synch/account/89?start_date=2019-09-20&end_date=2019-12-20
i'm only need start_date=2019-09-20&end_date=2019-12-20
i try with
$input = $request->all();
but it produces a different format because it becomes an array
Get query string:
$uri = \Request::getRequestUri();
$query = parse_url($uri)['query'];
$query is the query string.
get explicitly data.
$start_date = request()->start_date;
$end_date = request()->end_date;
Get raw query string:
$request->getQueryString()

Using querystrings with Laravel

I have a URL www.mydomain.com/jobs?applications=2|4|6
I am trying to get the values to work with my Input::get but failing. I have tried using array but this doesn't work. Can anyone advise? I'm unfamiliar with using Laravel with this structure of querystring.
$applicationIDs = Input::get('applications');
$applications = Job::with('users');
if(!empty($applicationIDs)){
$applications->whereIn('id', $applicationIDs);
}
$applications = $applications->get();
Your applications parameter is just a string. Use explode to turn it into an array of ids:
$applicationIDs = explode('|', Input::get('applications'));

Codeigniter Active Record return type

I tried to get a umat_id with this SQL query :
SELECT umat_id FROM msumat WHERE nama = $nama
I converted this SQL query into CI's Active Record :
$this->db->select('umat_id');
$terdaftar = $this->db->get_where('msumat', array('nama' => $nama));
So this query should return a string (example : "John").
But I got an error when I tried to echo it :
Object of class CI_DB_mysql_result could not be converted to string
I have tried something like this : echo (string)$terdaftar;, but it's not working.
All I want is to echo "John"
EDIT
Just said I want to insert "John" into a variable. How to do that?
$john = ????
As some of the users already pointed the solution, I'm only explaining why you did get this error so you can understand better the querying results that codeigniter gives.
This error:
But I got an error when I tried to echo it : Object of class
CI_DB_mysql_result could not be converted to string
Happens because you were trying to echo an object.
This piece of code
$terdaftar = $this->db->get_where('msumat', array('nama' => $nama));
Will return an object, this object will have information about the query you've done.
With this object you can get the result(rows) as objects doing this:
$results = $terdaftar->result();
Or you if you feel more comfortable with arrays you can return the results(rows) as an array doing this:
$results = $terdaftar->result_array();
You can also get the number of results doing this:
$number_results = $terdaftar->num_rows()
And this is just an example you can read more about the results here
http://ellislab.com/codeigniter/user-guide/database/results.html
EDIT
A better explanation: imagine that we use the result_array() function to get the result in a pure array format:
$results = $terdaftar->result_array();
Now your variable $results is an array, to iterate through it and get the data you want you'll do something like this:
foreach ($results as $key => $row) {
//the row variable will have each row of your database returned by your query
//so if you want to access a field from that row,
//let's say for example the name field. You would do something like this
if($row['name']=='John')
echo $row['name'];
}
Try:
$this->db->select('umat_id');
$terdaftar = $this->db->get_where('msumat', array('nama' => $nama));
foreach ($terdaftar->result() as $row)
{
echo $row->umat_id;
}
Read the documentation for more information.
Try this:
$this->db->select('umat_id');
$terdaftar = $this->db->get_where('msumat', array('nama' => $nama));
$row = $terdaftar->row_array();
$your_variable = $row['umat_id']; /*Here comes your john*/

array inside array in codeigniter

I am trying to put the integer value of $QQ array into presenter array, no luck please help.
$QQ = $this->MAudio->getAllAudio();
$presenter = $this->Profile_model->getProfile($QQ['a_presenter_id']);
$data['name'] = $presenter['name'];
I think issue is with $this->MAudio->getAllAudio(); I think u have return Object now u got to convert that object into value by calling row() or row_array() or result() or result_array()
Try this code
$QQ = $this->MAudio->getAllAudio()->row_array();
$presenter = $this->Profile_model->getProfile($QQ['a_presenter_id']);
$data['name'] = $presenter['name'];

Resources