Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/laravel/11.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/ssh/2.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
当我使用find()时,为什么laravel上的get()方法返回倍数_Laravel_Orm_Eloquent - Fatal编程技术网

当我使用find()时,为什么laravel上的get()方法返回倍数

当我使用find()时,为什么laravel上的get()方法返回倍数,laravel,orm,eloquent,Laravel,Orm,Eloquent,嗨,我写这行是为了得到一篇特定的文章,但是当我在find()之后使用get()时,它会返回大约30篇文章,尽管我使用find()方法。。 $post=post::find(5)--效果很好 $post=post::find(5)->get()!!!30个职位 我知道get()返回多条记录,但我希望当我使用find()时,它只能得到一篇文章。有人能解释一下它是如何工作的吗?您不需要使用get()和find() 鉴于 Post::find(5)->get(); 工作原理与 Post::all

嗨,我写这行是为了得到一篇特定的文章,但是当我在find()之后使用get()时,它会返回大约30篇文章,尽管我使用find()方法。。 $post=post::find(5)--效果很好 $post=post::find(5)->get()!!!30个职位
我知道get()返回多条记录,但我希望当我使用find()时,它只能得到一篇文章。有人能解释一下它是如何工作的吗?

您不需要使用
get()
find()

鉴于

Post::find(5)->get();
工作原理与

Post::all()

您不需要将
get()
find()

鉴于

Post::find(5)->get();
工作原理与

Post::all()

get方法返回相关模型的集合

如果您这样做:

Post::find(1)->get();
Post::find(30)->get()->find(1);
您将获得与id为1的帖子相关的帖子集合

如果您这样做:

Post::find(1)->get();
Post::find(30)->get()->find(1);

您将获得该集合的第一篇文章,因为get方法返回id为30的文章所属的文章集合。

get方法返回所讨论模型的集合

If you use Post::find(1)
it find record that have id 1 and return only that object not collection

but if you use get() then it return collection

in this case

Post::find(1)->get();
it is getting single data with find(1) but after that it run get() so it get all collection of database.
if you want to know how you can simply try by using get() without using find
Post::get(); is equivalent to Post::find(1)->get() or any number

Post::find(1)->get() find(1) is useless here
如果您这样做:

Post::find(1)->get();
Post::find(30)->get()->find(1);
您将获得与id为1的帖子相关的帖子集合

如果您这样做:

Post::find(1)->get();
Post::find(30)->get()->find(1);

您将获得该集合的第一篇文章,因为get方法返回id为30的文章所属的文章集合。

如果您只需要一条记录,可以尝试first()而不是get()。如果您只需要一条记录,可以尝试first()而不是get(),则第二条和第三条不会返回相同的结果
all()
返回表中的每条记录。第二条和第三条不会返回相同的记录
all()
返回表中的每条记录。非常感谢非常感谢
If you use Post::find(1)
it find record that have id 1 and return only that object not collection

but if you use get() then it return collection

in this case

Post::find(1)->get();
it is getting single data with find(1) but after that it run get() so it get all collection of database.
if you want to know how you can simply try by using get() without using find
Post::get(); is equivalent to Post::find(1)->get() or any number

Post::find(1)->get() find(1) is useless here