2017-06-16 22 views
0

我得到一個包含匹配變量的隱藏字段。Laravel從輸入數組中檢索元素

{{ Form::input('hidden', 'match', $match, ['id' => 'match']) }} 

如何在我的商店方法中檢索「主頁」字段等?

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('/'); 
    } 
+0

能否請您向我們展示'Log :: info($ request-> match);'?它是一個對象嗎?陣列? –

+0

我假設你將匹配id存儲在隱藏字段中,只需在你的控制器中用Match :: find($ request-> match)查詢它; – btl

回答

0

現在的代碼不起作用。

{{ Form::input('hidden', 'match', $match, ['id' => 'match']) }} 

將存儲字符串。在您的控制器功能中,您正試圖訪問對象參數$request->match->home。做這種事情的唯一方法是使用json_encoding/json_decoding。其中$match應該是json_encode對象,而$request->match應該在你的Controller中解碼。

可能的解決方案:

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

如果你不喜歡你的刀片使用PHP代碼的解決方案文件一個更好的解決辦法是序列化的匹配對象:

$match= App\Match::find(1); 

$match = $match->toJson(); 
return view('edit', ['match' => $match]) 
+0

使用json編碼和解碼做的工作,謝謝! – jordibenck

0

你可以在你輸入一個編碼的JSON字符串存儲:

{{ Form::input('hidden', 'match', $match, json_encode(['some' => 'thing'])) }}

您將需要再次解碼:

$match = json_decode($request->match)

然後,您可以:

$match['some'];