Select all from table with Laravel and Eloquent
Solution 1
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
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
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
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
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
RSM
Updated on October 16, 2021Comments
-
RSM over 2 years
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() { } }
-
Sturm about 10 yearsWhy not just $this->all()? That seems really roundabout.
-
beporter almost 7 yearsBe careful using
::all()
as it reads everything from the table into memory. If you table has many records, you can quickly exceed PHP's memory limit that way. The solution is apparently to useBuilder::cursor()
orBuilder::chunk()
. -
Elshan almost 6 yearsYou will get 500 error message with this
Blog::all();
if you reach maximum memory limit with apache -
Drewster almost 6 yearsSlight update to the code there: you need closing brackets and semicolon so: $posts->each(function($post) { //do something });
-
Nick about 5 yearsWelcome to SO. Code-only answers are improved by including an explanation of how you’ve answer addresses the question.
-
VishalParkash almost 4 yearsany comments on how to check if the $posts is empty.
-
Kamil Kiełczewski almost 3 years@Kamlesh write you comment as separate question on stack overflow - with more details
-
Morris almost 3 yearsThis goes against the MVC approach that Laravel takes. It will work but i think this is bad practice
-
Mahan Mashoof almost 3 yearsThanks Morris, please feel free to enhance the method
-
Morris almost 3 yearsWell it's simple. Instead of making queries inside of your controller just call your model and let it execute database commands for you.