Ekapp/skolehjem/app/Http/Controllers/UserEventController.php

94 lines
3.0 KiB
PHP

<?php
namespace App\Http\Controllers;
use App\UserEvent;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
class UserEventController extends Controller
{
public function __construct()
{
$this->middleware(["auth"]);
$this->middleware(["lang"]);
}
/**
* Update the specified resource in storage.
*
* @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\View\Factory|\Illuminate\View\View
*/
public function index(Request $request)
{
$userevents = UserEvent::join('events', 'events.id', '=', 'user_events.event_id')->orderBY('date' , 'asc')->where('user_id', auth()->user()->id)->get();
return Response::detect("events.yourevents", [ "userevents" => $userevents ]);
}
public function store() {
}
/**
* Update the specified resource in storage.
*
* @param UserEvent $eventid
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\View\Factory|\Illuminate\View\View
*/
public function create(Request $request)
{
// Get written data from events.index
$data = $request->validate([
"event_id" => "required|max:255"
]);
// Check the UserEvent table if there is a row that has the user_id AND the event_id
$getTableInfo = UserEvent::where('user_id', auth()->user()->id)
->where('event_id', $request->event_id)->get();
// If the row has both, then go back and show an error - Cause you're not allowed to be on the same event twice.
if (count($getTableInfo) > 0)
return redirect()->route("events.index")->with('error#' . $request->event_id, '<p class="text-center"><b>Du har allerede tilmeldt dig denne Aktivitet!</b></p>');
// If not, then it keeps going and saves and shows a success message
$UserEvent = new UserEvent($data);
$UserEvent->user_id = auth()->user()->id;
$UserEvent->save();
//return redirect()->back();
}
/**
* Update the specified resource in storage.
*
* @param UserEvent $id
* @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\View\Factory|\Illuminate\View\View
*/
public function destroy($id)
{
// Check the UserEvent table if there is a row that has the user_id AND the event_id
//$UserEvent = UserEvent::find($eventid)
$UserEvent = UserEvent::query()->where('user_id', "=", auth()->user()->id)->where('event_id', "=", $id);
$UserEvent->delete();
$JoinedEvents = UserEvent::query()->join('events', 'events.id', '=', 'user_events.event_id')->where('user_id', "=", auth()->user()->id)->get();
return redirect()->route("userevents.index", [ "userevents" => $JoinedEvents ])->with('eventunsubscribed', '<p class="text-center"><b>Du er hermed afmeldt aktiviteten!</b></p>');
}
public function show()
{
}
public function edit() {
}
public function update() {
}
}