RSM RSM - 2 months ago 6x
MySQL Question

Select all from table with laravel and eloquent

I am using Laravel 4 setting up my first model to pull all the rows from a table called


In standard mysql I would use:

SELECT * FROM posts;

How do I achieve this in my Laravel 4 model?

See below for my complete model code:


class Blog extends Eloquent

* The database table used by the model.
* @var string
protected $table = 'posts';

public function getAllPosts()




You simply call


//example usage.
$posts = Blog::all();

$posts->each(function($post) // foreach($posts as $post) { }
    //do something

from anywhere in your application.

Reading the documentation will help a lot.