I want to store all the records in my database, and I am using the resource route, but when I am submitting my form, data is not going to save in my database and on the other hand, it redirects me to post.create
route.
These are my Routes, and I am working on Post routes.
use App\Http\Controllers\Admin\CategoryController as AdminCategoryController;
use App\Http\Controllers\Admin\HomeController;
use App\Http\Controllers\Admin\PostController;
use App\Http\Controllers\Admin\TagController;
use App\Http\Controllers\Admin\UserController as AdminUserController;
use App\Http\Controllers\User\PostController as UserPostController;
use App\Http\Controllers\User\UserController;
use Illuminate\Support\Facades\Route;
// USER ROUTES
Route::prefix('')->group(function () {
Route::get('', [UserController::class, 'index']);
Route::get('post', [UserPostController::class, 'index']);
});
// ADMIN ROUTES
Route::prefix('admin')->group(function () {
Route::get('home', [HomeController::class, 'index']);
// User Routes
Route::resource('user', AdminUserController::class);
// Post Routes
Route::resource('post', PostController::class);
// Tag Routes
Route::resource('tag', TagController::class);
// Category Routes
Route::resource('category', AdminCategoryController::class);
});
Form
<!-- form start -->
<form action="{{ route('post.store') }}" method="post" enctype="multipart/form-data">
@csrf
<div class="card-body">
<div class="row">
<div class="col-md-6">
<div class="form-group">
<label for="title">Post Title</label>
<input type="text" name="title" class="form-control" id="title" placeholder="Title">
@if ($errors->has('title'))
<p class="text-danger">{{ $errors->first('title') }}</p>
@endif
</div>
<div class="form-group">
<label for="subtitle">Post Sub-Title</label>
<input type="text" name="subtitle" class="form-control" id="subtitle" placeholder="SubTitle">
@if ($errors->has('subtitle'))
<p class="text-danger">{{ $errors->first('subtitle') }}</p>
@endif
</div>
<div class="form-group">
<label for="slug">Slug</label>
<input type="text" name="slug" class="form-control" id="slug" placeholder="Slug">
@if ($errors->has('slug'))
<p class="text-danger">{{ $errors->first('slug') }}</p>
@endif
</div>
</div>
<div class="col-md-6">
<div class="form-group">
<label for="image">File input</label>
<div class="input-group">
<div class="custom-file">
<input type="file" name="image" class="custom-file-input" id="image">
<label class="custom-file-label" for="image">Choose file</label>
</div>
<div class="input-group-append">
<span class="input-group-text">Upload</span>
</div>
</div>
</div>
@if ($errors->has('image'))
<p class="text-danger">{{ $errors->first('image') }}</p>
@endif
<div class="form-check">
<input type="checkbox" name="status" class="form-check-input" id="status">
<label class="form-check-label" for="status">Publish</label>
</div>
</div>
</div>
<label for="title">Write Post Body Here</label>
<textarea id="summernote" name="body"></textarea>
@if ($errors->has('body'))
<p class="text-danger">{{ $errors->first('body') }}</p>
@endif
<div class="form-group">
<button type="submit" class="btn btn-primary">Submit</button>
</div>
</div>
<!-- /.card-body -->
</form>
This is my PostController, and I am working on Store Method
public function store(Request $request)
{
$request->validate([
'title' => 'required',
'subtitle' => 'required',
'slug' => 'required',
'image' => 'required',
'status' => 'required',
'body' => 'required',
]);
$post = new post;
$post->title = $request->title;
$post->subtitle = $request->subtitle;
$post->slug = $request->slug;
$file_name = time().'_'.$request->image->getClientOriginalName();
$file_path = $request->file('image')->storeAs('uploads',$file_name,'public');
$post->image = $file_path;
$post->status = $request->status;
$post->body = $request->body;
$post->save();
return redirect(route('post.index'));
}