access the edit function inside my controller - laravel

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

Related

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

How to use index method to display a listing of the resource in Laravel

I created a customerController with Laravel resource. Now I want to show the list of the customers from the database using the index() method and it doesn't work when I visit the customer/index route. However if use the show() method instead and visit the customer/show route, it works perfectly.
Why does this behave this way and how do I get the index() method to do this?
class CustomerController extends Controller
{
public function index()
{
$customers = Customer::all();
return view('customer')->with('customers' , $customers);
}
public function show($id)
{
// adding the code in the index() method here makes the code run
// as expected
}
}
customer.blade.php
<ul>
#foreach($customers as $customer)
<li>{{$customer->name}}</li>
#endforeach
</ul>
routes/web.php
Route::resource('customer' , 'CustomerController');
I expect the output to be:
.sucre
.hameed
.micheal
I just learned how to use resource on Laravel 8.
Which version are you using?
Run php artisan route:list on your console to see every route available and their names.
To see all customers through your index function do the following:
public function index()
{
$customers = Customer::all();
return view('customer', [
'customers' => $customers
]);
}
Now just access your customer.blade.php file on browser.
PS: Don't forget of #foreach
#foreach ($customers as $customer)
{{ $customer->attribute }}
#endforeach
Hope this could be useful.

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

form model Method not allow Error

Wanted to update the entry but getting this error.
I am using laravel 5.4
Please help
1) web.php
Route::resource('employeeTask','employeeTaskController');
2) employeeTaskController
public function update(Request $request, $id)
{
//
$task = task::find($id);
$task->userAssigned = $request->employeeName_txt;
$task->title = $request->title_txt;
$task->description = $request->description_txt;
$task->client = $request->clientName_txt;
$task->completionDate = date('Y-m-d',strtotime($request->completionDate_txt));
$task->status = $request->status_dd;
$task->save();
return redirect('employeeTask')->with('message','task has been Updated Successfully');
}
3) editTaskPage.blade
{!!Form::model($task,['method'=>'PUT','route'=>['employeeTask.update',$task->id], 'class' => 'well form-horizontal']) !!}
some code
{!! Form::close() !!}
Thanks in Advance
Your function requires an argument that is id
public function update(Request $request, $id)
Instead do this
for form
'method'=>'POST'
public function update(Request $request)
{
$task = task::find($request->id);
And add html
<input class="hidden" value="{{$id}}" name="id" />
And route
Route::post('employeeTask','employeeTaskController#update');

Laravel 5.3 How to use multiple controllers in one view

I'm working with a view that at first displays all products, and on the sidebar, users can see a list of categories. My aim is when users press on any categories, it will display products of that category only. However, since there are 2 controllers is interacting with this view and send the same data, one is not running(CategoriesController). It does not show any error, just when I click on the link of each of the categories, it does not reload the page.
Here is my ProductsController:
class ProductsController extends Controller
{
// display all products and categories
public function index() {
$products = Product::all();
$categories = Category::all();
return view('frontend.product', compact('products','categories'));
}
And my CategoriesController:
class CategoriesController extends Controller
{
public function showProducts($category_id) {
$products = Category::find($category_id)->products;
return view('frontend.product', compact('products'));
}
Here is my view:
// Products part
#foreach($products as $product)
{{ $product->name }}
#endforeach
//Categories part
#foreach($categories as $category)
{{ $category->name }}
#endforeach
And route:
Route::get('/products', [ 'as' => 'products', 'uses' => 'frontend\ProductsController#index']);
Route::get('/categories/{category_id}', ['as' => 'categories', 'uses' => 'backend\CategoriesController#showProducts']);
Just include all of the categories in your showProducts function, and omit the current category:
public function showProducts($category_id)
{
$categories = Category::whereNotIn('id', $category_id)->get();
$products = Category::find($category_id)->products;
return view('frontend.product', compact('products', 'categories'));
}
Now there will be no discrepancies between the variables that are being used.
If I have not misunderstood, I thought you can use #inject in blade. For example:
namespace App\Presenters;
use App\User;
class UserPresenter
{
/**
* 是否顯示email
* #param User $user
* #return string
*/
public function showEmail(User $user)
{
if ($user->show_email == 'Y')
return '<h2>' . $user->email . '</h2>';
else
return '';
}
}
and
<div>
#inject('userPresenter', 'MyBlog\Presenters\UserPresenter')
#foreach($users as $user)
<div>
{!! $userPresenter->showEmail($user) !!}
</div>
</div>

Resources