簡體   English   中英

如何從另一個表中獲取保存的記錄

[英]how to get the saved records from another table

所以我有一個帶有注冊表和 ExtraRegistration 表的 crud 應用程序。

在Registration 表中,我得到了一個foreach,其中包含ExtraOptions 表中的選項。

我現在擁有的:在 RegistrationController 中,我創建了一個新的 ExtraRegistration,並將 select 選項保存到 ExtraRegistration 表中。 現在,當我單擊編輯時,在其中一個注冊上,沒有我剛剛填寫的信息。

我想要什么:我想要它,所以當你創建一個新的注冊時,select 保存到 ExtraRegistration 表中,但是當你想在應用程序的 de Registration 表中編輯記錄時,select 是空的,因為我將它保存在額外注冊。

所以我的問題是我如何從 ExtraRegistration 表中獲取選擇(和其他輸入),以便它在“注冊”的編輯表單中顯示 select。

注冊控制器

options = Extra::where("exa_form_id", $afstand->asd_form_id)->get();
foreach($options as $option){
  $input_name = "option_" . $option->exa_id;
  $input_option = $request->$input_name;
  if(!is_null($input_option)){
    $input_name_extra = "extraoptions_" . $option->exa_id;
    $input_option_extra = $request->$input_name_extra;
    $extraregistration = new ExtraRegistration();
    $extraregistration->iea_registration_id = $registration->isg_id;
    $extraregistration->iea_extra_id = $input_option;
    $extraregistration->iea_extraoption_id = $input_option_extra;
    $extraregistration->iea_price = $option->exa_price;
    $extraregistration->save();
  }
}

我希望我解釋得足夠好! 提前致謝

因此,如果我做對了,您可以將Extra model 的復選框選項保存到另一個名為ExtraRegistration的 model 中,並且您想在編輯Extra Z20F35E630DAF494DBFAC4C3F6D 時顯示這些選項,對嗎?

為此,您的模型應該是相關的。 如果您還沒有它,請將extra_id添加到您的ExtraRegistration表並在您的ExtraRegistration model 上創建一個 belongsTo 關系。

public function extra(){
   return $this->belongsTo('App\Extra');
}

現在您的模型是相關的,您可以像這樣列出 model Extra的選項:

$extra = Extra::find($id)->first(); 
$options = ExtraRegistration::where('extra_id', $extra->id)->get()

希望我的問題是正確的。 如果您有任何其他問題,請隨時提出。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM