Using querystrings with Laravel - 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'));

Related

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()

How to use search value contain in array field using eloquent in Laravel

I'm working on laravel array serialize. Below is serialize in controller.
public function CreateSave(CreateTestTopicRequest $request){
...code..
$testtopic->class_room_id = $request->classroom;
$testtopic->roomno = serialize($request->roomno);
...code..
}
Then, roomno will be saved to database like.
a:2:{i:0;s:1:"1";i:1;s:1:"2";}
I would like to get result. For example class_room_id = 1 and roomno only contain in roomno array. I may use command to get all as below.
$testtopics = TestTopic::where('class_room_id',1)->get();
But, I do not know to get record only class_room_id = 1 and roomno contain in array. Any advice or guidance on this would be greatly appreciated, Thanks
You can use like search in json fields
TestTopic::where('class_room_id',1)->where('roomno', 'like', '%"id": 1%')->first()
When checking for an array of values the whereIn method can be used:
$roomno = 'a:2:{i:0;s:1:"1";i:1;s:1:"2";}';
$testtopics = TestTopic::where('class_room_id',1)
->whereIn('roomno', unserialize($roomno))
->get();
Multiple where statements can be combined by passing an array:
$roomno = 'a:2:{i:0;s:1:"1";i:1;s:1:"2";}';
$users = TestTopic::where([
['class_room_id', '=', '1'],
['roomno', '=', $roomno],
])->get();

How do i get php object from json?

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.

www.parse.com how to implement OR query

Hi Guys I am working on Parse php SDK ,Where I need to run a OR query but it does not working .I have read some of the article about this but did not get succcess (https://www.parse.com/questions/parsequeryequalto-or-parsequeryequalto)
This is my code
$query1 = new ParseQuery("Chat");
$query1->equalTo("sender", "emraanhashmi000");
$query2 = new ParseQuery("Chat");
$query2->equalTo("receiver", "emraanhashmi000");
$query3 = ParseQuery.orQueries($query1,$query2);
$result = $query3->find();
Please suggest me how can I run Or query in php
Given that $query1 and $query2 gives you the result you expect, you should use the following syntax to OR them together.
$query3 = ParseQuery::orQueries([$query1, $query2]);
$result = $query3->find();
Also, remember that parse.com is closing down and you should migrate to the open-source "parse-server" ASAP.

Laravel query optimization

I have a query in laravel:
...
$query = $model::group_by($model->table().'.'.$model::$key);
$selects = array(DB::raw($model->table().'.'.$model::$key));
...
$rows = $query->distinct()->get($selects);
this works fine and gives me the fields keys' that I need but the problem is that I need to get all the columns and not just the Key.
using this:
$selects = array(DB::raw($model->table().'.'.$model::$key), DB::raw($model->table().'.*'));
is not an option, cuz it's not working with PostgreSQL, so i used $rows to get the rest of columns:
for ($i = 0; $i<count($rows); $i++)
{
$rows[$i] = $model::find($rows[$i]->key);
}
but as you see this is it's so inefficient, so what can i do to make it faster and more efficient?
you can find the whole code here: https://gist.github.com/neo13/5390091
ps. I whould use join but I don't know how?
Just don't pass anything in to get() and it will return all the columns. Also the key is presumably unique in the table so I don't exactly understand why you need to do the group by.
$models = $model::group_by( $model->table() . '.'. $model::$key )->get();

Resources