Asked  2 Years ago    Answers:  5   Viewed   86 times

I'm trying to upgrade my project L5.1 -> L5.2. In upgrade guide there's one thing which isn't clear for me:

The lists method on the Collection, query builder and Eloquent query builder objects has been renamed to pluck. The method signature remains the same.

That's ok, rename refactoting from lists() to pluck() isn't a problem. But what with useful pluck() method which was in L5.0 and L5.1?

From the 5.0 documentation:

Retrieving A Single Column From A Row

$name = DB::table('users')->where('name', 'John')->pluck('name');

What is the alternative for old pluck() method in L5.2?

UPDATE:

Example:

var_dump(DB::table('users')->where('id', 1)->pluck('id'));

L5.1:

// int(1)

L5.2:

// array(1) { [0]=> int(1) }

 Answers

5

The current alternative for pluck() is value().

Thursday, December 15, 2022
 
5

If you're on Laravel 5.2 and using MySQL, there was a bit of a "bug" introduced with the timestamps. You can read all about the issue on github here. It has to do with the timestamp defaults, and MySQL automatically assigning DEFAULT CURRENT_TIMESTAMP or ON UPDATE CURRENT_TIMESTAMP attributes under certain conditions.

Basically, you have three options.

  1. Update MySQL variable:

If you set the explicit_defaults_for_timestamp variable to TRUE, no timestamp column will be assigned the DEFAULT CURRENT_TIMESTAMP or ON UPDATE CURRENT_TIMESTAMP attributes automatically. You can read more about the variable here.

  1. Use nullable timestamps:

Change $table->timestamps() to $table->nullableTimestamps(). By default, the $table->timestamps() command creates timestamp fields that are not nullable. By using $table->nullableTimestamps(), your timestamp fields will be nullable, and MySQL will not automatically assign the first one the DEFAULT CURRENT_TIMESTAMP or ON UPDATE CURRENT_TIMESTAMP attributes.

  1. Define the timestamps yourself:

Instead of using $table->timestamps, use $table->timestamp('updated_at'); $table->timestamp('created_at'); yourself. Make sure your 'updated_at' field is the first timestamp in the table, so that it will be the one that is automatically assign the DEFAULT CURRENT_TIMESTAMP or ON UPDATE CURRENT_TIMESTAMP attributes.

Saturday, October 22, 2022
1

I ended up coding my own Auth system...

Using session() and Input()

Wednesday, November 16, 2022
 
2

Try something like this, it should work using map...

return $TheEpisode->TheNumbers->map(function ($episode) {
    return ['episodeNumber' => $episode['episodeNumber']];
});
Friday, September 30, 2022
 
dev_r
 
3

You can use the map operation to reformat your date as your given format :

public function index() {
 
    $campaigns_dates = CampaignHistory::orderBy('created_at', 'ASC')
        ->pluck('created_at')
        ->map
        ->format('Y-m-d H:i:s');

    $campaigns_dates = json_decode($campaigns_dates);
    return $campaigns_dates;
}

Alternative Solution : There is an alternative solution with selectRaw() method, thanks to @OMR for provide this solution :

public function index() {
 
    $campaigns_dates = CampaignHistory::orderBy('created_at', 'ASC')
        ->selectRaw("DATE_FORMAT(created_at, '%Y-%m-%d %H:%i:%s') as formatted_date")
        ->pluck('formatted_date');

    $campaigns_dates = json_decode($campaigns_dates);
    return $campaigns_dates;
}
Sunday, November 27, 2022
 
owen_b
 
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :
 

Browse Other Code Languages