Reputation: 275
I am a newbie in Laravel MongoDB I would like to mention a problem here, outline of my Mongo document should be like
{
_id:****,
subscriptions: [{list_id: "14Q3"},
{list_id: "153"}],
offers: [ { targetURL: "www.qwerty.com", title: "25% discount" },
{ targetURL: "www.abcd.com", title: "55% discount" } ],
}
I have used the following code to insert a list id (was successful).
DB::connection('mongodb')->collection('subscribers')->insert(array(
'_id' => $subscriber->device_id,
'subscriptions' => array('list_id' => $subscriber->list1_id),
));
But when I try to push another list_id as next object it is showing error. I used the following code for pushing
DB::connection('mongodb')->collection('subscribers')
->push('subscriptions', array('list_id' => $subscriber->list1_id));
I don't know whether my code is right or wrong. I need to store the data as the outline given above. This is My actual problem. Please correct me....
My controller is is given below, I create new collection for each subscriber. Problem is second 'if' statement
public function store() {
$newsubscriber = Input::json();
//DB::connection('mongodb')->collection($newsubscriber->get('device_id'))->delete();
$result = Subscriber::where('list1_id',$newsubscriber->get('list_id'))->where('device_id',$newsubscriber->get('device_id'))->get();
if (!$result->isEmpty()) {
return "You are already a subscriber of this List";
}
else{
$result1 = Subscriber::where('device_id',$newsubscriber->get('device_id'))->get();
$subscriber = new Subscriber();
$subscriber->list1_id = $newsubscriber->get('list_id');
$subscriber->device_id = $newsubscriber->get('device_id');
$subscriber->subtype = 1;
$subscriber->save();
if (!$result1->isEmpty()) {
DB::connection('mongodb')->collection($subscriber->device_id)->push('subscriptions', array('list_id' => $subscriber->list1_id));
return "Subscribed successfully 1";
}
else{
DB::connection('mongodb')->collection($subscriber->device_id)->insert(array('_id' => $subscriber->device_id,'subscriptions' => array('list_id' => $subscriber->list1_id),
));
return "Subscribed successfully 2";
}
}
}
First i have used following API
curl -H "Content-Type: application/json" -d '{"list_id":"2","device_id":"987654321"}'
http://localhost/lemmeknw/public/index.php/api/v1/subscribe
This returned "Subscribed successfully 2"
but when I used API for second time
curl -H "Content-Type: application/json" -d '{"list_id":"1","device_id":"987654321"}'
http://localhost/lemmeknw/public/index.php/api/v1/subscribe
There was error "Something went wrong"
Upvotes: 1
Views: 7451
Reputation: 1969
I don't know much about Laravel, but what I'm seeing here:
DB::connection('mongodb')->collection('subscribers')->insert(array(
'_id' => $subscriber->device_id,
'subscriptions' => array('list_id' => $subscriber->list1_id),
));
Looks like you are creating subscriptions
as an object {list_id: <subscriber-list1_id>}
instead of an array of one object [{list_id: <subscriber-list1_id>}]
. So when you try to use the push
operation
DB::connection('mongodb')->collection('subscribers')
->push('subscriptions', array('list_id' => $subscriber->list1_id));
that is an attempt to push
to an object, not an array. My guess is that you will need to modify your insert to be
DB::connection('mongodb')->collection('subscribers')->insert(array(
'_id' => $subscriber->device_id,
'subscriptions' => array(array('list_id' => $subscriber->list1_id)),
));
Upvotes: 2