1

I want to show all values of two specified columns 'first_name' and 'pic' from my 'users' table. I am trying 'pluck' but it showing like json format when echo. But I need to show it, something - 'John pic' for all.Please, someone help me. Here is my sample code in 'index.blade.php' bellow -

<?php
$fName = DB::table('users')->pluck('first_name','pic');

echo $fName;
?>
Rashed Hasan
  • 3,721
  • 11
  • 40
  • 82
  • `echo $fName;` casts to json. Try `dd($fName);` to see a collection. pluck method generates array key and value pairs, useful when generating select options.. in this case `array('pic'=>'first_name');` – Emeka Mbah Aug 20 '17 at 06:11
  • you need array ??? `name1 => picture1` like this – Hamelraj Aug 20 '17 at 07:00

3 Answers3

2

Use you can simply use select as

$fName = DB::table('users')->select('first_name','pic')->get();

Since, direct access to database from view is not preferred, you can write this query in controller function,

public function getData(){
    $fName = DB::table('users')->select('first_name','pic')->get();
    return view('index',compact('fName'));
}

Now, iterate over loop in view file,

@foreach($fName as $name)
    // access data like {{$name->first_name}} and {{$name->pic}}
@endforeach

Hope you understand.

Sagar Gautam
  • 9,049
  • 6
  • 53
  • 84
  • $fName = DB::table('users')->select('first_name','last_name','pic')->get(); – Rashed Hasan Aug 20 '17 at 07:44
  • @Md.RashedulHasan you should use `` tag to display image and should have to add file location in src value – Sagar Gautam Aug 20 '17 at 07:46