RSM RSM - 1 year ago 112
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()



Answer Source

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download