php 使用 Laravel 和 Eloquent 从表中选择所有内容
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21891815/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Select all from table with Laravel and Eloquent
提问by RSM
I am using Laravel 4 to set up my first model to pull all the rows from a table called posts.
我正在使用 Laravel 4 来设置我的第一个模型,以从名为posts.
In standard MySQL I would use:
在标准 MySQL 中,我会使用:
SELECT * FROM posts;
How do I achieve this in my Laravel 4 model?
我如何在我的 Laravel 4 模型中实现这一点?
See below for my complete model source code:
请参阅下面的我的完整模型源代码:
<?php
class Blog extends Eloquent
{
/**
* The database table used by the model.
*
* @var string
*/
protected $table = 'posts';
public function getAllPosts()
{
}
}
回答by Sturm
You simply call
你只需打电话
Blog::all();
//example usage.
$posts = Blog::all();
$posts->each(function($post) // foreach($posts as $post) { }
{
//do something
}
from anywhere in your application.
从您的应用程序的任何地方。
Reading the documentationwill help a lot.
阅读文档会有很大帮助。
回答by Koushik Das
There are 3 ways that one can do that.
有 3 种方法可以做到这一点。
1.
1.
$entireTable = TableModelName::all();
eg,
例如,
$posts = Posts::get();
put this line before the class in the controller
use Illuminate\Support\Facades\DB; // this will import the DB facade into your controller class
将此行放在控制器中的类之前
使用 Illuminate\Support\Facades\DB; // 这会将 DB Facade 导入到您的控制器类中
Now in the class
现在在课堂上
$posts = DB::table('posts')->get(); // it will get the entire table
put this line before the class in the controller
Same import the DB facade like method 2
将此行放在控制器中的类之前
与方法 2 一样导入 DB 外观
Now in the controller
现在在控制器中
$posts = DB::select('SELECT * FROM posts');
回答by Chando
go to your Controller write this in function
去你的控制器写这个功能
public function index()
{
$posts = \App\Post::all();
return view('yourview', ['posts' => $posts]);
}
in view to show it
为了展示它
@foreach($posts as $post)
{{ $post->yourColumnName }}
@endforeach
回答by Kolby
Well, to do it with eloquent you would do:
好吧,要用 eloquent 做到这一点,你会这样做:
Blog:all();
From within your Model you do:
在您的模型中,您可以:
return DB::table('posts')->get();
回答by Poornima Subramani Naidu
How to get all data from database to view using laravel, i hope this solution would be helpful for the beginners.
如何使用laravel从数据库中获取所有数据以查看,我希望这个解决方案对初学者有所帮助。
Inside your controller
在你的控制器里面
public function get(){
$types = select::all();
return view('selectview')->with('types', $types);}
Import data model inside your controller, in my application the data model named as select.
在您的控制器中导入数据模型,在我的应用程序中,数据模型命名为 select。
use App\Select;
Inclusive of both my controller looks something like this
包括我的控制器看起来像这样
use App\Select;
class SelectController extends Controller{
public function get(){
$types = select::all();
return view('selectview')->with('types', $types);}
select model
选择型号
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Select extends Model
{
protected $fillable = [
'name', 'email','phone','radio1','service',
];
protected $table = 'selectdata';
public $timestamps = false;
}
inside router
路由器内部
Route::get('/selectview', 'SelectController@get');
selectview.blade.php
selectview.blade.php
@foreach($types as $type)
<ul>
<li>{{ $type->name }}</li>
</ul>
@endforeach
回答by Reetesh Gupta
Query
// Select all data of model table
Model::all();
// Select all data of model table
Model::get();
Model::where('foo', '=', 'bar')->get();
Model::find(1);
Model::find([1, 2, 3]);
Model::findOrFail(1);
回答by user2325149
This worked for me.
这对我有用。
$posts = Blog::get()->all();
回答by Guaracy A. Lima
回答by Kamil Kie?czewski
If your table is very big, you can also process rows by "small packages" (not all at oce) (laravel doc: Eloquent> Chunking Results)
如果您的表非常大,您还可以通过“小包”(并非全部在 oce 中)处理行(laravel 文档:Eloquent > Chunking Results)
Post::chunk(200, function($posts)
{
foreach ($posts as $post)
{
// process post here.
}
});

