简体   繁体   English

get()不起作用,但是First()起作用。 我需要使用get()获取所有数据

[英]get() doesn't work, but First() does. I need to use get() to get all my data

I have a page that shows categories/main posts which when clicked will navigate to a page with sub-posts concerning the topic of the main post. 我有一个显示类别/主要帖子的页面,单击该页面将导航到包含有关主要帖子主题的子帖子的页面。 The problem is, however, that when I try to use ->get() It shows the following error: 问题是,但是,当我尝试使用-> get()时,它显示以下错误: 糟糕,发生错误

The function I use to get the information is the following (URL Shows as slug): 我用来获取信息的函数如下(URL显示为“ slug”):

public function knowledge_collection($slug, Request $request){
    $knowledge_post_primary = Knowledge::where('knowledge_slug', $slug)->get();
    //dd($knowledge_post_primary);
    $knowledge_posts = Knowledge_Post::where('parent_id', $knowledge_post_primary->id)->get();
    return view('knowledge.knowledge_posts', compact('knowledge_posts', 'knowledge_post_primary'));
}

It's most likely an easy fix, but I can't seem to find it. 这很可能是一个简单的修复程序,但我似乎找不到。 I hope someone else can and will be able to help me :) 我希望其他人能够并且能够帮助我:)

I've tried requesting the data in different ways and with a FindorFail(), but nothing worked up till now. 我尝试过用FindorFail()以不同的方式请求数据,但到目前为止没有任何进展。

EDIT: The screenshot that was asked for. 编辑:要求的屏幕截图。 yeet

EDIT EDIT: After using @Gulshans code I got the following error: 编辑:使用@Gulshans代码后,出现以下错误: ii刀片错误

Try this for single result: 尝试以下结果:

$knowledge_post_primary = Knowledge::where('knowledge_slug','=',$slug)->first()->toArray();
if(!empty($knowledge_post_primary)){
   $knowledge_posts = Knowledge_Post::where('parent_id', '=',$knowledge_post_primary['id'])->get();
}

Try this for multiple result: 尝试以下方法以得到多个结果:

 $knowledge_post_primary = Knowledge::where('knowledge_slug','=',$slug)->get()->toArray();
 if(!empty($knowledge_post_primary)){
       foreach($knowledge_post_primary as $post_primary){
       $knowledge_posts = Knowledge_Post::where('parent_id', '=',$post_primary['id'])->get()->toArray();
          dd($knowledge_posts);
       }
  }

If you want to get Knowledge_Post for the knowledge_slug and if there is only one Knowledge record you should use 如果您想获取Knowledge_slug的Knowledge_Post,并且只有一条知识记录,则应该使用

public function knowledge_collection($slug, Request $request){
    $knowledge_post_primary = Knowledge::where('knowledge_slug', $slug)->first();
    $knowledge_posts = Knowledge_Post::where('parent_id', $knowledge_post_primary->id)->get();
    return view('knowledge.knowledge_posts', compact('knowledge_posts', 'knowledge_post_primary'));
}

if there is many records and want Knowledge_Posts related to all it will be whereIn, to get all ids, you should use pluck('id')->all() or pluck('id')->toArray() 如果有很多记录并且想要与所有相关的Knowledge_Posts在whereIn,要获取所有ID,则应使用pluck pluck('id')->all()pluck('id')->toArray()

public function knowledge_collection($slug, Request $request){
        $knowledge_post_primary_Ids = Knowledge::where('knowledge_slug', $slug)->pluck('id')->all();
        $knowledge_posts = Knowledge_Post::whereIn('parent_id', $knowledge_post_primary_Ids)->get();
        return view('knowledge.knowledge_posts', compact('knowledge_posts', 'knowledge_post_primary'));
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM