Carbon.php The separation symbol could not be found Data missing
Asked Answered
P

2

13

First, I retrieve all the records,

//get inventory items
$inv = inventory::all();

and then I loop on the retrieved records and modify the created_at and updated_at data to make it more human readable date.

foreach($inv as $i){
    $i->created_at = date("M d, Y",strtotime($i->created_at));
    $i->updated_at = date("M d, Y",strtotime($i->updated_at));
}

but it returns me this error,

InvalidArgumentException in Carbon.php line 425: Unexpected data found. Unexpected data found. The separation symbol could not be found Data missing

any ideas, help, clues, suggestions, recommendations please?

here's my model

namespace App;

use Illuminate\Database\Eloquent\Model;

class inventory extends Model
{
    protected $table = "inventory";
    protected $primaryKey = "item_id";
    public $incrementing = false;

    public function profile(){
        return $this->belongsTo('App\profile','username');
    }
    public function inventory_images(){
        return $this->hasMany('App\inventory_images','item_id');
    }
}

and in blade, I can just use

{{ date("M d, Y",strtotime($i->created_at)) }}

{{ date("M d, Y",strtotime($i->updated_at)) }}

and it work just fine.

Portage answered 22/6, 2016 at 2:34 Comment(8)
Did you happen to do something fancy with your inventory model to make it handle date properties with Carbon?Longsome
@mopo922: no I haven't sirPortage
Is there more code that you're not showing, where you use Carbon for something?Longsome
@mopo922: I didn't use any carbon or such, in the blade template, it render just fine but when I try to do it in my controller, it gives me that error.Portage
Looks like it may happen automatically: #27181509 Can you plz share the code in your view where you print these dates?Longsome
it has something to do with your date format, what date format are you using in your model? From the error, it seems that in your model you have a date format which differs from the one you pass inBinomial
@mopo922: please see my updated post.Portage
@SteD: please my updated postPortage
M
17

I think you're going about this the wrong way. The data in your database doesn't need to be more human readable, only the display that a human actually interacts with.

To solve this, we will create a custom accessor method that will apply to all calls for the created_at. You can recreate this for the updated_at.

public function getCreatedAtAttribute($timestamp) {
    return Carbon\Carbon::parse($timestamp)->format('M d, Y');
}

Then when you call $model->created_at your attribute will return in that format.

If for some reason you absolutely need the date stored in that format, then you need to add an attribute to your model telling it that the timestamp columns should be formatted according to a specific type, such as:

protected $dateFormat = 'M d, Y';

Sidenote
The reason that Carbon is involved is that it is applied to all of the columns generated by $table->timestamps(), so the created_at and updated_at columns.
Furthemore, if you add more columns in the model to the protected $dates = [] array, those will also automagically be handled by Carbon.

Myxoma answered 22/6, 2016 at 4:23 Comment(5)
I have added "public function getCreatedAtAttribute($timestamp) { return $timestamp->format('M d, Y'); }" but it returns me an error "Call to a member function format() on a non-object" and pointing on the line 20 which is the "return $timestamp->format('M d, Y');" any ideas?Portage
@CodeDemon That's true, Do return Carbon::parse($timestamp)->format('M d, Y')Myxoma
I have no carbon installed in my laravel 5.2 app and I don't want to install that extension yet at this moment, any alternative way without involving carbon?Portage
@CodeDemon It's prebundled with Laravel. You can't avoid it use Carbon\Carbon. You can also just simply format the string as you'd like using your method if for some reason you want to avoid using Carbon, which is baked in.Myxoma
@Ohgodwhy, where should I add the accessor method and that too for one particular column of the table.?Intervene
F
2

So simple error please do like this

check your date format (d-m-Y) then use the same format

$shipDate = Carbon::createFromFormat('d-m-Y H:i',Carbon::parse($request->date)->format('d-m-Y') . " " . $request->time);
Freeloader answered 8/2, 2022 at 16:58 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.