'''
@Snapey please help im lost here
here is my entire controller and I would have to remove the image name and allow unique ids for each image, how can I add the image table
''' <?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use App\Models\Post;
class CreateController extends Controller
{
public function index()
{
$posts = Post::all();
return view('create.post', compact('posts'));
}
public function __construct()
{
$this->middleware('auth');
}
public function store(Request $request)
{
$request->validate([
'title' => 'required',
'body' => 'required|string|min:15|max:100',
'location' => 'required',
'category' => 'required',
'files' => 'required', // checks length of array
'files.*' => 'mimes:jpeg,jpg,png,gif,csv,txt,WebP,|max:10048'
]);
$title = $request->input('title');
$user_id = Auth::user()->id;
$body = $request->input('body');
$location = $request->input('location');
$category = $request->input('category');
if ($request->hasfile('files')) {
foreach ($request->file('files') as $file) {
$name = $file->getClientOriginalName();
$file->move(public_path() . '/storage/postImages', $name);
$imgData[] = $name;
}
$post = new Post();
$post->title = $title;
$post->user_id = auth()->id(); // add this line
$post->body = $body;
$post->location = $location;
$post->category = $category;
$post->name = json_encode($imgData); //Don't use json
$post->image_path = json_encode($imgData); // Don't use json
$post->save();
return back()->with('success', ' Post created successfully! ');
}
}
}
'''