【问题标题】:how can I update the new checked ones in the database in laravel如何在 laravel 的数据库中更新新的选中项
【发布时间】:2018-07-07 13:21:41
【问题描述】:

我可以请求选中的并查看它们,但我不能更新它。

在数据库中,我有表 types 和类型 2 列:rijbewijsnummer, rijbewijstype

如何更新数据库中新检查的?

我的控制器中的功能

public  function Modify (Request $request) {



   $user = Auth::user();
   $type = Type::all();

   $user->rijbewijsnummer = Request::input('rijbewijsnummer');
   $type->rijbewijstype = Request::input('rijbewijstype');




   $user->save();




   return redirect()->route('home');

   }

我觉得我做错了什么

public function details(Request $request)
{
    $user = Auth::user();
    $types = Type::where("rijbewijsnummer", "=", $user->rijbewijsnummer)->get();


    foreach ($types as $type)
    {



        $rijbewijstype[]= $type->rijbewijstype;



    }

    $user->save();






    return view('details', compact('user','rijbewijstype', 'types'));

}

查看:

<div class="col-md-6">
    <div name="rijbewijstype">
        <input  type="checkbox"  name="rijbewijstype[]" @foreach ($types as $type) @if ($type->rijbewijstype == "AM")) checked  @endif @endforeach value="AM" >AM</input>
        <input type="checkbox"  name="rijbewijstype[]" @foreach ($types as $type) @if ($type->rijbewijstype == "A1") checked @endif @endforeach value="A1">A1</input>
        <input type="checkbox"  name="rijbewijstype[]" @foreach ($types as $type) @if ($type->rijbewijstype == "A2") checked @endif @endforeach value="A2">A2</input>
        <input type="checkbox" name="rijbewijstype[]" @foreach ($types as $type) @if ($type->rijbewijstype == "A") checked @endif @endforeach value="A">A</input>
        <input type="checkbox" name="rijbewijstype[]" @foreach ($types as $type) @if ($type->rijbewijstype == "B1") checked @endif @endforeach value="B1">B1</input>
        <input type="checkbox" name="rijbewijstype[]" @foreach ($types as $type) @if ($type->rijbewijstype == "B") checked @endif @endforeach value="B">B</input> <br>
        <input type="checkbox" name="rijbewijstype[]" @foreach ($types as $type) @if ($type->rijbewijstype == "C1") checked @endif @endforeach value="C1">C1</input>
        <input type="checkbox" name="rijbewijstype[]" @foreach ($types as $type) @if ($type->rijbewijstype == "D1") checked @endif @endforeach value="D1">D1</input>
        <input type="checkbox" name="rijbewijstype[]" @foreach ($types as $type) @if($type->rijbewijstype == "D") checked @endif @endforeach value="D">D</input>
        <input type="checkbox" name="rijbewijstype[]" @foreach ($types as $type) @if ($type->rijbewijstype == "BE") checked @endif @endforeach value="BE">BE</input>
        <input type="checkbox" name="rijbewijstype[]" @foreach ($types as $type) @if ($type->rijbewijstype == "C1E") checked @endif @endforeach value="C1E">C1E</input>
        <input type="checkbox" name="rijbewijstype[]" @foreach ($types as $type) @if ($type->rijbewijstype == "CE") checked @endif @endforeach value="CE">CE</input>
        <input type="checkbox" name="rijbewijstype[]" @foreach ($types as $type) @if ($type->rijbewijstype == "D1E") checked @endif @endforeach value="D1E">D1E</input>
        <input type="checkbox" name="rijbewijstype[]" @foreach ($types as $type) @if ($type->rijbewijstype == "DE") checked @endif @endforeach value="DE">DE</input>
        <input type="checkbox" name="rijbewijstype[]" @foreach ($types as $type) @if ($type->rijbewijstype == "T") checked @endif @endforeach value="T">T</input>

    </div>
</div>

【问题讨论】:

    标签: php html arrays database laravel


    【解决方案1】:

    简短的回答,您没有将“rijbewijsnummer”保存在类型表中。

    长答案(使用 cmets 解释代码及其作用):

     $user = Auth::user();
       $type = Type::all();
    
    // Here you set the attribute rijbewijsnummer to the input value.
       $user->rijbewijsnummer = Request::input('rijbewijsnummer');
    
    // Here you set the selected type as an attribute on the type object.
       $type->rijbewijstype = Request::input('rijbewijstype');
    
    // Here you save the user (this means you can now get the rijbewijsnummer from the user table)
       $user->save();
    
    // But you don't save the Type you selected, 
    // let alone add the rijbewijsnummer to it, 
    // so even if you did save it, you'd just get an empty rijbewijsnummer
    // and rijbewijstype would be filled. 
    // (or rather, overwrite the other empty rijbewijsnummer field with the new rijbewijstype, if you allow rijbewijsnummer to be empty)
    
       return redirect()->route('home');
    

    您可以通过更改以下行来解决此问题。

    $type->rijbewijstype = Request::input('rijbewijstype');
    

    // Creates a new entry in Types, using the rijbewijstype and rijbewijsnummer.
    $type->create(['rijbewijstype' => Request::input('rijbewijstype'), 'rijbewijsnummer' => Request::input('rijbewijsnummer'));
    

    我假设您希望为不同类型保存相同 rijbewijsnummer 的多个条目。

    您还使用了一些低效的代码,而且我认为您的数据库结构没有经过深思熟虑。但这超出了这个问题的范围。 如果您的代码完全正常工作,您可以随时寻求帮助以改进代码审查堆栈上的代码。

    【讨论】:

      猜你喜欢
      • 2022-11-02
      • 2017-06-08
      • 1970-01-01
      • 1970-01-01
      • 2019-10-11
      • 2018-06-11
      • 2021-12-24
      • 1970-01-01
      • 2019-11-29
      相关资源
      最近更新 更多