Reputation: 69
After creating a new post, I want it to redirect the page to a list of the posts. It is creating it successfully, and data insert into the database correctly, but when it redirects, I got this error, and the same class is working in authors functions in the same controller.
BlogController
namespace App\Http\Controllers\Backend;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Models\Blog\BlogPostCategory;
use Carbon\Carbon;
use App\Models\BlogPost;
use Image;
class BlogController extends Controller
{
public function BlogPostStore(Request $request)
{
$request->validate([
'post_title_en' => 'required',
'post_title_ar' => 'required',
'post_image' => 'required',
], [
'post_title_en.required' => 'Input Post Title English Name',
'post_title_ar.required' => 'Input Post Title Arabic Name',
]);
$image = $request->file('post_image');
$name_gen = hexdec(uniqid()).'.'.$image->getClientOriginalExtension();
Image::make($image)->resize(780, 433)->save('upload/post/'.$name_gen);
$save_url = 'upload/post/'.$name_gen;
BlogPost::insert([
'category_id' => $request->category_id,
'post_title_en' => $request->post_title_en,
'post_title_ar' => $request->post_title_ar,
'post_slug_en' => strtolower(str_replace(' ', '-', $request->post_title_en)),
'post_slug_ar' => str_replace(' ', '-', $request->post_title_ar),
'post_image' => $save_url,
'post_details_en' => $request->post_details_en,
'post_details_ar' => $request->post_details_ar,
'created_at' => Carbon::now(),
]);
$notification = array(
'message' => 'Blog Post Inserted Successfully',
'alert-type' => 'success'
);
return redirect()->route('list.post')->with($notification);
}
}
Pposts list function in BlogController*
public function ListBlogPost()
{
$blogpost = BlogPost::with('category')->latest()->get();
return view('backend.blog.post.post_list', compact('blogpost'));
}
Routes
Route::prefix('blog')->group(function(){
Route::get('/list/post', [BlogController::class, 'ListBlogPost'])
->name('list.post');
Route::post('/post/store', [BlogController::class, 'BlogPostStore'])
->name('post-store');
});
and I create BlogPostCategory class in path
App\Models\Blog\BlogPostCategory;
Upvotes: 0
Views: 1917
Reputation: 449
@ALAA ELDIN, There would be one of the below errors. -
Just rectify the path & clear the cache, and the error will be resolved.
Upvotes: 0
Reputation: 1280
The problem is most likely due to the relationship definition within the App\Models\BlogPost
model.
I think you have a category
method in your App\Models\BlogPost
model;
public function category()
{
return $this->belongsTo(BlogPostCategory::class);
}
So it is looking for BlogPostCategory
in same namespace but since your BlogPostCategory
's full path is App\Models\Blog\BlogPostCategory
you need to specify this.
Add use App\Models\Blog\BlogPostCategory;
on top of your App\Models\BlogPost
class.
<?php
namespace App\Models;
use App\Models\Blog\BlogPostCategory;
// other classes
class BlogPost extends Model
{
// other stuff
public function category()
{
return $this->belongsTo(BlogPostCategory::class);
}
}
Upvotes: 3