YouTube Icon

Code Playground.

How to retrieving a list of column values in Laravel 5

CFG

How to retrieving a list of column values in Laravel 5

If you want a list containing a single column values to be retrieved, you can use the pluck process.

Let's build a laravel 5 pluck method

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

class PostController extends Controller
{
    public function addPost()
    {
        $titles = DB::table('tbl_add_post')->pluck('title');
        foreach ($titles as $title) {
           echo $title;
        }
    }
}

You may also specify a custom key column for the returned Collection.

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

class PostController extends Controller
{
    public function addPost()
    {
        $titles = DB::table('tbl_add_post')->pluck('title','slug');
        foreach ($titles as $slug => $title) {
            echo $slug;
        }           
    }
}




CFG