如何为名为数组的数据库列添加变量值?

时间:2013-05-12 13:59:27

标签: php laravel

/* 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();
    }
}

用户可以上传5张图片。上传的图片应放在image1, image2, image3, image4 and image5 columns中的wedding table

基本上,

$wedding->image1 = $randomName;

应该是这样的:

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

我该如何解决这个问题?

1 个答案:

答案 0 :(得分:2)

Concat {}中的名称:

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

因此,您可以向object / stdClass实例添加动态字段/属性。