form model Method not allow Error - laravel

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

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

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

How to display messages received from other users using Laravel

I am working on a messaging functionality on my website between two users and I have managed to make the messaging system work quite alright. But I have a little issue which I can't find a solution to it. I want to be able to show, A list of all message received from other users when the user clicks on this route /conversations. But right now what it does is that it display a list of all users in the users table when I click on the route /conversations which I don't want.
Here are my routes in web
Route::get('/conversations', 'ConversationsController#index')->name('conversations');
Route::get('/conversations/{user}', 'ConversationsController#show')->name('conversations.show');
Route::post('/conversations/{user}', 'ConversationsController#store');
Here is my list of conversations route for index
<div class="contact-edit" id="ci">
<h3>MEssages</h3>
#include('conversations.users', ['users'=>$users, 'unread'=>$unread])
</div>
Here is the conversation.users
<div class="col-md-3">
<div class="list-group">
#foreach($users as $user)
<a class = "list-group-item d-flex justify-content-between align-items-center" href="{{route('conversations.show', $user->id)}}">
{{ $user->name }}
#if(isset($unread[$user->id]))
<span class="badge-pill badge-primary">{{$unread[$user->id]}}</span>
#endif
</a>
#endforeach
</div>
Here is my route opening the conversations tab
Ecrire un message
Here is my conversation controller
class ConversationsController extends Controller
{
private $r;
private $auth;
public function __construct(ConversationRepository $conversationRepository, AuthManager $auth)
{
$this->r = $conversationRepository;
$this->middleware('auth');
$this->auth = $auth;
}
public function index (User $user){
$me = Auth::user();
//dd(Auth::user());
return view('conversations/index', [
'users'=>$this->r->getConversations(Auth::user()->id),
'unread' => $this->r->unreadCount(Auth::user()->id)
]);
}
public function show (User $user){
$me = Auth::user();
$messages = $this->r->getMessagesFor($me->id, $user->id)->paginate(5);
$unread = $this->r->unreadCount($me->id);
if (isset($unread[$user->id])) {
$this->r->readAllFrom($user->id, $me->id);
unset($unread[$user->id]);
}
return view('conversations/show', [
'users'=>$this->r->getConversations($me->id),
'user'=>$user,
'messages'=>$messages,
'unread' => $unread
]);
}
public function store (User $user, StoreMessageRequest $request){
$message = $this->r->createMessage(
$request->get('content'),
Auth::user()->id,
$user->id
);
$user->notify(new MessageReceived($message));
return redirect(route('conversations.show', ['id'=>$user->id]));
}
}
And here is my conversation repository
class ConversationRepository
{
private $user;
private $message;
public function __construct(User $user, Message $message)
{
$this->user = $user;
$this->message = $message;
}
public function getConversations(int $userId)
{
$conversations = $this->user->newQuery()
->select('name','surname','photo','id')
->where('id', '!=', $userId)
->whereType('jobber')
->get();
return $conversations;
}
public function createMessage(string $content, int $from, int $to)
{
return $this->message->newQuery()->create([
'content'=>$content,
'from_id'=>$from,
'to_id'=>$to,
'created_at'=>\Carbon\Carbon::now()
]);
}
public function getMessagesFor(int $from, int $to) : Builder
{
return $this->message->newQuery()
->whereRaw("((from_id = $from AND to_id = $to )OR(from_id = $to AND to_id = $from))")
->orderBy('created_at', 'DESC')
->with([
'from'=> function ($query){ return $query->select('name', 'id');}
]);
}
//Recupere le nombre de message non lu pour chaque conversation
public function unreadCount (int $userId)
{
return $this->message->newQuery()
->where('to_id', $userId)
->groupBy('from_id')
->selectRaw('from_id, COUNT(id) as count')
->whereRaw('read_at IS NULL')
->get()
->pluck('count', 'from_id');
}
THis is my user model
public function messages()
{
return $this->hasMany('App\Message', 'from_id', 'to_id', 'content');
}
This is message model
public function user()
{
return $this->belongsTo('App\User');
}
Any help will be welcome
I'm not sure I am understanding what you want to do but here is a possible solution assuming you want to get all messages that are to you.
In your repository:
$this->message->where('to_id',Auth::user()->id)->get();

How to Insert id from a Name in Laravel 5.3 Combobox

I want to insert the id of the supplier by choosing the name of the supplier from a combo box.
View
<label for="supplier">Supplier</label>
<input list="supplier" name="supplier" placeholder="Select Supplier" class="form-control">
#foreach($suppliers as $key=>$value)
<datalist id="supplier">
<option value="{{$key}}">{{$value}}
</datalist>
#endforeach
Controller
<?php
public function store(Request $request, User $user)
{
$user = Auth::user();
$product = new Product;
$request->user()->products()->create($request->all());
}
Model
protected $fillable = ['name', 'qty', 'bprice', 'sprice', 'edate'];
public function user()
{
return $this->belongsTo(User::class, user_id);
}
use this
public function store(Request $request, User $user)
{
$user = Auth::user();
$product = new Product;
$product->supplier_id = $request->supplier;
$product->price = $request->price; // if u hv it
. // finish the rest then
$user->products()->save($product);
// finish the rest
}

Resources