Laravel 正在从输入数组中检索元素
Laravel retrieving element from input array
我得到一个包含匹配变量的隐藏字段。
{{ Form::input('hidden', 'match', $match, ['id' => 'match']) }}
如何在我的存储方法中检索 'home' 字段等?
public function store(Request $request)
{
Log::info($request->match);
Ticket::create([
'home' => $request->match->home,
'away' => $request->away,
'place' => $request->place,
'price' => $request->price,
'section' => $request->section,
'amount' => $request->amount,
'competition' => $request->competition
]);
return redirect('/');
}
您可以在输入中存储编码的 json 字符串:
{{ Form::input('hidden', 'match', $match, json_encode(['some' => 'thing'])) }}
您需要再次解码:
$match = json_decode($request->match)
那么您可以:
$match['some'];
您现在拥有的代码将无法使用。
{{ Form::input('hidden', 'match', $match, ['id' => 'match']) }}
将在其中存储一个字符串。在您的 Controller 函数中,您正在尝试访问 object 参数 $request->match->home
。做这样的事情的唯一方法是使用 json_encoding/json_decoding。其中 $match
应该是 json_encode object 并且 $request->match
应该在你的控制器中解码。
可能的解决方案:
//in your blade file
{{ Form::input('hidden', 'match', json_encode($match), ['id' => 'match']) }}
//in your controller
public function store(Request $request)
{
$match = json_decode($request->match);
Ticket::create([
'home' => match->home,
'away' => $request->away,
'place' => $request->place,
'price' => $request->price,
'section' => $request->section,
'amount' => $request->amount,
'competition' => $request->competition
]);
return redirect('/');
}
如果您不喜欢在 blade 文件中使用 php 代码的解决方案,更好的解决方案是序列化您的匹配项 object:
$match= App\Match::find(1);
$match = $match->toJson();
return view('edit', ['match' => $match])
我得到一个包含匹配变量的隐藏字段。
{{ Form::input('hidden', 'match', $match, ['id' => 'match']) }}
如何在我的存储方法中检索 'home' 字段等?
public function store(Request $request)
{
Log::info($request->match);
Ticket::create([
'home' => $request->match->home,
'away' => $request->away,
'place' => $request->place,
'price' => $request->price,
'section' => $request->section,
'amount' => $request->amount,
'competition' => $request->competition
]);
return redirect('/');
}
您可以在输入中存储编码的 json 字符串:
{{ Form::input('hidden', 'match', $match, json_encode(['some' => 'thing'])) }}
您需要再次解码:
$match = json_decode($request->match)
那么您可以:
$match['some'];
您现在拥有的代码将无法使用。
{{ Form::input('hidden', 'match', $match, ['id' => 'match']) }}
将在其中存储一个字符串。在您的 Controller 函数中,您正在尝试访问 object 参数 $request->match->home
。做这样的事情的唯一方法是使用 json_encoding/json_decoding。其中 $match
应该是 json_encode object 并且 $request->match
应该在你的控制器中解码。
可能的解决方案:
//in your blade file
{{ Form::input('hidden', 'match', json_encode($match), ['id' => 'match']) }}
//in your controller
public function store(Request $request)
{
$match = json_decode($request->match);
Ticket::create([
'home' => match->home,
'away' => $request->away,
'place' => $request->place,
'price' => $request->price,
'section' => $request->section,
'amount' => $request->amount,
'competition' => $request->competition
]);
return redirect('/');
}
如果您不喜欢在 blade 文件中使用 php 代码的解决方案,更好的解决方案是序列化您的匹配项 object:
$match= App\Match::find(1);
$match = $match->toJson();
return view('edit', ['match' => $match])