Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/email/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 什么';laravel中的With()方法是什么?_Php_Laravel_Eloquent - Fatal编程技术网

Php 什么';laravel中的With()方法是什么?

Php 什么';laravel中的With()方法是什么?,php,laravel,eloquent,Php,Laravel,Eloquent,我已经参考了一些在线参考资料,以了解如何使用laravel中的方法,但仍然不明白。 这里有一些我不懂的代码,我可以知道a,b,c,d,e,f是指什么吗 $example = Test::with('a', 'b', 'c', 'd', 'e', 'f'); 让我们给你一个直接的例子。如果您有user表,并且该用户表与多个表相关,对吗??它也属于多个表 这里,我要四张桌子 城市 身份证 名字 用户 App\User::with('city','profile','do

我已经参考了一些在线参考资料,以了解如何使用laravel中的方法,但仍然不明白。

这里有一些我不懂的代码,我可以知道a,b,c,d,e,f是指什么吗

 $example = Test::with('a', 'b', 'c',
            'd', 'e', 'f');

让我们给你一个直接的例子。如果您有user表,并且该用户表与多个表相关,对吗??它也属于多个表

这里,我要四张桌子

城市

  • 身份证
  • 名字
  • 用户

    App\User::with('city','profile','documents')->get();
    
  • 身份证
  • 名字
  • 城市标识
  • 用户档案

  • 身份证
  • 用户id
  • 地址
  • 电话
  • 用户文档

  • 身份证
  • 用户id
  • 文件名
  • 类型

  • 用户
    属于一个城市

    用户
    有一个
    配置文件

    用户
    有许多
    文档

    现在,在用户模型中,您可以如下定义关系

    User.php

    //User belongs To City
    public function city(){
        return $this->belongsTo('App\City','city_id','id')->withDefault();
    }
    
    
    //User hasone profile
    public function profile(){
        return $this->hasOne('App\Profile','user_id','id')->withDefault();
    }
    
    //User hasmany document
    public function documents(){
        return $this->hasMany('App\UserDocument','user_id','id')->withDefault();
    }
    
    现在,如果您想访问控制器中的所有数据,则可以使用with()

    控制器

    App\User::with('city','profile','documents')->get();
    
    您将获得对象中的所有数据


    用户

    App\User::with('city','profile','documents')->get();
    
    身份证

    名字

    • {城市数据}

    • {profile}

    • {文件}


    此外,您还可以获取多个模型嵌套关系,例如城市属于州,如果您希望获取城市的州数据

    User::with('city.state','profile','documents')->get(): 
    //define state relationship in city model you'll get state data with the city as well.
    
    您还可以使用()在
    中添加条件


    让我们给你一个直接的例子。如果您有user表,并且该用户表与多个表相关,对吗??它也属于多个表

    这里,我要四张桌子

    城市

  • 身份证
  • 名字
  • 用户

    App\User::with('city','profile','documents')->get();
    
  • 身份证
  • 名字
  • 城市标识
  • 用户档案

  • 身份证
  • 用户id
  • 地址
  • 电话
  • 用户文档

  • 身份证
  • 用户id
  • 文件名
  • 类型

  • 用户
    属于一个城市

    用户
    有一个
    配置文件

    用户
    有许多
    文档

    现在,在用户模型中,您可以如下定义关系

    User.php

    //User belongs To City
    public function city(){
        return $this->belongsTo('App\City','city_id','id')->withDefault();
    }
    
    
    //User hasone profile
    public function profile(){
        return $this->hasOne('App\Profile','user_id','id')->withDefault();
    }
    
    //User hasmany document
    public function documents(){
        return $this->hasMany('App\UserDocument','user_id','id')->withDefault();
    }
    
    现在,如果您想访问控制器中的所有数据,则可以使用with()

    控制器

    App\User::with('city','profile','documents')->get();
    
    您将获得对象中的所有数据


    用户

    App\User::with('city','profile','documents')->get();
    
    身份证

    名字

    • {城市数据}

    • {profile}

    • {文件}


    此外,您还可以获取多个模型嵌套关系,例如城市属于州,如果您希望获取城市的州数据

    User::with('city.state','profile','documents')->get(): 
    //define state relationship in city model you'll get state data with the city as well.
    
    您还可以使用()在
    中添加条件


    它渴望加载关系以减少数据库调用-@aynber然后参数inside with method指的是什么?每个参数都是Test model.ya中定义的关系,在测试模型中有一些方法,我可以知道什么是reference和什么用途吗?它渴望加载关系以减少数据库调用-@aynber然后带方法的param inside reference是什么?每个参数都是Test model.ya中定义的一个关系,在Test model中有一些方法,我可以知道reference是什么和什么用途吗?你能帮我解决这个问题吗?thx你能帮我吗?谢谢