Answers for "laravel pluck relationship"

PHP
3

when to use pluck method in laravel

// QUESTION
When We should use Pluck method in laravel???

// ANSWER
You might often run into a situation where you have to 
extract certain values (excluding the keys) from a collection 
then you should use pluck().
i.e (when you only need value, not the key)
  
//Example 1
let we have a list of results and we only need the value of one colum

$attendees = collect([
    ['name' => 'Bradmen', 'email' => '[email protected]', 'city' => 'London'],
    ['name' => 'Jhon Doe', 'email' => '[email protected]', 'city' => 'paris'],
    ['name' => 'Martin', 'email' => '[email protected]', 'city' => 'washington'],
]);

$names = $attendees->pluck('name')
//Reult ['Bradmen', 'Jhon Doe', 'Martin'];

//Example 2
OR You can use like this
  
$users = User::all();
$usernames = $users->pluck('username');
Posted by: Guest on January-28-2021
0

laravel pluck relationship

Seller::with('user')->get()->pluck('user.first_name', 'id')
Posted by: Guest on August-30-2021
0

pluck laravel

$name = DB::table('users')->where('name', 'John')->pluck('name');
Posted by: Guest on January-18-2021
0

laravel pluck example

DB::table('users')->where('id', 1)->pluck('name')->first();
Posted by: Guest on June-29-2021
0

laravel pluck

$users = Users::pluck('name','email');
dd($users);
Posted by: Guest on October-05-2020

Browse Popular Code Answers by Language