我正在使用Laravel 4来设置我的第一个模型,以从名为posts
的表中提取所有行。
在标准MySQL中,我会使用:
SELECT * FROM posts;
如何在我的 Laravel 4 模型中实现这一点?
请参阅下面的完整模型源代码:
<?php
class Blog extends Eloquent
{
/**
* The database table used by the model.
*
* @var string
*/
protected $table = 'posts';
public function getAllPosts()
{
}
}
你只需调用
Blog::all();
//example usage.
$posts = Blog::all();
$posts->each(function($post) // foreach($posts as $post) { }
{
//do something
}
从应用程序中的任何位置。
阅读文档将有很大帮助。
有 3 种方法可以做到这一点。
1 - 使用 all() 或 get();
$entireTable = TableModelName::all();
例如,
$posts = Post::get(); // both get and all will work here
或
$posts = Post::all();
2 - 使用 DB 外观
将此行放在控制器中的类之前
use IlluminateSupportFacadesDB; // this will import the DB facade into your controller class
现在在课堂上
$posts = DB::table('posts')->get(); // it will get the entire table
或者更动态的方式是——
$postTable = (new Post())->getTable(); // This will get the table name
$posts = DB::table($postTable)->get();
这种方式的优点是,如果您更改表名,它不会返回任何错误,因为它从Post
模型中动态获取表名。确保在顶部导入Post
模型,就像DB
法达德一样。
3 - 将数据库外观与选择
将此行放在控制器中的类之前
*Same import the DB facade like method 2*
现在在控制器中
$posts = DB::select('SELECT * FROM posts');
转到控制器,在函数中写下这个
public function index()
{
$posts = AppPost::all();
return view('yourview', ['posts' => $posts]);
}
为了显示它
@foreach($posts as $post)
{{ $post->yourColumnName }}
@endforeach
好吧,要雄辩地做到这一点,你会做:
Blog:all();
在模型中,您可以:
return DB::table('posts')->get();
http://laravel.com/docs/queries
如何使用laravel从数据库中获取所有数据以查看,我希望此解决方案对初学者有所帮助。
控制器内部
public function get(){
$types = select::all();
return view('selectview')->with('types', $types);}
在控制器内部导入数据模型,在我的应用程序中将数据模型命名为 select。
use AppSelect;
包括我的控制器看起来像这样
use AppSelect;
class SelectController extends Controller{
public function get(){
$types = select::all();
return view('selectview')->with('types', $types);}
选择型号
<?php
namespace App;
use IlluminateDatabaseEloquentModel;
class Select extends Model
{
protected $fillable = [
'name', 'email','phone','radio1','service',
];
protected $table = 'selectdata';
public $timestamps = false;
}
路由器内部
Route::get('/selectview', 'SelectController@get');
选择视图.刀片.php
@foreach($types as $type)
<ul>
<li>{{ $type->name }}</li>
</ul>
@endforeach
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);
这对我有用。
$posts = Blog::get()->all();
public function getAllPosts()
{
return Blog::all();
}
看看文档,这可能是他们解释的第一件事。
使用数据库外观可以执行SQL查询
public function index()
{
return DB::table('table_name')->get();
}
在Laravel Eloquent 中,您可以在控制器中给出以下查询,以从所需的表中获取所有数据:
$posts = Post::all();
return view('post', compact('posts'));
或
$posts = Post::orderBy('id')->get();
return view('post', compact('posts'));
$posts = DB::select('SELECT * FROM table_x');
或获取特定列:
$posts = DB::select('SELECT col_a, col_b, col_c FROM table_x');
如果你的表非常大,你也可以通过"小包"来处理行(不是全部在oce)(laravel doc:雄辩>分块结果)
Post::chunk(200, function($posts)
{
foreach ($posts as $post)
{
// process post here.
}
});