i have a collection "website" which data structure is like
[_id] => MongoId Object (
[$id] => 57beda7f0640fd14ca5cc307
)
[website] => MongoId Object (
[$id] => 57beda3d279871c80e8b4567
)
[url] => https://acb.com
[meta_tags] => Array (
[description] =>
[title] => Login
[title_length] => 18
)
ans same i have 10 rows of data.
now i am using laravel eloquent and i need to fetch all data which website is 57beda3d279871c80e8b4567 and title is empty or null.
who should i update my query for title attribute
$this->website->where('website', $website)->get();
Thanks.
Add another where condition:
$this->website
->where('website', $website)
->where('meta_tags.title', '=','')
->whereNull('meta_tags.title')
->get();
Laravel documentation: https://laravel.com/docs/5.2/queries
Related
I have a BlogPost model that has a belongsToMany relationship called images, this relationship uses a link table to associate the blog post id with the image id.
When pulling in the data for a blog post, the images property looks like this:
[images] => Array
(
[0] => Array
(
[id] => 8304
[original] => /img/blog/2017/5/wifiradio_original_59089cae673db.png
[large] => /img/blog/2017/5/wifiradio_large_59089cae673db.jpg
[medium] => /img/blog/2017/5/wifiradio_medium_59089cae673db.jpg
[small] => /img/blog/2017/5/wifiradio_small_59089cae673db.jpg
[name] => wifiradio.png
[alt] => wifiradio.png
[created_at] => 2017-05-02 14:50:22
[updated_at] => 2017-05-02 14:50:22
[pivot] => Array
(
[blog_post_id] => 47749
[image_id] => 8304
[id] => 136949
[type] => featured
)
)
)
)
The array key is not useful as the numeric value, i would like the array key to be the value of pivot->type.
Laravels keyBy method almost does what I need but I can not get it to work directly on the data returned.
Is it possible to use keyBy from within the model so that data is always returned in a useable format?
I solved this by formatting the array within the controller.
Here's the function I created if anyone has a similair problem:
public function formatPosts($posts){
foreach($posts as $post){
$images = collect($post->images);
unset($post->images);
$post->images = $images->keyBy('pivot.type');
}
return $posts;
}
I have my code in PHP which is returning this Array of data:
GoCardlessPro\Core\ListResponse Object
(
[records] => Array
(
[0] => GoCardlessPro\Resources\Mandate Object
(
[model_name:protected] => Mandate
[created_at:protected] => 2017-04-01T16:49:09.642Z
[id:protected] => ID001
[links:protected] => stdClass Object
(
[customer_bank_account] => CB001
[creditor] => CR001
[customer] => CU001
)
[metadata:protected] => stdClass Object
(
)
[next_possible_charge_date:protected] => 2017-04-06
[payments_require_approval:protected] =>
[reference:protected] => RE001
[scheme:protected] => bacs
[status:protected] => active
[data:GoCardlessPro\Resources\BaseResource:private] => stdClass Object
(
[id] => 123
[created_at] => 2017-04-01T16:49:09.642Z
[reference] => RE001
[status] => active
[scheme] => bacs
[next_possible_charge_date] => 2017-04-06
[payments_require_approval] =>
[metadata] => stdClass Object
(
)
[links] => stdClass Object
(
[customer_bank_account] => 001
[creditor] => CR001
[customer] => CU001
)
)
[api_response] =>
)
)
)
I want to be able to read the ID of the first item in therecords array.
This data is contained inside a variable called $GC_Mandate;
I have tried these:
echo $GC_Mandate->records->{0}->id;
echo $GC_Mandate->records->0->id;
echo $GC_Mandate->records->[0]->id;
$GC_Mandate = $GC_Mandate->records;
echo $GC_Mandate->{0}->id;
But none will return the data
To get the first record, the syntax you need is $GC_Mandate->records[ 0 ].
However, that object is a GoCardlessPro\Resources\Mandate object and its member id is protected1, so we'd need to know the interface of GoCardlessPro\Resources\Mandate (its public methods1), to know if we can somehow retrieve the value of id.
My guess would be getId(), so the full syntax would become
$GC_Mandate->records[ 0 ]->getId()
But, that's just a guess. You'd have to look into the documentation/class definition of GoCardlessPro\Resources\Mandate, to be sure if you can retrieve id.
Turns out (provided I'm linking to the correct github repository) you can do:
$GC_Mandate->records[ 0 ]->id
since GoCardlessPro\Resources\Mandate extends GoCardlessPro\Resources\BaseResource, which exposes the protected members through GoCardlessPro\Resources\BaseResource::__get()2.
1. visibility in PHP
2. magic methods in PHP
I can't comment so I guess I'll have to post.
You should try to print_r($GC_Mandate); and see what it gives out and then go from there.
Try $GC_Mandate->records[0]->__get('id')
it will return all data ..for perticulat data put this in foreach loop
print_r($GC_Mandate['records']);
I'm new to backend programming and CodeIgniter is my first framework. This is new to me. Right now, the thing that I'm working is that I need to create a query based on the JSON encoded data saved at my table.
I have here a segmentation form that should build a query to look/search for the customers profiles based on the condition set
Segmentation form
$segment = array(
...
'filters' => json_encode($post['filters']), //to insert the filters in json
...
);
if ($this->model->addSegments($segment))
The filters saved in the database are like this :
{"data":[{"data":{"condition":"0","attribute":"gender","sign":"=","value":"Male"},"type":"condition","operator":"0","filter":{"operator":"0"}}],"type":"match","operator":"0"}
Looking for the answers, I found this forum and I relate it to the problem that I had: http://www.codingforums.com/php/202207-turning-json-object-into-nested-sql-where-clause.html
Calling the same like function (solution at the link) to the controller using this lines:
$filter = $this->model->getSegmentsFilters($id_segment);
$filters = $this->model->parseFilterToQuery(json_decode($filter['filters'], true));
With the parameter TRUE from the json_decode, It throws the filters in array and using print_r shown like this:
Array (
[0] => Array (
[data] => Array (
[condition] => 0
[attribute] => firstname
[sign] => =
[value] => John )
[type] => condition
[operator] => 0
[filter] => Array (
[operator] => 0 )
)
[1] => Array (
[data] => Array (
[condition] => 0
[attribute] => gender
[sign] => =
[value] => Male )
[type] => condition
[operator] => condition
[filter] => Array (
[operator] => 0 )
)
)
the $sql inside if(is_array) doesn't show any value to concatenate with. I want it to concatenate with another query as a WHERE clause like this idea.
$sql = "SELECT * FROM customers WHERE" . parseFilterToQuery($filters);
If you think that the solution/procedure that I came up is correct. My question is that inside the if(is_array) condition,
how should I work on it with associative JSON array?
I have mongo collection array as below
[_id] => MongoId Object (
[$id] => 7f841cef13cfcdba230019e2
)
[id] => 33
[name] => Adam
[table] => people
I want to add a field count to existing one
[_id] => MongoId Object (
[$id] => 7f841cef13cfcdba230019e2
)
[id] => 33
[name] => Adam
[count] => 0
[table] => people
If count is already set , then update the array.
I have used 'update' method, but need to change the all array instead of one field. How I can do this with php ?
If you are asking how to set the count field to a given value (and creating the field if not present yet) without changing anything else , you can use the $set operator:
{ $set : { count: 0 }}
use update function, like:
$collection->update('$set'=>array("_id" => new MongoID("id of document")),
array("new fiels" => "value of field"));
i've got referenced Users collection object in my MongoDB Items collection. Random Item document looks like this:
ps: to clarify, i really dont want to embed Items into Users collection.
Array
(
[_id] => MongoId Object
(
[$id] => 4d3c589378be56a008000000
)
[modified] => 1295800467
[order] => 1
[title] => MyFirstItem
[user] => Array
(
[$ref] => users
[$id] => MongoId Object
(
[$id] => 4d3c55e7a130717c09000012
)
)
)
So i need to find only items, which are assigned to the specific user. Find this question of my problem, but the solution didnt work for me.
MongoDB-PHP: JOIN-like query
Here is snippet of my code, givin' me no results at all.
$user = $db->users->findOne(array("_id" => new MongoID("4d3c55e7a130717c09000012")));
$items = $db->items->find(array("user" => array('$id' => $user["_id"])));
What is the correct way to finding that data? Should i instead put an user_id as a MongoID without reference?
Spent all my day with this, thanks in advance!
Try
$items = $db->items->find(array("user.$id" => $user["_id"]));