Can you display BlogController and also try this before that,
public function edit(Post $post,Blog $blog){
//Something like
}
can anybody help me with this error? i use route resource
here is my route
Route::resource('/blog/post', 'Backend\BlogController');
but when it access edit($id) it has error like above, i access the url throught button, and here's my button code
<a href="{{ route('post.edit', $post->id) }}" class="btn btn-xs btn-default">
Can you display BlogController and also try this before that,
public function edit(Post $post,Blog $blog){
//Something like
}
@MUNAZZIL - here my BlogController
public function edit($id)
{
$post = Post::findOrFail($id);
return view("backend.blog.edit", compact("post"));
}
it has the same error with show function
Use as like below and check. You have to use in compact only ', and also have you used this is above of controller.
use App\Post;
public function edit($id)
{
$post = Post::findOrFail($id);
return view("backend.blog.edit", compact('post'));
}
else try this
public function edit(Post $post,$id)
{
$post = Post::findOrFail($id);
return view("backend.blog.edit", compact('post'));
}
its return 404 when i edit code like above
can i see your route list
@MAC03733 - ``` +--------+-----------+------------------------+---------------------+------------------------------------------------------------------------+------------------------------+ | Domain | Method | URI | Name | Action | Middleware | +--------+-----------+------------------------+---------------------+------------------------------------------------------------------------+------------------------------+ | | GET|HEAD | / | blog | App\Http\Controllers\BlogController@index | web | | | GET|HEAD | api/user | | Closure | api,auth:api | | | GET|HEAD | author/{author} | author | App\Http\Controllers\BlogController@author | web | | | POST | blog/post | post.store | App\Http\Controllers\Backend\BlogController@store | web,auth | | | GET|HEAD | blog/post | post.index | App\Http\Controllers\Backend\BlogController@index | web,auth | | | GET|HEAD | blog/post/create | post.create | App\Http\Controllers\Backend\BlogController@create | web,auth | | | DELETE | blog/post/{post} | post.destroy | App\Http\Controllers\Backend\BlogController@destroy | web,auth | | | PUT|PATCH | blog/post/{post} | post.update | App\Http\Controllers\Backend\BlogController@update | web,auth | | | GET|HEAD | blog/post/{post} | post.show | App\Http\Controllers\Backend\BlogController@show | web,auth | | | GET|HEAD | blog/post/{post}/edit | post.edit | App\Http\Controllers\Backend\BlogController@edit | web,auth | | | GET|HEAD | blog/post{id} | | App\Http\Controllers\Backend\BlogController@edit | web,auth | | | GET|HEAD | category/{category} | category | App\Http\Controllers\BlogController@category | web | | | GET|HEAD | email/resend | verification.resend | App\Http\Controllers\Auth\VerificationController@resend | web,auth,throttle:6,1 | | | GET|HEAD | email/verify | verification.notice | App\Http\Controllers\Auth\VerificationController@show | web,auth | | | GET|HEAD | email/verify/{id} | verification.verify | App\Http\Controllers\Auth\VerificationController@verify | web,auth,signed,throttle:6,1 | | | GET|HEAD | home | home | App\Http\Controllers\Backend\HomeController@index | web,auth | | | POST | login | | App\Http\Controllers\Auth\LoginController@login | web,guest | | | GET|HEAD | login | login | App\Http\Controllers\Auth\LoginController@showLoginForm | web,guest | | | POST | logout | logout | App\Http\Controllers\Auth\LoginController@logout | web | | | POST | password/email | password.email | App\Http\Controllers\Auth\ForgotPasswordController@sendResetLinkEmail | web,guest | | | POST | password/reset | password.update | App\Http\Controllers\Auth\ResetPasswordController@reset | web,guest | | | GET|HEAD | password/reset | password.request | App\Http\Controllers\Auth\ForgotPasswordController@showLinkRequestForm | web,guest | | | GET|HEAD | password/reset/{token} | password.reset | App\Http\Controllers\Auth\ResetPasswordController@showResetForm | web,guest | | | GET|HEAD | {post} | blog.single | App\Http\Controllers\BlogController@single | web | +--------+-----------+------------------------+---------------------+------------------------------------------------------------------------+------------------------------+
Your edit function is wrong.
Since you are using a resource controller you have to use the name of the resource
You can use php artisan route:list and see what parameter the route is expecting.
You will see the edit route has this URI
blog/post/{post}/edit
so in your controller, you must reference $post not ID
public function edit(Post $post)
{
return view("backend.blog.edit", compact("post"));
}
and since you use the same name then Laravel will use Route Model Binding to load the post for you and pass it to the controller method.
@SNAPEY - it return 404 when i edit it
That probably means that the post is not found. Are you using slugs perhaps? Can you show your Post model code? Also, which laravel version are you using?
@RONB1985 - here is my full Post Model, i use 5.7 version https://paste.laravel.io/7ae2074e-860a-48fd-8b4f-f9ebd1436e4b
In your route list says as like below i have removed one / in route.
Route::resource('blog/post{id}', 'Backend\BlogController');
else
Route::resource('blog/{id}', 'Backend\BlogController');
Personally I never used resource controllers for urls like:
blogs/posts
So not sure if that will work properly.
Try this instead, just to see if it works (it should):
Route::resource('posts', 'Backend\PostsController');
And then for your button url:
<a href="{{ route('posts.edit', $post->id) }}" class="btn btn-xs btn-default">
Or try without a resource controller, also just to see if it works:
Route::get('blog/post/{post}/edit', 'Backend\BlogController@edit')->name('post.edit');
And then in your controller:
public function edit(Post $post)
{
dd($post);
}
See if you get an instance of Post. If not, then the Post just cannot be found. In that case, just try this in your controller:
public function edit($post)
{
dd($post);
}
And see what is being returned exactly and check if that is expected.
Also, I see you're working with slugs, so in your model you could use this:
public function getRouteKeyName()
{
return 'slug';
}
So you can use the slug to find a Post, with your button:
<a href="{{ route('post.edit', $post->slug) }}" class="btn btn-xs btn-default">
@RONB1985 - it work, but if post is not publish (draft) it return 404, it's look like i filter the draft posts in showing before, it mean that user can see the draft posts, but in admin side it must show up, so how i can make for the admin can see it?
How are you filtering it exactly?
@RONB1985 - i make a scope like this in Post Model to filter draft post in user view, it also filter in admin view, how to change it?
public function scopePublished($query)
{
return $query->where("published_at", "<=", Carbon::now());
}
when i disable the scope it work but in user view it show draft posts
@chairuman, I would simply use a different controller for the admins, you can return the same view, but you would select all Posts without that scope. Keep it simple.
@RONB1985 - yes, i use different controlloer for the admin
<?php
namespace App\Http\Controllers\Backend;
use Illuminate\Http\Request;
use App\Post;
use App\Http\Requests;
class BlogController extends BackendController
{
protected $limit = 10;
protected $uploadPath;
public function __construct()
{
parent::__construct();
$this->uploadPath = public_path('img');
}
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
$posts = Post::with('category','author')->latest()->paginate($this->limit);
$postCount = Post::count();
return view("backend.blog.index", compact('posts','postCount'));
}
/**
* Show the form for creating a new resource.
*
* @return \Illuminate\Http\Response
*/
public function create(Post $post)
{
return view("backend.blog.create", compact("post"));
}
/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function store(Requests\PostRequest $request)
{
$data = $this->handleRequest($request);
$request->user()->posts()->create($data);
return redirect('/blog/post')->with('message','Your posts was created successfully');
}
private function handleRequest($request)
{
$data = $request->all();
if ($request->hasFile('image')) {
$image = $request->file('image');
$fileName = $image->getClientOriginalName();
$destination = $this->uploadPath;
$image->move($destination, $fileName);
$data['image'] = $fileName;
}
return $data;
}
/**
* Display the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function show(Post $post)
{
dd($id);
}
/**
* Show the form for editing the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function edit(Post $post)
{
dd($post);
}
/**
* Update the specified resource in storage.
*
* @param \Illuminate\Http\Request $request
* @param int $id
* @return \Illuminate\Http\Response
*/
public function update(Request $request, $id)
{
//
}
/**
* Remove the specified resource from storage.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function destroy(Post $post)
{
//
}
}
that's my controller for the admin i did not use the scope
and this my Post model
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
use Carbon\Carbon;
use GrahamCampbell\Markdown\Facades\Markdown;
class Post extends Model
{
protected $fillable = ['title', 'slug','excerpt','body','published_at','category_id','image'];
protected $dates = ['published_at'];
public function getImageUrlAttribute($value)
{
$imageUrl = "";
if (! is_null($this->image)) {
$imagePath = public_path() . "/img/" . $this->image;
if (file_exists($imagePath)) $imageUrl = asset("img/" . $this->image);
}
return $imageUrl;
}
public function author()
{
return $this->belongsTo(User::class);
}
public function category()
{
return $this->belongsTo(Category::class);
}
public function setPublishedAtAttribute($value)
{
$this->attributes['published_at'] = $value ?: NULL;
}
public function getDateAttribute($value)
{
return is_null($this->published_at) ? '' : $this->published_at->diffForHumans();
}
public function getBodyHtmlAttribute($value)
{
return $this->body ? Markdown::convertToHtml(e($this->body)) : NULL;
}
public function getExcerptHtmlAttribute($value)
{
return $this->excerpt ? Markdown::convertToHtml(e($this->excerpt)) : NULL;
}
public function dateFormatred($showTimes = false)
{
$format = "d/m/Y";
if ($showTimes) $format = $format . " H:i:s";
return $this->created_at->format($format);
}
public function publicationLabel()
{
if (! $this->published_at) {
return '<span class="label label-warning">Draft</span>';
}
elseif ($this->published_at && $this->published_at->isFuture()) {
return '<span class="label label-info">Schedule</span>';
}
else{
return '<span class="label label-success">published</span>';
}
}
public function scopePublished($query)
{
return $query->where("published_at", "<=", Carbon::now());
}
public function scopePopular($query)
{
return $query->orderBy('view_count','desc');
}
}
So what is the exact issue you're having right now?
@RONB1985 - the draft posts cannot be edit by admin, only published posts that can be edit,
@chairuman, ok, and why not? Are you getting an error when an admin wants to edit a draft post? And how do you distinguish an admin from a normal user? Are you using roles and permissions?
@RONB1985 - yes it getting error when admin edit draft posts, it's like the post is not found but it exist in the database, i'm not using roles
Your scopePublished should only be applied when you explicitly mention it in your query.
Have you done something to register global scope so that it is applied to EVERY query?
@SNAPEY - no, i did not use global scope for it, that make me confuse why it applied in admin
my problem solved me route id not {id solved me {id}
Route::get('/store/district/{id}', 'EditDistrict')->name('edit.district');
Please or to participate in this conversation.