0
/* Place uploaded images into appropriate columns. */
for($i = 1; $i <= 5; $i++)
{
    if(Input::file('image' . $i . '.size') >= 1)
    {
        $randomName = substr( md5( rand(1,9999999) ), 1, 15);
        Input::upload('image' . $i, path('public') . 'uploads/backgrounds/', Auth::user()->username . $randomName . '.jpg');

        $wedding= Wedding::where('wedding_owner', '=', Auth::user()->username)->first();
            $wedding->image1 = $randomName;
        $wedding->save();
    }
}

User may upload 5 pictures. The uploaded images should be placed in image1, image2, image3, image4 and image5 columns in wedding table.

Basically,

$wedding->image1 = $randomName;

Should be something like:

$wedding->image{$i} = $randomName;

How can I solve this?

3
  • Having enumerated fields names always smells of a bad design Commented May 12, 2013 at 14:02
  • Why not take all values in loop & than save at once ? Commented May 12, 2013 at 14:03
  • @Rikesh Care to elaborate a bit? Commented May 12, 2013 at 14:04

1 Answer 1

2

Concat the name inside the {} :

$wedding->{'image' . $i} = $randomName;

Thus you can add dynamic fields/properties to an object/stdClass instance.

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

5 Comments

This is the answer. What's the meaning of {} in PHP? I never use them, but I often see people using { } between " " brackets.
This is not. An array have to be used instead
@Imaqtpie There are various uses of {}, but here it allows you to use dynamic property name.
@YourCommonSense Would like to hear why it shouldn't work, have you tested?
Here are some: stackoverflow.com/q/2596837/656489 , Googling will show other uses.

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.