pbootcms网站模板|日韩1区2区|织梦模板||网站源码|日韩1区2区|jquery建站特效-html5模板网

      <bdo id='MtEkY'></bdo><ul id='MtEkY'></ul>

    1. <tfoot id='MtEkY'></tfoot>

      <legend id='MtEkY'><style id='MtEkY'><dir id='MtEkY'><q id='MtEkY'></q></dir></style></legend>
    2. <small id='MtEkY'></small><noframes id='MtEkY'>

    3. <i id='MtEkY'><tr id='MtEkY'><dt id='MtEkY'><q id='MtEkY'><span id='MtEkY'><b id='MtEkY'><form id='MtEkY'><ins id='MtEkY'></ins><ul id='MtEkY'></ul><sub id='MtEkY'></sub></form><legend id='MtEkY'></legend><bdo id='MtEkY'><pre id='MtEkY'><center id='MtEkY'></center></pre></bdo></b><th id='MtEkY'></th></span></q></dt></tr></i><div class="xpvfhzr" id='MtEkY'><tfoot id='MtEkY'></tfoot><dl id='MtEkY'><fieldset id='MtEkY'></fieldset></dl></div>

    4. Laravel,如何為關系列使用 where 條件?

      Laravel, How to use where conditions for relation#39;s column?(Laravel,如何為關系列使用 where 條件?)

      1. <tfoot id='FiBqf'></tfoot>
            <tbody id='FiBqf'></tbody>
          <i id='FiBqf'><tr id='FiBqf'><dt id='FiBqf'><q id='FiBqf'><span id='FiBqf'><b id='FiBqf'><form id='FiBqf'><ins id='FiBqf'></ins><ul id='FiBqf'></ul><sub id='FiBqf'></sub></form><legend id='FiBqf'></legend><bdo id='FiBqf'><pre id='FiBqf'><center id='FiBqf'></center></pre></bdo></b><th id='FiBqf'></th></span></q></dt></tr></i><div class="fl7n5nj" id='FiBqf'><tfoot id='FiBqf'></tfoot><dl id='FiBqf'><fieldset id='FiBqf'></fieldset></dl></div>

            • <bdo id='FiBqf'></bdo><ul id='FiBqf'></ul>

              <small id='FiBqf'></small><noframes id='FiBqf'>

              <legend id='FiBqf'><style id='FiBqf'><dir id='FiBqf'><q id='FiBqf'></q></dir></style></legend>

                本文介紹了Laravel,如何為關系列使用 where 條件?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

                問題描述

                我正在使用 Laravel 并且在使用 Eloquent ORM 時遇到了一個小問題.我可以使用 JOIN 簡單地使用 SQL 查詢來實現它,但我似乎無法讓它與 Eloquent 一起使用!

                I'm using Laravel and having a small problem with Eloquent ORM.. I can get this working simply with SQL query using a JOIN but I can't seem to get it working with Eloquent!

                這就是我想要的,我有兩張表.一個是Restaurants",另一個是Restaurant_Facilities".

                This is what I want, I have two tabels. one is 'Restaurants' and other is 'Restaurant_Facilities'.

                表格很簡單……而且是一對一的關系.就像有一個帶有 id、name、slug 等的 restaurant 表和另一個名為 restaurant_facilities 的表 帶有 id、restaurant_id、wifi、parking

                The tables are simple.. and One-To-One relations. like there is a restaurant table with id, name, slug, etc and another table called restaurant_facilities with id, restaurant_id, wifi, parking, etc

                現在我想做的是..加載所有wifi = 1或wifi = 0的餐廳..我怎么能用 Eloquent 做到這一點?我嘗試過急切加載、數據透視表、with()、collections(),但似乎沒有任何效果!

                Now what I want to do is.. load all restaurants which have wifi = 1 or wifi = 0.. How can i do that with Eloquent ? I have tried eager loading, pivot tables, with(), collections() and nothing seems to work!

                對于cuisines 的多對多關系,我遇到了同樣的問題!我有相同的 restaurant 表和一個 cuisine 表和一個 restaurant_cuisine_connection 表..

                The same problem I have for a Many-To-Many relation for cuisines! I have the same restaurant table and a cuisine table and a restaurant_cuisine_connection table..

                但是如何使用 ID 將所有餐廳加載到特定美食中?

                but how do I load all restaurants inside a specific cuisine using it's ID ?

                這行得通.

                Cuisine::find(6)->restaurants()->get();

                但我想從 Restaurant:: 模型加載它而不是從菜系中加載..因為我有很多條件鏈接在一起..它用于搜索和過濾/瀏覽頁面.

                but I wanna load this from Restaurant:: model not from cuisines.. because I have many conditions chained together.. its for a search and filtering / browse page.

                任何想法或方法?我已經為此苦苦掙扎了 3 天,但仍然沒有答案.

                Any ideas or ways ? I've been struggling with this for 3 days and still no answer.

                示例模型:

                class Restaurant extends Eloquent {
                
                    protected $table = 'restaurants';
                
                    public function facilities() {
                        return $this->hasOne('Facilities'); 
                    }
                }
                
                class Facilities extends Eloquent {
                
                    protected $table = 'restaurants_facilities';
                
                    public function restaurant() {
                        return $this->belongsTo('Restaurant');
                    }
                
                }
                

                附:這似乎有效..但這不是雄辯的方式嗎?

                PS : This seems to be working.. but this is not Eloquent way right ?

                Restaurant::leftJoin(
                                'cuisine_restaurant', 
                                'cuisine_restaurant.restaurant_id', 
                                '=', 'restaurants.id'
                             )
                             ->where('cuisine_id', 16)
                               ->get();
                

                還有什么是在沒有其他查詢的情況下找到具有特定列值的餐館數量的最佳方法?比如..我必須找到有停車位 = 1 和 wifi = 1 的餐廳總數?

                Also what is the best method to find a count of restaurants which have specific column value without another query ? like.. i have to find the total of restaurants which have parking = 1 and wifi = 1 ?

                請幫忙解決這個問題.

                謝謝.

                推薦答案

                如果您必須從 Restaurant 模型加載,我認為在此處執行左連接沒有任何問題.我可能會將它抽象為我的 Restaurant 模型上的一個方法,如下所示:

                I don't see anything wrong with doing the left join here, if you have to load from the Restaurant model. I might abstract it away to a method on my Restaurant model, like so:

                class Restaurant extends Eloquent {
                    protected $table = 'restaurants'; // will be default in latest L4 beta
                
                    public function facility()
                    {
                      return $this->hasOne('Facility');
                    }
                
                    // Or, better, make public, and inject instance to controller.
                    public static function withWifi()
                    {
                      return static::leftJoin(
                        'restaurant_facilities',
                        'restaurants.id', '=', 'restaurant_facilities.restaurant_id'
                      )->where('wifi', '=', 1);
                    }
                }
                

                然后,從您的路線:

                Route::get('/', function()
                {
                  return Restaurant::withWifi()->get();
                });
                

                在旅途中 - 尚未測試該代碼,但我認為它應該可以工作.您可以改為使用帶有約束的預加載,但這只會指定工具對象是否為空.除非您指定 where 子句,否則它仍會返回所有餐廳.

                On the go - haven't tested that code, but I think it should work. You could instead use eager loading with a constraint, but that will only specify whether the facility object is null or not. It would still return all restaurants, unless you specify a where clause.

                (P.S. 我會堅持使用 Facility 的單數形式.注意 hasOne('Facilities') 怎么讀不正確?)

                (P.S. I'd stick with the singular form of Facility. Notice how hasOne('Facilities') doesn't read correctly?)

                這篇關于Laravel,如何為關系列使用 where 條件?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

                【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

                相關文檔推薦

                Deadlock exception code for PHP, MySQL PDOException?(PHP、MySQL PDOException 的死鎖異常代碼?)
                PHP PDO MySQL scrollable cursor doesn#39;t work(PHP PDO MySQL 可滾動游標不起作用)
                PHP PDO ODBC connection(PHP PDO ODBC 連接)
                Using PDO::FETCH_CLASS with Magic Methods(使用 PDO::FETCH_CLASS 和魔術方法)
                php pdo get only one value from mysql; value that equals to variable(php pdo 只從 mysql 獲取一個值;等于變量的值)
                MSSQL PDO could not find driver(MSSQL PDO 找不到驅動程序)
              • <small id='UmGi6'></small><noframes id='UmGi6'>

                1. <tfoot id='UmGi6'></tfoot>
                2. <legend id='UmGi6'><style id='UmGi6'><dir id='UmGi6'><q id='UmGi6'></q></dir></style></legend>

                  • <bdo id='UmGi6'></bdo><ul id='UmGi6'></ul>
                      <i id='UmGi6'><tr id='UmGi6'><dt id='UmGi6'><q id='UmGi6'><span id='UmGi6'><b id='UmGi6'><form id='UmGi6'><ins id='UmGi6'></ins><ul id='UmGi6'></ul><sub id='UmGi6'></sub></form><legend id='UmGi6'></legend><bdo id='UmGi6'><pre id='UmGi6'><center id='UmGi6'></center></pre></bdo></b><th id='UmGi6'></th></span></q></dt></tr></i><div class="d7ndtlt" id='UmGi6'><tfoot id='UmGi6'></tfoot><dl id='UmGi6'><fieldset id='UmGi6'></fieldset></dl></div>

                            <tbody id='UmGi6'></tbody>

                          主站蜘蛛池模板: 东莞猎头公司_深圳猎头公司_广州猎头公司-广东万诚猎头提供企业中高端人才招聘服务 | 我车网|我关心的汽车资讯_汽车图片_汽车生活! | 全国国际学校排名_国际学校招生入学及学费-学校大全网 | 全自动真空上料机_粉末真空上料机_气动真空上料机-南京奥威环保科技设备有限公司 | 盘装氧量分析仪-防爆壁挂氧化锆分析仪-安徽吉帆仪表有限公司 | 模具硅橡胶,人体硅胶,移印硅胶浆厂家-宏图硅胶科技 | 北京网站建设公司_北京网站制作公司_北京网站设计公司-北京爱品特网站建站公司 | 托盘租赁_塑料托盘租赁_托盘出租_栈板出租_青岛托盘租赁-优胜必达 | 牛奶检测仪-乳成分分析仪-北京海谊 | 电动葫芦|手拉葫芦|环链电动葫芦|微型电动葫芦-北京市凌鹰起重机械有限公司 | 成都茶楼装修公司 - 会所设计/KTV装修 - 成都朗煜装饰公司 | 【ph计】|在线ph计|工业ph计|ph计厂家|ph计价格|酸度计生产厂家_武汉吉尔德科技有限公司 | 东莞精密模具加工,精密连接器模具零件,自動機零件,冶工具加工-益久精密 | 反渗透水处理设备|工业零排放|水厂设备|软化水设备|海南净水设备--海南水处理设备厂家 | 冷藏车厂家|冷藏车价格|小型冷藏车|散装饲料车厂家|程力专用汽车股份有限公司销售十二分公司 | BESWICK球阀,BESWICK接头,BURKERT膜片阀,美国SEL继电器-东莞市广联自动化科技有限公司 | 氮化镓芯片-碳化硅二极管 - 华燊泰半导体 | 常州翔天实验仪器厂-恒温振荡器-台式恒温振荡器-微量血液离心机 恒温恒湿箱(药品/保健品/食品/半导体/细菌)-兰贝石(北京)科技有限公司 | 热闷罐-高温罐-钢渣热闷罐-山东鑫泰鑫智能热闷罐厂家 | 高效节能电机_伺服主轴电机_铜转子电机_交流感应伺服电机_图片_型号_江苏智马科技有限公司 | 高楼航空障碍灯厂家哪家好_航空障碍灯厂家_广州北斗星障碍灯有限公司 | 代理记账_免费注册公司_营业执照代办_资质代办-【乐财汇】 | 注塑机-压铸机-塑料注塑机-卧式注塑机-高速注塑机-单缸注塑机厂家-广东联升精密智能装备科技有限公司 | 深圳市人通智能科技有限公司| 电位器_轻触开关_USB连接器_广东精密龙电子科技有限公司 | 分轨 | 上传文件,即刻分离人声和伴奏 | 洛阳防爆合格证办理-洛阳防爆认证机构-洛阳申请国家防爆合格证-洛阳本安防爆认证代办-洛阳沪南抚防爆电气技术服务有限公司 | 淬火设备-钎焊机-熔炼炉-中频炉-锻造炉-感应加热电源-退火机-热处理设备-优造节能 | 聚天冬氨酸,亚氨基二琥珀酸四钠,PASP,IDS - 远联化工 | 深圳APP开发_手机软件APP定制外包_小程序开发公司-来科信 | 艾默生变频器,艾默生ct,变频器,ct驱动器,广州艾默生变频器,供水专用变频器,风机变频器,电梯变频器,艾默生变频器代理-广州市盟雄贸易有限公司官方网站-艾默生变频器应用解决方案服务商 | 礼仪庆典公司,礼仪策划公司,庆典公司,演出公司,演艺公司,年会酒会,生日寿宴,动工仪式,开工仪式,奠基典礼,商务会议,竣工落成,乔迁揭牌,签约启动-东莞市开门红文化传媒有限公司 | 杭州高温泵_热水泵_高温油泵|昆山奥兰克泵业制造有限公司 | 粒米特测控技术(上海)有限公司-测功机_减速机测试台_电机测试台 | 聚氨酯催化剂K15,延迟催化剂SA-1,叔胺延迟催化剂,DBU,二甲基哌嗪,催化剂TMR-2,-聚氨酯催化剂生产厂家 | 膜结构_ETFE膜结构_膜结构厂家_膜结构设计-深圳市烨兴智能空间技术有限公司 | 旅游规划_旅游策划_乡村旅游规划_景区规划设计_旅游规划设计公司-北京绿道联合旅游规划设计有限公司 | 智能交通网_智能交通系统_ITS_交通监控_卫星导航_智能交通行业 | 英思科GTD-3000EX(美国英思科气体检测仪MX4MX6)百科-北京嘉华众信科技有限公司 | 购买舔盐、舔砖、矿物质盐压块机,鱼饵、鱼饲料压块机--请到杜甫机械 | 打造全球沸石生态圈 - 国投盛世|