Fixed Washing machine creation and editing

This commit is contained in:
frederikpyt 2020-07-01 10:30:28 +02:00
parent 6ecc96ef55
commit 2846dac2dc
5 changed files with 38 additions and 25 deletions

View File

@ -2,10 +2,13 @@
namespace App\Http\Controllers; namespace App\Http\Controllers;
use Illuminate\Contracts\Foundation\Application;
use Illuminate\Contracts\View\Factory;
use Illuminate\Http\Request; use Illuminate\Http\Request;
use Illuminate\Http\Response; use Illuminate\Http\Response;
use App\WashingMachine; use App\WashingMachine;
use Illuminate\View\View;
class WashingMachineController extends Controller class WashingMachineController extends Controller
{ {
@ -24,11 +27,11 @@ class WashingMachineController extends Controller
* Display a listing of the resource. * Display a listing of the resource.
* *
* @param Request $request * @param Request $request
* @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\View\Factory|\Illuminate\View\View * @return Application|Factory|View
*/ */
public function index(Request $request) public function index(Request $request)
{ {
$machines = WashingMachine::query()->paginate($request->query("page", 1)); $machines = WashingMachine::query()->paginate($request->query("limit", 20));
return Response::detect("washing-machines.index", [ "machines" => $machines ]); return Response::detect("washing-machines.index", [ "machines" => $machines ]);
} }
@ -36,7 +39,7 @@ class WashingMachineController extends Controller
/** /**
* Show the form for creating a new resource. * Show the form for creating a new resource.
* *
* @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\View\Factory|\Illuminate\View\View * @return Application|Factory|View
*/ */
public function create() public function create()
{ {
@ -47,12 +50,12 @@ class WashingMachineController extends Controller
* Store a newly created resource in storage. * Store a newly created resource in storage.
* *
* @param \Illuminate\Http\Request $request * @param \Illuminate\Http\Request $request
* @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\View\Factory|\Illuminate\View\View * @return Application|Factory|View
*/ */
public function store(Request $request) public function store(Request $request)
{ {
$data = $request->validate([ $data = $request->validate([
"time" => "required" "name" => "required"
]); ]);
$machine = new WashingMachine($data); $machine = new WashingMachine($data);
@ -65,7 +68,7 @@ class WashingMachineController extends Controller
* Display the specified resource. * Display the specified resource.
* *
* @param int $id * @param int $id
* @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\View\Factory|\Illuminate\View\View * @return Application|Factory|View
*/ */
public function show($id) public function show($id)
{ {
@ -80,7 +83,7 @@ class WashingMachineController extends Controller
* Show the form for editing the specified resource. * Show the form for editing the specified resource.
* *
* @param int $id * @param int $id
* @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\View\Factory|\Illuminate\View\View * @return Application|Factory|View
*/ */
public function edit($id) public function edit($id)
{ {
@ -96,36 +99,43 @@ class WashingMachineController extends Controller
* *
* @param \Illuminate\Http\Request $request * @param \Illuminate\Http\Request $request
* @param int $id * @param int $id
* @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\View\Factory|\Illuminate\View\View * @return Application|Factory|View
*/ */
public function update(Request $request, $id) public function update(Request $request, $id)
{ {
$data = $request->validate([ $data = $request->validate([
"time" => "required" "name" => "required"
]); ]);
$machine = WashingMachine::find($id); $machine = WashingMachine::find($id);
$machine->update($data); $machine->update($data);
$machine->save(); $saved = $machine->save();
return Response::detect("washing-machines.edit", [ if(!$saved){
return Response::detect("washing-machines.update", [
"machine" => $machine "machine" => $machine
]); ]);
}else{
$machines = WashingMachine::query()->paginate($request->input("limit", 20));
return Response::detect("washing-machines.index", [
"machines" => $machines
]);
}
} }
/** /**
* Remove the specified resource from storage. * Remove the specified resource from storage.
* *
* @param int $id * @param $id
* @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\View\Factory|\Illuminate\View\View * @return Response
*/ */
public function destroy($id) public function destroy($id)
{ {
$machine = WashingMachine::find($id); $machine = WashingMachine::find($id);
$machine->delete(); $machine->delete();
return Response::detect("washing-machines.destroy"); return Response::detect("washing-machines.delete");
} }
} }

View File

@ -6,5 +6,7 @@ use Illuminate\Database\Eloquent\Model;
class WashingMachine extends Model class WashingMachine extends Model
{ {
// protected $fillable = [
'name'
];
} }

View File

@ -6,7 +6,7 @@
@endsection @endsection
@section("path") @section("path")
<a href="{{ route('washing-machines.delete') }}" class="text-white">Fjern Vaskemaskine</a> / <a href="{{ route('washing-machines.destroy') }}" class="text-white">Fjern Vaskemaskine</a> /
@endsection @endsection
@section("content") @section("content")

View File

@ -6,14 +6,15 @@
@endsection @endsection
@section("path") @section("path")
<a href="{{ route('washing-machines.edit', ['id' => $user->id]) }}" class="text-white">Rediger Vaskemaskiner</a> / <a href="{{ route('washing-machines.edit', ['washing_machine' => $machine]) }}" class="text-white">Rediger Vaskemaskiner</a> /
@endsection @endsection
@section("content") @section("content")
<form method="post" action="{{ route("washing-machines.store") }}"> <form method="post" action="{{ route("washing-machines.update", [ 'washing_machine' => $machine]) }}">
@csrf @csrf
@method("put")
<label for="name_first">Vaskemaskine Navn:</label> <label for="name_first">Vaskemaskine Navn:</label>
<input type="text" name="name" id="name" max="60" required> <input type="text" name="name" id="name" max="60" value="{{$machine->name}}" required>
<input type="submit" class="btn btn-dark text-white" value="Opret"> <input type="submit" class="btn btn-dark text-white" value="Rediger">
</form> </form>
@endsection @endsection

View File

@ -21,9 +21,9 @@
</tr> </tr>
@foreach($machines as $machine) @foreach($machines as $machine)
<tr> <tr>
<td>{Navn}</td> <td>{{$machine->name}}</td>
<td><a href=""><img class="w-100" src="{{ asset('/images/icons/pencil-dark.svg') }}" alt="Update"></a></td> <td><a href="{{ route('washing-machines.edit', [ 'washing_machine' => $machine ]) }}"><img class="w-100" src="{{ asset('/images/icons/pencil-dark.svg') }}" alt="Update"></a></td>
<td><form method="post" action="{{ route("washing-machines.destroy", [ "machine" => $machine ]) }}" class="w-100 nostyle"> <td><form method="post" action="{{ route('washing-machines.destroy', [ 'washing_machine' => $machine ]) }}" class="w-100 nostyle">
@csrf @csrf
@method("delete") @method("delete")