RSM
RSM

Reputation: 15108

Select all from table with Laravel and Eloquent

I am using Laravel 4 to set 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 source code:

<?php

class Blog extends Eloquent 
{

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

    public function getAllPosts()
    {

    }

}

Upvotes: 75

Views: 412654

Answers (12)

Koushik Das
Koushik Das

Reputation: 10793

There are 3 ways that one can do that.

1 - Use all() or get();

$entireTable = TableModelName::all();

eg,

$posts = Post::get(); // both get and all  will work here

or

$posts = Post::all();

2 - Use the DB facade

Put this line before the class in the controller

use Illuminate\Support\Facades\DB; // this will import the DB facade into your controller class

Now in the class

$posts = DB::table('posts')->get(); // it will get the entire table

or a more dynamic way would be -

$postTable = (new Post())->getTable(); // This will get the table name
$posts = DB::table($postTable)->get();

The advantage of this way is that in case you ever change the table name, it would not return any error since it gets the table name dynamically from the Post model. Make sure to import Post model at the top like DB fadade.

3 - Use the DB facade with select

Put this line before the class in the controller

*Same import the DB facade like method 2*

Now in the controller

$posts = DB::select('SELECT * FROM posts');

Upvotes: 54

Mahan Mashoof
Mahan Mashoof

Reputation: 169

$posts = DB::select('SELECT * FROM table_x');

or to get specific columns:

$posts = DB::select('SELECT col_a, col_b, col_c FROM table_x');

Upvotes: 1

Hedayatullah Sarwary
Hedayatullah Sarwary

Reputation: 2834

In Laravel Eloquent you can give the below queries in your controller to get all the data from your desired table:

$posts = Post::all();
return view('post', compact('posts'));

Or

$posts = Post::orderBy('id')->get();
return view('post', compact('posts'));

Upvotes: 1

Reetesh Gupta
Reetesh Gupta

Reputation: 31

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);

Upvotes: 3

Kamil Kiełczewski
Kamil Kiełczewski

Reputation: 92367

If your table is very big, you can also process rows by "small packages" (not all at oce) (laravel doc: Eloquent> Chunking Results )

Post::chunk(200, function($posts)
{
    foreach ($posts as $post)
    {
        // process post here.
    }
});

Upvotes: 0

Guaracy A. Lima
Guaracy A. Lima

Reputation: 32

using DB facade you can perform SQL queries

 public function index()
{
    return DB::table('table_name')->get();
}

Upvotes: 1

user2325149
user2325149

Reputation: 44

This worked for me.

$posts = Blog::get()->all();

Upvotes: 2

How to get all data from database to view using laravel, i hope this solution would be helpful for the beginners.

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.

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

@foreach($types as $type)
    <ul>
    <li>{{ $type->name }}</li>
    </ul>

    @endforeach

Upvotes: 4

Chando
Chando

Reputation: 434

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

Upvotes: 10

Kolby
Kolby

Reputation: 2865

Well, to do it with eloquent you would do:

Blog:all();

From within your Model you do:

return DB::table('posts')->get();

http://laravel.com/docs/queries

Upvotes: 6

Lucky Soni
Lucky Soni

Reputation: 6878

 public function getAllPosts()
 {
   return  Blog::all();        
 }

Have a look at the docs this is probably the first thing they explain..

Upvotes: 1

Sturm
Sturm

Reputation: 4285

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.

Upvotes: 135

Related Questions