动态引用类属性

时间:2016-11-23 11:03:27

标签: php laravel

for ($e = 1; $e <= 10; $e++)
{
    $edd = Setting::where('id', '=', $e)->update([
      'nama' => $request->nama1,
      'isi'  => $request->isi1,
    ]);
}

如何在for循环中动态引用类属性?我想在nama1对象上引用isi1nama10最多isi10$request

1 个答案:

答案 0 :(得分:0)

for ($e = 1; $e <= 10; $e++)
{
    $edd = Setting::where('id', '=', $e)->update([
      'nama' => $request->{"nama$e"},
      'isi'  => $request->{"isi$e"},
    ]);
}

为了更具可读性,您可能希望这样做:

$request->{"nama{$e}"} // Notice the inner braces around $e