Select all from table with Laravel and Eloquent

PhpMysqlLaravelLaravel 4Eloquent

Php Problem Overview


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()
	{
		
	}

}

Php Solutions


Solution 1 - Php

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.

Solution 2 - Php

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

Solution 3 - Php

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

Solution 4 - Php

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

Solution 5 - Php

> 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
     

Solution 6 - Php

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

Solution 7 - Php

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

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

Solution 8 - Php

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

Solution 9 - Php

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

Solution 10 - Php

This worked for me.

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

Solution 11 - Php

using DB facade you can perform SQL queries

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

Solution 12 - Php

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionRSMView Question on Stackoverflow
Solution 1 - PhpSturmView Answer on Stackoverflow
Solution 2 - PhpKoushik DasView Answer on Stackoverflow
Solution 3 - PhpChandoView Answer on Stackoverflow
Solution 4 - PhpKolbyView Answer on Stackoverflow
Solution 5 - PhpPoornima Subramani NaiduView Answer on Stackoverflow
Solution 6 - PhpReetesh GuptaView Answer on Stackoverflow
Solution 7 - PhpLucky SoniView Answer on Stackoverflow
Solution 8 - PhpHedayatullah SarwaryView Answer on Stackoverflow
Solution 9 - PhpMahan MashoofView Answer on Stackoverflow
Solution 10 - Phpuser2325149View Answer on Stackoverflow
Solution 11 - PhpGuaracy A. LimaView Answer on Stackoverflow
Solution 12 - PhpKamil KiełczewskiView Answer on Stackoverflow