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;
use Illuminate\Contracts\Foundation\Application;
use Illuminate\Contracts\View\Factory;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use App\WashingMachine;
use Illuminate\View\View;
class WashingMachineController extends Controller
{
@ -24,11 +27,11 @@ class WashingMachineController extends Controller
* Display a listing of the resource.
*
* @param Request $request
* @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\View\Factory|\Illuminate\View\View
* @return Application|Factory|View
*/
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 ]);
}
@ -36,7 +39,7 @@ class WashingMachineController extends Controller
/**
* 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()
{
@ -47,12 +50,12 @@ class WashingMachineController extends Controller
* Store a newly created resource in storage.
*
* @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)
{
$data = $request->validate([
"time" => "required"
"name" => "required"
]);
$machine = new WashingMachine($data);
@ -65,7 +68,7 @@ class WashingMachineController extends Controller
* Display the specified resource.
*
* @param int $id
* @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\View\Factory|\Illuminate\View\View
* @return Application|Factory|View
*/
public function show($id)
{
@ -80,7 +83,7 @@ class WashingMachineController extends Controller
* Show the form for editing the specified resource.
*
* @param int $id
* @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\View\Factory|\Illuminate\View\View
* @return Application|Factory|View
*/
public function edit($id)
{
@ -96,36 +99,43 @@ class WashingMachineController extends Controller
*
* @param \Illuminate\Http\Request $request
* @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)
{
$data = $request->validate([
"time" => "required"
"name" => "required"
]);
$machine = WashingMachine::find($id);
$machine->update($data);
$machine->save();
$saved = $machine->save();
return Response::detect("washing-machines.edit", [
"machine" => $machine
]);
if(!$saved){
return Response::detect("washing-machines.update", [
"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.
*
* @param int $id
* @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\View\Factory|\Illuminate\View\View
* @param $id
* @return Response
*/
public function destroy($id)
{
$machine = WashingMachine::find($id);
$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
{
//
protected $fillable = [
'name'
];
}

View File

@ -6,7 +6,7 @@
@endsection
@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
@section("content")

View File

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

View File

@ -21,9 +21,9 @@
</tr>
@foreach($machines as $machine)
<tr>
<td>{Navn}</td>
<td><a href=""><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>{{$machine->name}}</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', [ 'washing_machine' => $machine ]) }}" class="w-100 nostyle">
@csrf
@method("delete")