YouTube Icon

Code Playground.

How to retrieving all rows from a table in Laravel 5

CFG

How to retrieving all rows from a table in Laravel 5

To start a query, we can collect all records in the table using table method on the DB façade. Many ways to link such as get(), first() etc, which helps to record the table using the 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;
}




CFG