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

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'));
}

Related

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

access the edit function inside my controller

Hi guys new to laravel here. I am trying to edit my post and when i try to access the edit function in my controller i get this error Property [id] does not exist on this collection instance.
This is My route code
Route::resource('/article','PostController');
This is my Controller
public function edit(Post $post)
{
$post = Post::all();
return view('article.edit',compact('post'));
}
This is my View code
<a href="{{route('article.edit', $post->id)}}" class="btn btn-info btn-sm btn-bordred wave-light">
<i class="fas fa-edit"></i></a>
And solution ? Thanks in advance
When you use "all", you will get all data from post table. In edit function you get $id as a parameter.
public function edit($id)
{
$post = Post::find($id);
return view('article.edit',compact('post'));
}
you are passing collection to your view instead of single post
public function edit(Post $post)
{
$post = Post::all(); // this is a collection of posts
return view('article.edit',compact('post'));
}
it should be:
public function edit(Post $post)
{
// here $post holds the instance of single/current post
return view('article.edit',compact('post'));
}
Then in your blade
<a href="{{route('article.edit', ['post' => $post->id ])}}" class="btn btn-info btn-sm btn-bordred wave-light">
<i class="fas fa-edit"></i></a>
Note: Since you are passing $post as instance of the model so therefor you don't have to use Post::find(); laravel will take care of that automatically at back-end
Thanks
Post::all() return a Collection (multiple Models)
You need to pass the id in the edit($id) function and then you can find($id)
public function edit($id){
$post = Post::find($id);
return view('article.edit')->with('post', $post);
}
That's because the variable $post holds a Laravel Collection. You have to iterate on that collection to access each post model.
// Returns a collection of posts model (think of
// it as an array of posts)
$post = Post::all();
On your blade, use a foreach to iterate through each post.
Edit
I see that you already have an instance of post available, in that case just use this code
public function edit(Post $post)
{
return view('article.edit', ["post" =>$post]);
}

Display reviews for a specific restaurant Laravel

I am trying to display reviews and ratings on a restaurant profile.
<div>
<h1>Reviews</h1>
#foreach($reviews as $review)
<hr>
<div class="row">
<div class="col-md-12">
#for ($i=1; $i <= 5 ; $i++)
<span class="glyphicon glyphicon-star{{ ($i <= $review->rating) ? '' : '-empty'}}"></span>
#endfor
{{ $review->user ? $review->user->name : 'Anonymous'}}
<p>{{{$review->value}}}</p>
</div>
</div>
#endforeach
</div>
Error
Undefined variable: reviews (View:
C:\xampp\htdocs\restaurantFinder\resources\views\restaurants\viewer.blade.php)
Reviews Controller
class ReviewsController extends Controller
{
public function index()
{
//
}
public function create()
{
//
}
public function store(Request $request)
{
if (!Auth::check()) {
return redirect('/index');
}
$review = new Review;
$review->user_id = auth()->user()->id;
$review->restaurant_id = $request->get('restaurant_id');
$review->value = $request->input('value');
$review->rating = $request->input('rating');
$review->save();
}
public function show($restaurant)
{
// $restaurant=Restaurant::find($id);
return view('restaurants.review', compact('restaurant'));
}
public function edit($id)
{
//
}
public function update(Request $request, $id)
{
//
}
public function destroy($id)
{
//
}
}
you are looking in the wrong place. to track the problem start with your route file. Search for the URL in the web.php file you will find the route there. ones you get the route, you will also get which controller and action are making this page render.
this issue is shown when you use a variable in a blade template but do not set it in the controller for use. In the controller, you can set this variable and you are good to go.
hope this help. incase of any issue feel free to ask.
update your ReviewsController.php
public function show($restaurantID)
{
$reviews = Reviews::find($restaurantID); // Find all reviews model by restaurantID
return view('viewer',compact('reviews ')); // This will do $reviews = reviews
// now you can foreach over reviews in your blade
}
this Link is useful

Post route not working and returns no errors in laravel

I am working on a multistep form in laravel. When I click next button after filling the first page, the same page is refreshed and it doesn't go to next page. It was working fine properly but suddenly it is working like this (getting refreshed).
My web.php has a route like this:
Route::get('register', 'registercontroller#page1')
Route::post('register', 'registercontroller#postpage1')
in register controller, I have defined functions like this
public function page1(Request $request)
{
$request->session()->flush();
$info = $request->session()->get('Register');
return view('register',compact('Register', $info));
}
public function postpage1(Request $request)
{
/* business logic here */
}
My register.blade.php has form like this:
<form action="register" method="POST">
#csrf
<input type="text" name="name">
...
...
...
</form>
However when I tried route in web.php like this:
Route::post('register', 'registercontroller#test')
and in registercontroller function as
public function test(){
echo "test";
}
It works fine...
All this I am working on local environment using xampp
Please help someone.. thanks
In your register controller you will need to return another view or redirect:
public function page1(Request $request)
{
$request->session()->flush();
$info = $request->session()->get('Register');
return view('register',compact('Register', $info));
}
public function postpage1(Request $request)
{
/* business logic here */
return redirect('register-2');
// or
return view('register-2');
}

Laravel Edit link doesn't work

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');

Resources