Laravel Edit link doesn't work - laravel

Every post in the list has Edit link
Edit
routes
Route::get('dashboard/posts/{id}/edit', 'PostsController#edit');
Route::put('dashboard/posts/{id}', 'PostsController#update');
methods in PostsController
public function edit($id)
{
$post = Post::findOrFail($id);
return view('dashboard.edit', compact('post'));
}
public function update($id, PostRequest $request)
{
$post = Post::findOrFail($id);
$post->update($request->all());
return redirect ('dashboard');
}
but on clink on the Edit button I get an error
NotFoundHttpException in RouteCollection.php line 161:
What's wrong? How to fix it?

In the route file you have written posts and in the href post
{{ URL::to('dashboard/post/' . $post->id . '/edit') }}
Route::get('dashboard/posts/{id}/edit', 'PostsController#edit');

Related

Attempt to read property "title" on null - Laravel

when I enter the detail page in Laravel, it gives an error. I don't understand where the error is coming from. My codes are as follows.
where is the problem? actually it says it's empty, but it needs to pull data from $blog on controller side.
Controller:
public function show($id)
{
$blog = Blog::where('id',$id)->first();
return view('front.detail',compact('blog'));
}
routes/web.php:
Route::prefix('{lang?}')->middleware('locale')->group(function() {
Route::get('/', [MainController::class, 'index'])->name('home');
Route::get('/about', [MainController::class, 'about'])->name('about');
Route::resource('/blogs', MainController::class)->only([ 'show']);
});
detail.blade.php:
<li>
<h2>{{$blog->title}}</h2>
<p>{!! $blog->text !!}</p>
</li>
If you want to get a model by the main column you use find not where and the column:
public function show($id)
{
$blog = Blog::find($id);
return view('front.detail',compact('blog'));
}
Then, find can return the Model (hence an object) or null, so in your case it is not finding the model by ID.
What you should use is findOrFail so it throws an exception if it did not find the model:
public function show($id)
{
$blog = Blog::findOrFail($id);
return view('front.detail',compact('blog'));
}
Then you can continue debugging why it is not found
Doesn't appear you are passing the blog ID in with your URL:
Route::resource('/blogs', MainController::class)->only([ 'show']);
Try changing your route to this:
Route::resource('/blogs/{id}', MainController::class)->only([ 'show']);
Your URL should be something like (where 5 is the blog ID):
http://yourdomain.com/blogs/5
You could also use binding:
// Route
Route::get('/blogs/{blog}', [MainController::class, 'show']);
public function show(Blog $blog)
{
return view('front.detail', ['blog' => $blog]);
}
More info can be found here:
Route model binding

Trying to get property 'id' of non-object in Laravel 8

I have a page of posts. I added an edit icon so that when I click on it, it should show me the edit page. However, when I click, it displays an error.
Blade/Form
<form method="POST" action="{{ route('post.update',
['id'=>$post->id]) }}" enctype="multipart/form-data">
#csrf
Route
Route::get('post/edit/{id}', 'PostController#edit')
->name('post.edit');
The following is the edit() method inside the PostController.
public function edit($id)
{
$post = Post::find($id);
return view('posts.edit')->with('post', $post);
}
Try using findOrFail to throw a 404 if you pass an id that doesn't exist.
public function edit($id)
{
$post = Post::findOrFail($id);
return view('posts.edit')->with('post', $post);
}
or bind the model in the route
Route::get('post/edit/{post}', 'PostController#edit')->name('post.edit');
public function edit(Post $post)
{
return view('posts.edit', compact('post'));
}

Laravel: undefined variable posts within website

Really struggling here, trying to display the posts on my page, bare in mind these posts are displaying on other pages. However i cannot display them on my index.php page.
welcome.blade.php
#if(count($posts) > 1)
#foreach($posts as $post)
<h2>{{$post->title}}</h2>
#endforeach
#else
</p>no posts found</p>
#endif
WelcomeController.php
public function index()
{
$posts = Post::all();
return view('Pages.welcome')->with('posts', $posts);
}
PostsController.php
public function index()
{
$posts = Post::all();
return view('posts.index')->with('posts', $posts);
}
Web.php (routes)
Route::get('/', 'PageController#index');
Route::get('/welcome','PageController#Welcome');
Route::get('/services', 'PageController#services');
Route::get('/register', 'PageController#register');
Route::get('/Create', 'PageController#Create');
Route::get('/search', 'PageController#search');
Route::get('/payment', 'PageController#Payment');
Route::resource('posts', 'PostsController');
Route::resource('search', 'SearchController');
Route::resource('reviews', 'ReviewsController');
HomeController.php
public function __construct()
{
$this->middleware('auth');
}
/**
* Show the application dashboard.
*
* #return \Illuminate\Contracts\Support\Renderable
*/
public function index()
{
return view('home');
}
I have put postscontroller because this controller works and displays data to other pages (posts.index), however when i try to display data on Pages.index i am unable to do so ?
Make sure you are accessing the right path on your URL. In this case you should access localhost:8000/welcome .
In your route
Route::get('/index','PageController#HomePage');
Shoud be
Route::get('/welcome','WelcomeController#index');
because your controller file name is WelcomeController.php and you need to redirect to your index function in that controller.
Make sure you are redirecting to the right view in your controller
public function index()
{
$posts = Post::all();
return view('Pages.welcome', compact('posts'));
}
Here you are seeking the welcome.blade.php file in your /Pages directory
Your controllers and routes are fine but you miss-spell foreach
#foreac h($posts as $post)
Replace with :
#foreach($posts as $post)
now this should work

Is it possible with laravel to go from post.store to post.edit?

I can't figure out how to go from a store method directly to a edit page.
Routes:
Route::post('/', 'PostController#store')->name('posts.store');
Route::get('/', 'PostController#edit')->name('posts.edit');
post controller:
public function store(Request $request)
{
$post = new Post;
$post->save();
return view('posts.edit');
}
public function edit($id)
{
dd('edit post');
}
I keep getting view not found or other errors. I have checked php artisan route:list and the correct route is there. What am i missing here?
Well you should do a redirect to the route instead. See Redirecting to Named Routes
So this should work:
return redirect()->route('posts.edit');
At this point, you are trying to load a view.

Laravel 5 User Permissions

I am new to laravel (5.2) and followed this great series https://www.youtube.com/watch?v=Zxmf0n2sC1I&index=34&list=PLwAKR305CRO-Q90J---jXVzbOd4CDRbVx
Can somebody point me into the right direction how to setup the authentication that registered users can only edit / delete their OWN posts.
e.g: Logged in User "A" is not allowed to edit Posts from User B.
Thanks for helping me out.
You can use the ApiGuard. See more: https://github.com/chrisbjr/api-guard
Great. This works perfectly for edit. However something goes wrong with my index view (where all posts are listed). My Code looks like:
public function index($id)
{
//create a var and store all blog posts from DB
$posts = Post::findOrFail($id);
if($posts->id !== Auth::user()->id){
abort(403, 'Access denied');
}
//return a view and pass in the above var
return view('posts.index')->withPosts($posts);
}
The error message in my view is: ErrorException in PostController.php line 23:
Missing argument 1 for App\Http\Controllers\PostController::index()
I am not sure what to do with the index($id), where ca I grab the id from?
Thanks again
If your posts table has an user_id then you can check if that user is the same as the logged user. For example:
routes.php
Route:get('post/{id}/edit', PostsController#edit);
PostsController.php
class PostsController extends Controller{
public function edit($id){
$post = Post::findOrFail($id);
if($post->user_id !== Auth::user()->id){
abort(403);
}
return view('posts.edit', $post);
}
}
EDIT: Updated including an index method as requested in the comments.
routes.php
Route:get('posts', PostsController#index);
Route:get('post/{id}/edit', PostsController#edit);
PostsController.php
class PostsController extends Controller{
public function index(){
$posts = Post::all();
return view('posts.index', $posts);
}
public function edit($id){
$post = Post::findOrFail($id);
if($post->user_id !== Auth::user()->id){
abort(403);
}
return view('posts.edit', $post);
}
}
I your Post has an user_id then you can check if that user is the same as the loggin user. The code will be
PostController.php
class PostController extends Controller
{
public function getUpdatePost($post_id) {
$post = Post::find($post_id);
if($post->user_id !== Auth::user()->id){
abort(403);
}
return view('posts.edit',['post' => $post]);
}
}
and your routes file will be
Route::get('/post/{post_id}/edit', [
'uses' => 'PostController#getUpdatePost',
'as' => 'post.edit'
]);

Resources