How to retrieving all rows from a table in Laravel 5

We can retrieve all record in table to using table method on the DB facade to begin a query. Several methods to connect like, get(), first() etc, that helps to get table record using table method.

Let’s create a table method in laravel 5

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Http\Requests;
use DB;

class PostController extends Controller
    public function addPost()
        $getpost = DB::table('tbl_post')->get();
        return view('viewpost', ['getpost' => $getpost]);

The get method returns an Illuminate\Support\Collection containing the results where each result is an instance of the PHP StdClass object.

foreach ($getpost as $post) {
    echo $post->title;

Leave a Reply

Your email address will not be published. Required fields are marked *