Reputation: 15108
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
Reputation: 10793
There are 3 ways that one can do that.
$entireTable = TableModelName::all();
eg,
$posts = Post::get(); // both get and all will work here
or
$posts = Post::all();
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.
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
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
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
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
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
Reputation: 32
using DB facade you can perform SQL queries
public function index()
{
return DB::table('table_name')->get();
}
Upvotes: 1
Reputation: 393
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
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
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
Reputation: 6878
public function getAllPosts()
{
return Blog::all();
}
Have a look at the docs this is probably the first thing they explain..
Upvotes: 1
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