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;
?>
2
  • 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'); Commented Aug 20, 2017 at 6:11
  • you need array ??? name1 => picture1 like this Commented Aug 20, 2017 at 7:00

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

8 Comments

Here is an error when iterate over loop in view file
I am using sentinel package
You have error in controller code so inpect and view error preview in network
Now, It's showing just pic name, not showing the image
Can you add code you have write to display your image ?
|
1

You can use this code for retrieving specific column value:

//Eloquent: Get specific columns (not all the row). Pluck returns an array.
\App\Model::where('id', 1)->pluck('first_name','pic');
// If you only want to get the result value:
\App\Model::where('id', 1)->value('first_name');

Comments

0

You can get specific columns from a model using get() method, like

PostModel::where('post_status', 'publish')->get(['title', 'content', 'slug', 'image_url'])

pass the list of columns to get([...]) it will retrieve the modal data with specified columns.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.