You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
TheBadSpace/app/Http/Controllers/LocationController.php

51 lines
1.6 KiB
PHP

<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\Location;
use Ramsey\Uuid\Uuid;
use Illuminate\Support\Facades\Auth;
class LocationController extends Controller
{
//
public function addLocation(Request $request)
{
$fields = $request->validate([
'name' => ['required'],
'url' => ['required'],
'description' => ['required'],
'tags' => ['required'],
]);
if ($fields) {
$examples = [];
$files = $request->files->get("loc_examples");
if ($request->hasfile('loc_examples')) {
foreach ($request->file('loc_examples') as $file) {
$path = $file->store('reference');
array_push($examples, ["path" => $path]);
}
}
$request->merge(['active' => true]);
$request->merge(['uuid' => Uuid::uuid4()]);
$request->merge(['images' => json_encode($examples)]);
$request->merge(['added_by' => Auth::user()->id]);
//NOTE: Laravel gets funky if sequencing isn't explicitly set
$new = Location::create($request->all());
if ($new) {
return back()->with('message', 'New Location Added. Take a break!');
} else {
return back()->withErrors([
'error' => 'Uh oh. There was an inssue',
]);
}
} else {
return back()->withErrors([
'error' => 'All fields are required',
]);
}
}
}