lucassimines's avatar

Problem to unserialize my returned value

Hello guys, I serialized a form input named "items[]", but I can't unserialize it, so the value that is being returned is: a:2:{i:0;s:5:"item1";i:1;s:5:"item2";}

How can I handle with it and make it return:

Items:

  • item1
  • item2

Thanks!

0 likes
12 replies
Snapey's avatar

Is there more to it than unserialize?

lucassimines's avatar

No, I need to unserialize only because I'm creating a repeating field.

Snapey's avatar

Tinker;

>>> $t=['item1'=>1,'item2'=>2]
=> [
     "item1" => 1,
     "item2" => 2,
   ]
>>> $s=serialize($t)
=> "a:2:{s:5:"item1";i:1;s:5:"item2";i:2;}"
>>> unserialize($s)
=> [
     "item1" => 1,
     "item2" => 2,
   ]
>>>
lucassimines's avatar

Thanks for the Help @Snapey , but, I'll have to make a controller for my "items" value return unserialized in my frontend page? Because when I use {{ product->items }} it returns the serialized value.

Is there something I need to put in my compact?


    return view('admin.products.index', compact('products'));

Snapey's avatar

I can't really follow you because I don't know why you chose to serialize it in the first place, but I would have thought the obvious answer;

$productsArray = unserialize($products);

return view('admin.products.index', compact('productsArray'));
lucassimines's avatar

@Snapey I'm serializing it to store the array inside my table #products, so I have:


Collection {#221 ▼
  #items: array:1 [▼
    0 => Product {#222 ▼
      #fillable: array:4 [▶]
      #connection: null
      #table: null
      #primaryKey: "id"
      #perPage: 15
      +incrementing: true
      +timestamps: true
      #attributes: array:6 [▶]
      #original: array:6 [▼
        "id" => "1"
        "title" => "prod"
        "desc" => "sjhfkjhkj"
        "items" => "a:2:{i:0;s:6:"item 1";i:1;s:6:"item 2";}"
        "created_at" => "2016-03-28 19:20:39"
        "updated_at" => "2016-03-28 19:20:39"
      ]
      #relations: []
      #hidden: []
      #visible: []
      #appends: []
      #guarded: array:1 [▶]
      #dates: []
      #dateFormat: null
      #casts: []
      #touches: []
      #observables: []
      #with: []
      #morphClass: null
      +exists: true
      +wasRecentlyCreated: false
    }
  ]
}

But I'm trying to get each "items" of each product and unserialize them, but it's returning the following on frontend:

Items

a:2:{i:0;s:6:"item 1";i:1;s:6:"item 2";}

--

Do I have to unserialize it in my index function, that's called by my index page?


public function index()
    {
        $products = $this->product->paginate(5);
        return view('admin.products.index', compact('products'));
    }
lucassimines's avatar

@premsaurav WOW! Got it! And made it work. SO I don't need to serialize, when I put inside $casts it does what I need, thanks for the answer! Thanks @Snapey for the help too!

Snapey's avatar

Serializing in this way is a bit of a code smell. Really you should have a separate model for the items and a one to many relationship.

1 like
lucassimines's avatar

@Snapey yea, I'm studying Laravel and getting how it really works, I'll keep reading the models and relationships.

davestewart's avatar

Also, you can cast as JSON, which would at least make your field more readable in the DB

d3xt3r's avatar

I agree with @Snapey. This should not be used for storing relationship ... (you will face significant overhead while maintaining it) ... But for me , I showed you a general way that you don't need to do all this manually, laravel can do this elegantly for you .. be it json or array ...

Please or to participate in this conversation.