Laravel model observer updated & deleted events are not trigerring? - php

Observer
class FileLogObserver
{
public function updated(FileLogs $fileLogs)
{
$fileChangeLogs = FileChangeLogs::firstWhere('fileId', $fileLogs->filedId);
if ( !empty($fileChangeLogs)) {
$fileChangeLogs->save([
'logDetails' => '1 file updated',
]);
}
}
}
Controller
class FileLogController extends Controller
{
public function update(Request $request,$id){
$validator = Validator::make(
$request->all(),
[
'orderId' => 'required|integer',
'fileId' => 'required|integer',
'status' => 'required|string'
]
);
if ($validator->fails()) {
return response()->json($validator->errors(), 400);
}
$data = FileLogs::find($id);
if($data){
$data->orderId=$request->orderId;
$data->fileId=$request->fileId;
$data->status=$request->status;
$data->update();
return response()->json(['status'=>'success','StatusCode'=> 200,'message'=>'Successfully Updated','data'=>$data]);
}
else{
return response()->json(['status'=>'Failed','message'=>'Update Failed'],400);
}
}
}
The created & retrieved methods are being properly triggered. However, the updated & deleted methods not triggered. Gone through many links & read that a reason can be becoz the update is not directly on the model. so, i tried like below in my controller. But update function is not working this method. I'm using Laravel-8 version. Any help is much appreciated.
$data = FileLogs::find($id);
if($data){
$data->update(['$data->orderId'=>'$request->orderId','$data->fileId'=>'$request->fileId','$data->status'=>'$request->status']);

you need to register those observer in App\Providers\EventServiceProvider
like
/**
* Register any events for your application.
*
* #return void
*/
public function boot()
{
FileLogs::observe(FileLogObserver::class);
}
ref link https://laravel.com/docs/8.x/eloquent#observers

Related

Target Laravelista is not instantiable

I'm trying to post a comment to my news post using the Laravelista\Comments but I got this error: Target [Laravelista\Comments\CommentControllerInterface] is not instantiable.
It was working just fine before, and I didn't change anything except the interface design. Not long after I couldn't post a comments anymore. This is the package I'm using https://github.com/laravelista/comments
Tried adding Laravelista\Comments\ServiceProvider::class, to the config\app.php but it didn't do any change.
Is there any solutions?
So on Laravel 9 there seems to be an issue getting with $this->middleware('auth'); in the constructor in the vendor CommentController. A workaround that is working for me is to basically create a custom controller and move the middleware('auth') to the actual routes instead.
extend the vendor CommentController
Copy the original constructor from there into your new comment controller but remove $this->middleware('auth');
Copy/paste the store, update, destroy, and reply functions from CommentService in the vendor folder into your new controller.
update the config (config\comments.php) to point to your new controller, example controller' => 'App\Http\Controllers\CustomCommentsController',
copy the routes from vendor into your routes file and chain the middleware('auth') to each route there.
Note: I have not tested this with guest commenting enable.
Controller:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Laravelista\Comments\Comment;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Gate;
use Illuminate\Support\Facades\Config;
use Spatie\Honeypot\ProtectAgainstSpam;
use Illuminate\Support\Facades\Validator;
use Laravelista\Comments\CommentController;
class CustomCommentsController extends CommentController
{
public function __construct()
{
if (Config::get('comments.guest_commenting') == true) {
$this->middleware('auth')->except('store');
$this->middleware(ProtectAgainstSpam::class)->only('store');
} else {
// $this->middleware('auth');
}
}
public function store(Request $request)
{
// If guest commenting is turned off, authorize this action.
if (Config::get('comments.guest_commenting') == false) {
Gate::authorize('create-comment', Comment::class);
}
// Define guest rules if user is not logged in.
if (!Auth::check()) {
$guest_rules = [
'guest_name' => 'required|string|max:255',
'guest_email' => 'required|string|email|max:255',
];
}
// Merge guest rules, if any, with normal validation rules.
Validator::make($request->all(), array_merge($guest_rules ?? [], [
'commentable_type' => 'required|string',
'commentable_id' => 'required|string|min:1',
'message' => 'required|string'
]))->validate();
$model = $request->commentable_type::findOrFail($request->commentable_id);
$commentClass = Config::get('comments.model');
$comment = new $commentClass;
if (!Auth::check()) {
$comment->guest_name = $request->guest_name;
$comment->guest_email = $request->guest_email;
} else {
$comment->commenter()->associate(Auth::user());
}
$comment->commentable()->associate($model);
$comment->comment = $request->message;
$comment->approved = !Config::get('comments.approval_required');
$comment->save();
return $comment;
}
/**
* Handles updating the message of the comment.
* #return mixed the configured comment-model
*/
public function update(Request $request, Comment $comment)
{
Gate::authorize('edit-comment', $comment);
Validator::make($request->all(), [
'message' => 'required|string'
])->validate();
$comment->update([
'comment' => $request->message
]);
return $comment;
}
/**
* Handles deleting a comment.
* #return mixed the configured comment-model
*/
public function destroy(Comment $comment): void
{
Gate::authorize('delete-comment', $comment);
if (Config::get('comments.soft_deletes') == true) {
$comment->delete();
} else {
$comment->forceDelete();
}
}
/**
* Handles creating a reply "comment" to a comment.
* #return mixed the configured comment-model
*/
public function reply(Request $request, Comment $comment)
{
Gate::authorize('reply-to-comment', $comment);
Validator::make($request->all(), [
'message' => 'required|string'
])->validate();
$commentClass = Config::get('comments.model');
$reply = new $commentClass;
$reply->commenter()->associate(Auth::user());
$reply->commentable()->associate($comment->commentable);
$reply->parent()->associate($comment);
$reply->comment = $request->message;
$reply->approved = !Config::get('comments.approval_required');
$reply->save();
return $reply;
}
}
Routes:
Route::post('comments', [Config::get('comments.controller'), 'store'])->middleware('auth')->name('comments.store');
Route::post('comments/{comment}', Config::get('comments.controller') . '#reply')->middleware('auth')->name('comments.reply');
Route::delete('comments/{comment}', Config::get('comments.controller') . '#destroy')->middleware('auth')->name('comments.destroy');
Route::put('comments/{comment}', Config::get('comments.controller') . '#update')->middleware('auth')->name('comments.update');

Adding filter to eloquent resource to attach relationship conditionally

Laravel 5.8
PHP 7.4
I want to load the relationships conditionally like
http://127.0.0.1:8000/api/posts
and
http://127.0.0.1:8000/api/posts/1 are my end points now, I want to load comments like
http://127.0.0.1:8000/api/posts/?include=comments and
http://127.0.0.1:8000/api/posts/1/?include=comments
If the query parameter is there, only then it should load comments with posts or it should load only posts/post
I am doing this by referring a blog post
now, RequestQueryFilter
<?php
namespace App\Http\Resources;
use Illuminate\Http\Request;
class RequestQueryFilter
{
public function attach($resource, Request $request = null)
{
$request = $request ?? request();
return tap($resource, function($resource) use($request) {
$this->getRequestIncludes($request)->each(function($include) use($resource) {
$resource->load($include);
});
});
}
protected function getRequestIncludes(Request $request)
{
// return collect(data_get($request->input(), 'include', [])); //single relationship
return collect(array_map('trim', explode(',', data_get($request->input(), 'include', [])))); //multiple relationships
}
}
and in helper
<?php
if ( ! function_exists('filter') ) {
function filter($attach)
{
return app('filter')->attach($attach);
}
}
?>
in PostController
public funciton show(Request $request, Post $post) {
return new PostResource(filter($post));
}
but when I am trying to retrieve
http://127.0.0.1:8000/api/posts/1/?include=comments getting no comments, with no error in log
A work around will be PostResource
public function toArray($request)
{
// return parent::toArray($request);
$data = [
'id' => $this->id,
'name' => $this->title,
'body' => $this->content,
];
$filter = $request->query->get('include', '');
if($filter){
$data[$filter] = $this->resource->$filter;
}
return $data;
}
I want to load the relationships conditionally like
Lazy Eager Loading using the load() call
The Lazy Eager Loading accomplishes the same end results as with() in Laravel, however, not automatically. For example:
?include=comments
// Get all posts.
$posts = Post::without('comments')->all();
if (request('include') == 'comments')) {
$posts->load('comments');
}
return PostResource::collection($posts);
Alternativelly, you could require the include query string to be an array:
?include[]=comments&include[]=tags
// Validate the names against a set of allowed names beforehand, so there's no error.
$posts = Post::without(request('includes'))->all();
foreach (request('includes') as $include) {
$posts->load($include);
}
return PostResource::collection($posts);
The call without() is only required in case you defined your model to automatically eager load the relationships you want to conditionally load.
With all data filtered in Controller, just make sure to display only loaded relations in your PostResource
public function toArray($request) {
$data = [...];
foreach ($this->relations as $name => $relation)
{
$data[$name] = $relation;
}
return $data;
}
I would create a custom resource for the posts with
php artisan make_resource
command.
E.g. PostResource.
The toArray function of the resource must return the data.
PostResource.php
public function toArray($request){
$data =['title' => $this->resource->title,
'body' => $this->resource->body,
'images' => new ImageCollection($this->whenLoaded('images')),
];
$filter = $request->query->get('filter', '');
if($filter){
$data['comments'] => new CommentCollection($this->resource->comments);
}
return $data;
}
Also, for collections, you need to create a ResourceCollection.
PostResourceCollection.php
class PostResourceCollection extends ResourceCollection
{
/**
* Transform the resource into an array.
*
* #param \Illuminate\Http\Request
* #return array
*/
public function toArray($request)
{
return [
'data' => $this->collection,
];
}
}
In your controller:
PostsController.php
//show one post
public function show(Post $post, Request $request)
{
/**this response is for API or vue.js if you need to generate view, pass the resource to the view */
return $this->response->json( new PostResource($post));
}
//list of posts
public function index(Request $request)
{
$posts = Post::all();
/**this response is for API or vue.js if you need to generate view, pass the resource to the view */
return $this->response->json( new PostResourceCollection($posts));
}
Partial Solution
It will need a small change in resource class
public function toArray($request)
{
// return parent::toArray($request);
$data = [
'id' => $this->id,
'title' => $this->title,
'body' => $this->body,
'comments' => new CommentCollection($this->whenLoaded('comments')),
'images' => new ImageCollection($this->whenLoaded('images')),
];
return $data;
}
and it will load comments and images if loaded and that depends on the include query parameter, if that is not included, it will not load the relationship.
However,
In post collection
return [
'data' => $this->collection->transform(function($post){
return [
'id' => $post->id,
'title' => $post->title,
'body' => $post->body,
'comments' => new CommentCollection($post->whenLoaded('comments')),
'images' => new ImageCollection($post->whenLoaded('images')),
];
}),
];
will results in
"Call to undefined method App\Models\Customer::whenLoaded()",, if anyone suggests a complete solution, it will be a great help, if I will able to do, it I will update here.

Laravel Model, deleted callback not being called

im trying to run some additional code when a row is deleted using my Model. However the callback statis::deleted simply isn't being triggered.
Controller:
/**
* #param Website $website
* #param Request $request
* #return \Illuminate\Http\RedirectResponse
* #throws \Exception
*/
public function delete(Website $website, Request $request)
{
$id = $request->input('id-value');
WebsiteRedirects::query()->where(['website_id' => $website['id'], 'id' => $id])->delete();
Session::flash('message', [ 'is-success' => [ '1 Redirect has been deleted!' ] ]);
return back();
}
Model:
class WebsiteRedirects extends Model
{
protected $table = 'website_redirects';
protected $guarded = [];
public $timestamps = false;
protected static function boot()
{
parent::boot();
static::saved(function ($redirect) {
PlannerStatus::status('redirect', $redirect->website_id, 1);
});
static::deleted(function($redirect) {
dd('deleted');
PlannerStatus::status('redirect', $redirect->website_id, 1);
});
}
...
static::saved works fine, and I insert using query too.
WebsiteRedirects::query()->create(
[
'website_id' => $website->id,
'redirect_from' => $request->input('redirect-from'),
'redirect_to' => $request->input('redirect-to')
]
);
The event is not being called because you are not deleting the row via Eloquent. You are deleting the row directly, without fetching the result - therefor Eloquent can't run the deleted event.
You will have to fetch the model before deleting for the event to be triggered.
WebsiteRedirects::where(['website_id' => $website['id'], 'id' => $id])->first()->delete();
Add first() to retrieve the WebsiteRedirect before you run delete()
In your code
WebsiteRedirects::query()->where(['website_id' => $website['id'], 'id' => $id])
right before the delete() method, the instance of the object is Illuminate\Database\Eloquent\Builder not your model. wich will trigger the Eloquent delete (DB) not your model's one.
Normaly you would do something like:
$user = User::find($id);
$user->delete();

REST api not working

I am new to php development. Just for practicing I am creating a rest API following a video tutorial. I have followed each and every step but still unable to get the desired result. Below is the code
Employee Model
class Employee extends \yii\db\ActiveRecord
{
const SCENARIO_CREATE = 'create';
/**
* #inheritdoc
*/
public static function tableName()
{
return 'employee';
}
/**
* #inheritdoc
*/
public function rules()
{
return [
[['emp_name', 'emp_email', 'emp_sal'], 'required'],
[['emp_name', 'emp_email', 'emp_sal'], 'string', 'max' => 100],
];
}
public function scenarios()
{
$scenarios = parent::scenarios();
$scenarios['create'] = ['emp_name','emp_email', 'emp_sal'];
return $scenarios;
}
/**
* #inheritdoc
*/
public function attributeLabels()
{
return [
'id' => 'ID',
'emp_name' => 'Emp Name',
'emp_email' => 'Emp Email',
'emp_sal' => 'Emp Sal',
];
}
}
Above the ID field is auto-increment
Employee Controller
public function actionCreateEmployee()
{
\Yii::$app->response->format= \yii\web\Response::FORMAT_JSON;
$employee = new Employee();
$employee-> scenario = Employee::SCENARIO_CREATE;
$employee->attributes = \Yii::$app->request->post();
if ($employee->validate())
{
return array('status'=> true, 'data' => 'Employee Created Sussessfully');
}
else
{
return array('status'=> false, 'data'=>$employee->getErrors());
}
//return array('status'=> true);
}
Now when I run the API in Postman. I got the following result.
Though I have entered all the required fields data still it gives me false status
Any help would be highly appreciated
You need to select x-www-form-urlencoded
The documentation says that the $_POST-parameter only gets filled on application/x-www-form-urlencoded or multipart/form-data and yii is probably using this.
An associative array of variables passed to the current script via the HTTP POST method when using application/x-www-form-urlencoded or multipart/form-data as the HTTP Content-Type in the request.
from php.net

Laravel 4 - redirect from a repository when not returning the called method

I am using a repository pattern in my Laravel 4 project but come across something which I think I am doing incorrectly.
I am doing user validation, before saving a new user.
I have one method in my controller for this:
public function addNewUser() {
$validation = $this->userCreator->validateUser($input);
if ( $validation['success'] === false )
{
return Redirect::back()
->withErrors($validation['errors'])
->withInput($input);
}
return $this->userCreator->saveUser($input);
}
Then the validateUser method is:
public function validate($input) {
$rules = array(
'first_name' => 'required',
'last_name' => 'required',
'email_address' => 'unique:users'
);
$messages = [
];
$validation = Validator::make($input, $rules, $messages);
if ($validation->fails())
{
$failed = $validation->messages();
$response = ['success' => false, 'errors' => $failed];
return $response;
}
$response = ['success' => true];
return $response;
}
This may be okay, but I dont like doing the if statement in my controller? I would rather be able to handle that in my validation class.
But to be able to redirect from the validation class, I need to return the method in the controller.
What if I then want to have 5 methods called, I cant return them all?
I would like to be able to simply call the methods in order, then in their respective class handle what I need to and if there is any errors redirect or deal with them. But if everything is okay, simply ignore it and move to the next function.
So example:
public function addNewUser()
{
$this->userCreator->validateUser($input);
$this->userCreator->formatInput($input);
$this->userCreator->sendEmails($input);
return $this->userCreator->saveUser($input);
}
If doing the if statement in the controller isn't as bad as I think then I can continue, but this seems incorrect?
For repository pattern, you can use this :-
setup your basemodel like this
<?php namespace App;
use Illuminate\Database\Eloquent\Model;
class BaseModel extends Model{
protected static $rules=null;
protected $errors=null;
public function validateForCreation($data)
{
$validation=\Validator::make($data,static::$rules);
if($validation->fails())
{
$this->errors=$validation->messages();
return false;
}
return true;
}
/**
* #return errors
*/
public function getErrors() { return $this->errors; }
}
now in your repository, add these methods
protected $model;
protected $errors=null;
public function model(){ return $this->model; }
public function getErrors(){ return $this->errors; }
public function create($inputs)
{
if(!$this->model->validateForCreation($inputs))
{
$this->errors=$this->model->getErrors();
return false;
}
$new=$this->model->create($inputs);
return $new;
}
and the controller will look like this..
public function postCreate(Request $request)
{
$inputs=$request->all();
if($new=$this->repo->create($inputs))
{
return redirect()->back()
->with('flash_message','Created Successfully');
}
return redirect()->back()->withInput()->withErrors($this->repo->getErrors())
->with('flash_message','Whoops! there is some problem with your input.');
}

Categories