RSM RSM - 3 months ago 8
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

posts
.

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:

<?php

class Blog extends Eloquent
{

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

public function getAllPosts()
{

}

}

Answer

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 documentation will help a lot.

Comments