Advertisement
chilly2go

create/edit controller method

Aug 3rd, 2018
173
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <?php
  2.  
  3. namespace App\Http\Controllers;
  4.  
  5. use App\Http\Requests\IPCreateRequest;
  6. use App\Http\Requests\IPEditRequest;
  7. use App\IP_Pool;
  8. use Illuminate\Http\Request;
  9. use Illuminate\Support\ViewErrorBag;
  10.  
  11. class IPController extends Controller
  12. {
  13.     /**
  14.      * Show the form for editing the specified resource.
  15.      *
  16.      * @param  \App\IP_Pool $iP_Pool
  17.      *
  18.      * @return \Illuminate\Http\Response
  19.      */
  20.     public
  21.     function edit(IP_Pool $ip)
  22.     {
  23.         return view("ip.edit", ["ip" => $ip]);
  24.     }
  25.  
  26.     /**
  27.      * Update the specified resource in storage.
  28.      *
  29.      * @param  \Illuminate\Http\Request $request
  30.      * @param  \App\IP_Pool             $iP_Pool
  31.      *
  32.      * @return \Illuminate\Http\Response
  33.      */
  34.     public
  35.     function update(IPEditRequest $request, IP_Pool $ip)
  36.     {
  37. // get validated values
  38.         $validated = $request->validated();
  39. // is there an entry for the ip -> search
  40.         $found     = IP_Pool::where("ip", "=", $validated['edit_ip'])->get();
  41. // found an ip? yes? is it the same (same id)?
  42.         if (optional($found)->count() > 1 || (optional($found)->count() == 1 && $found->first()->id != $ip->id))
  43.         {
  44. // ip already in use with different id
  45.             return back()->withInput()->with("errors", "IP wird bereits verwendet!");
  46.         } else
  47.         {
  48. // assign values
  49.             $ip->ip   = $validated['edit_ip'];
  50.             $ip->name = $validated['edit_name'];
  51. // something changed?
  52.             if ($ip->isDirty())
  53.             {
  54. // save it
  55.                 $ip->save();
  56. // return with info
  57.                 return redirect()->route("ip.edit", ["ip" => $ip->id])->with("status", "Daten erfolgreich gespeichert");
  58.             }
  59. // nothing changed. just return
  60.             return redirect()->route("ip.edit", ["ip" => $ip->id]);
  61.         }
  62.     }
  63. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement