Laravel + MongoDB 数组字段相关查询

与laravel-mongodb查询关系

06-26

<div class="post-text" itemprop="text"> <p>I Have two models in Laravel</p> <pre><code>class Customer extends Moloquent{ protected $collection = 'Customers'; protected $guarded = []; public function maritalStatus() { return $this->hasOne('App\Models\MaritalStatus', 'maritalStatus_id', '_id'); } } class MaritalStatus extends Moloquent{ protected $collection = 'MaritalStatus'; protected $guarded = []; public function customer() { return $this->belongsTo('App\Models\Customer'); } } </code></pre> <p>And when I make a query </p> <pre><code>$customers = Customer::with('maritalStatus')->get(); </code></pre> <p>the result is a complete customer entry but in marital_status is always NULL</p> <pre><code>> _id: "558daaf95129a452020043b7" address: "Solanda Sector 1" cantonCode: "1" cantonName: "Quito" cellphones: [] created_at: > "2015-06-26 19:41:45" customerSex: "M" customerType: "N" emails: [] > identification: "1715339444" incomeSourceCode: "B" incomeSourceName: > "Empleado Público" isPassport: "0" maritalStatusCode: "" > maritalStatusName: "Soltero" maritalStatus_id: > "558d7c545129a45202004355" > **marital_status: null** names: "Bryan Alexis" parishCode: "8" parishName: "Chillogallo" provinceCode: "17" provinceName: "Pichincha" > surnames: "Guamba Chamorro" telephones: [] updated_at: "2015-06-26 > 19:41:45" user_id: "558d7c5e5129a452020043aa" </code></pre> <p>I try several times with diferent options but i dont know what happend, I am crazy!, please help me!</p> </div> 问答

如何在嵌入式文档中查找,嵌入式文档使用mongodb具有id

06-24

<div class="post-text" itemprop="text"> <p>I'm using mongoDb in my project, and I have problem using lookup on embedded data.. I searched for my problem and read similar codes.. but they didn't help my problem..</p> <p>I mean in my embed document there is a variable named <code>user</code> that I want to load its data by lookup..</p> <p>this is how my documents is: </p> <pre class="lang-php prettyprint-override"><code>"_id" : ObjectId("5d0f6a993702da22bc00474d"), "quotes" : [ { "amount" : NumberInt(100), "user" : { "_id" : ObjectId("5cc69f7f46ab4cedda5c3c12") }, "details" : "some quote details", "_id" : ObjectId("5d0f6a993702da22bc00474c"), "status" : "a" }, { "amount" : NumberInt(120), "user" : { "_id" : ObjectId("5cc69kij46ab4cedda5c3c12") }, "details" : "some quote details 2", "_id" : ObjectId("5d0f6a993702da22bc00484k"), "status" : "a" } ], "status" : "Approved", "title" : "gardening", "created_at" : NumberInt(1560841417) </code></pre> <p>so I want my output be like this:</p> <pre class="lang-php prettyprint-override"><code>"_id" : ObjectId("5d0f6a993702da22bc00474d"), "quotes" : [ { "amount" : NumberInt(100), "user" : { "_id" : ObjectId("5cc69f7f46ab4cedda5c3c12"), "firstName": "John", "lastNAme": "Doe", "phone": 514298742 }, "details" : "some quote details", "_id" : ObjectId("5d0f6a993702da22bc00474c"), "status" : "a" }, { "amount" : NumberInt(120), "user" : { "_id" : ObjectId("5cc69kij46ab4cedda5c3c12") "firstName": "Jane", "lastNAme": "Doe", "phone": 5148547642 }, "details" : "some quote details 2", "_id" : ObjectId("5d0f6a993702da22bc00484k"), "status" : "a" } ], "status" : "Approved", "title" : "gardening", "created_at" : NumberInt(1560841417) </code></pre> </div> 问答

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 大白 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览

应支付9.90元
点击重新获取
扫码支付

支付成功即可阅读