Php 结果令人震惊。我在代码中忽略了这一点,不明白为什么找不到任何结果。我真傻!希望这能帮助其他有同样问题的人。很好,只需要添加外键,在这里它的post_id否则结果将是空的。这里提到外键很重要。谢谢:)这是我的收藏!如果需要数组,请调用toArray()$us

Php 结果令人震惊。我在代码中忽略了这一点,不明白为什么找不到任何结果。我真傻!希望这能帮助其他有同样问题的人。很好,只需要添加外键,在这里它的post_id否则结果将是空的。这里提到外键很重要。谢谢:)这是我的收藏!如果需要数组,请调用toArray()$us,php,laravel,orm,eloquent,laravel-query-builder,Php,Laravel,Orm,Eloquent,Laravel Query Builder,结果令人震惊。我在代码中忽略了这一点,不明白为什么找不到任何结果。我真傻!希望这能帮助其他有同样问题的人。很好,只需要添加外键,在这里它的post_id否则结果将是空的。这里提到外键很重要。谢谢:)这是我的收藏!如果需要数组,请调用toArray()$user->favorites->list('id')->toArray()查询仍将获取其他列,因为list()方法只是更改了结果数组,因此如果您只需要该表中的“id”,则可能需要在查询中指定它。在执行查询时,将性能牢记在心始终是一个好习惯。享受编



结果令人震惊。我在代码中忽略了这一点,不明白为什么找不到任何结果。我真傻!希望这能帮助其他有同样问题的人。很好,只需要添加外键,在这里它的post_id否则结果将是空的。这里提到外键很重要。谢谢:)这是我的收藏!如果需要数组,请调用
toArray()
<代码>$user->favorites->list('id')->toArray()查询仍将获取其他列,因为list()方法只是更改了结果数组,因此如果您只需要该表中的“id”,则可能需要在查询中指定它。在执行查询时,将性能牢记在心始终是一个好习惯。享受编码-1
$user->favorities
将返回
集合
,并选择所有字段。正确的用法是:
$user->favorites()->列表('id')
。选择所有内容以便以后使用PHP过滤是个坏主意。最好只使用查询中所需的字段。了解
Query\Builder::lists
方法和
Collection::lists
方法之间的区别很重要。是的,确切地说,“不要忘了包含外键(在本例中假设它是用户id)”,您应该在选择字段中包含定义关系的列,在这种情况下,user_idNice catch brother.awesome bro,外键的使用非常重要,幸运的是我看到了你的ans,否则我会抓挠我的头好几个小时哈哈。谢谢你!真正地对我来说,这只是不返回任何内容,即它破坏了它?别忘了添加外键字段别忘了注意@hendra1的注释,所有外键字段都是主键所必需的,否则您将得到空白集合。这家伙节省了我的时间。谢谢不要忘记包含外键(假设这里是post_id)来解析关系,否则您将得到关系的空结果。这应该是选择的答案。工作起来很有魅力:)@Adam,这将限制子表中的列,我如何才能限制表父表中的列以及子表中的列?@GauravGenius在
get()
method
Post::with('user:id,username')->get(['age',color'))中您想要限制的所有内容
@HashaamAhmed+1这与中的语法相同(只是模型和列的名称不同),这为我返回null,但我以前不知道这是可能的!好的,所以我贴了早产儿,但我确实找到了一个结果。要使用此方法(看起来更干净),您必须包含关系链接,例如,在使用数据拉取时包含ID列。如果没有此说明符,则返回空值。是的@Adsy2010,要获取相关关系数据,还必须获取id列或主键或负责该关系的id。@Kamlesh
Post::with('user:user.id,username')->get()此代码将返回post模型或表格的所有字段,并且仅返回
用户
表格中的
id
用户名
字段,但请记住,在使用
with()
函数之前,您应该与模型中的相关表格有关系,如果没有关系,则可以从查询生成器使用。
public function user(){
    return $this->belongsTo('user');
}
public function getAllPosts() {
    return Post::with('user')->get();
}
select * from `posts`
select * from `users` where `users`.`id` in (<1>, <2>)
select * from `posts`
select id,username from `users` where `users`.`id` in (<1>, <2>)
Post::with('user')->get(array('columns'....));
public function user()
{
    return $this->belongsTo('User')->select(['id', 'username']);
}
$favourites = $user->favourites->lists('id');
Array
(
    [0] => 3
    [1] => 7
    [2] => 8
)
User::with(array('post'=>function($query){
    $query->select('id','user_id');
}))->get();
public function userWithName()
{
    return $this->belongsTo('User')->select(array('id', 'first_name', 'last_name'));
}
App\Models\User::find(2)->posts->pluck('uuid')
=> Illuminate\Support\Collection {#983
     all: [
       "1",
       "2",
       "3",
     ],
   }
Post::with('user:id,username')->get();
Post::with('user:id,username,team_id')->get();
Post::with('user:id,username,post_id')->get();
$users = App\Book::with('author:id,name')->get();
Post::with(['user' => function ($query) {
        $query->select('id','company_id', 'username');
    }, 'user.company' => function ($query) {
        $query->select('id', 'name');
    }])->get();
Post::with(['user' => function ($query) {
        $query->select('id', 'username');
    }])
    ->select('title', 'content', 'user_id')
    ->get();
EmployeeGatePassStatus::with('user:id,name')->get();
$id = 1;
Post::with(array('user'=>function($query) use ($id){
    $query->where('id','=',$id);
    $query->select('id','username');
}))->get();
Post::with('user:id,username')->get();
Post::with('user:user.id,username')->get();
// For example you have this relation defined with "user()" method
public function user()
{
    return $this->belongsTo('User');
}
// Just make another one defined with "user_frontend()" method
public function user_frontend()
{
    return $this->belongsTo('User')->select(array('id', 'username'));
}

// Then use it later like this
$thing = new Thing();
$thing->with('user_frontend');

// This way, you get only id and username, 
// and if you want all fields you can do this

$thing = new Thing();
$thing->with('user');
Post::with(['user' => fn ($query) => $query->select('id','username')])->get();
 public function getSection(Request $request)
{

  Section::with(['sectionType' => function($q) {
      $q->select('id', 'name');
  }])->where('position',1)->orderBy('serial_no', 'asc')->get(['id','name','','description']);
  return response()->json($getSection);
}
public function sectionType(){
    return $this->belongsTo(Section_Type::class, 'type_id');
}
public function show(Post $post)
    {
       $posts = $post->has('user')->with('user:id,name,email,picture')->findOrFail($post->id);

        return view('your_blade_file_path',compact('posts);
    }
  public function user()
    {
        return $this->belongsTo( User::class, 'user_id')->withDefault();
    }