Get the $request object in Laravel's model events (ex. created, updated etc.)

Asked
Active3 hr before
Viewed126 times

7 Answers

createdrequestobject
90%

I am using Eloquent's model events to do some actions after model has been created/updated/deleted. I'm refering to these:, Stack Overflow for Teams Where developers & technologists share private knowledge with coworkers ,The question: I need to access the $request object in any of these. Is it possible? The scenario I am trying to do is to insert some values in another database table once the current model has been stored. The values are in the $request. And I don't have access to the controller to do it manually there., You could create a model from the request then assign it to the model statically then you could use in the boot() method. This makes the model less coupled with the system. – ka_lin Jul 24 '20 at 14:59

There is request helper in laravel. You can use Request Object anywhere. For example

request() - > field_name
88%

Here, i want to know you how to create Eloquent models events for creating, updating, deleting, retrieved, created, updated, saving, saved, deleted, restoring, restored in PHP Laravel 5. here i will show you simple and quick example for event fire on eloquent model method. Using this tutorial you will learn to create laravel event listener, also can set queue.,Laravel full calendar tutorial example from scratch,Laravel introduce very unique and fantastic feature event for your each task of database. Laravel Events allow you to easily execute code each time a specific model class is saved, updated, deleted, restored etc in the mysql database. If you are new in laravel then you think how to built it but here you can see how it's easy to implement events for each task. It is very useful concept. So you can also add your activity log by using events.,First thing is we have to create "items". so we have to create migration for items table using Laravel 5.5 php artisan command, so first fire bellow command:

php artisan make: migration create_items_table
load more v
72%

Laravel includes Eloquent, an object-relational mapper (ORM) that makes it enjoyable to interact with your database. When using Eloquent, each database table has a corresponding "Model" that is used to interact with that table. In addition to retrieving records from the database table, Eloquent models allow you to insert, update, and delete records from the table as well.,As we have seen, Eloquent methods like all and get retrieve multiple records from the database. However, these methods don't return a plain PHP array. Instead, an instance of Illuminate\Database\Eloquent\Collection is returned.,You may call the truncate method to delete all of the model's associated database records. The truncate operation will also reset any auto-incrementing IDs on the model's associated table:,Of course, when using Eloquent, we don't only need to retrieve models from the database. We also need to insert new records. Thankfully, Eloquent makes it simple. To insert a new record into the database, you should instantiate a new model instance and set attributes on the model. Then, call the save method on the model instance:

To get started, let's create an Eloquent model. Models typically live in the app\Models directory and extend the Illuminate\Database\Eloquent\Model class. You may use the make:model Artisan command to generate a new model:

php artisan make: model Flight
load more v
65%

There are a bunch of events that happen in your eloquent objects:,Please let me know in the comments if there are any useful features of Laravel that you think are underused, that I've not mentioned. I'm sure there are tons!,$snakeAttributes: If a model's relationships have snake-casing enabled, Eloquent will snake case the keys so that the relation attribute is snake cased in the returned array to the developers.,$incrementing: If you have a table without auto-incrementing ID rows then you will want to set the following to false:

Let's say you want to do the following SQL query:

< ? php
select * from `users`
where
   `name` = 'some_name'
and `email` = 'some_email'
limit 1

You can achieve this with one 'where' method call. Eloquent will work out that the "and" in the middle means two seperate where clauses:

< ? php\ App\ User::whereNameAndEmail('some_name', 'some@email') - > first();
// the above has the exact same result as:
\
App\ User::where('name', 'some_name') - > where('email', 'som@_email') - > first();
// also same as:
\
App\ User::where(['name' => 'some_name', 'email' => 'some@email']) - > first();

And as well as "and", you can also do an "or" like this:

< ? php\ App\ User::whereFooOrBar('foo value', 'bar value') - > first();

Which will produce the following SQL query:

< ? php
select * from `users`
where
   `foo` = 'foo value'
or `bar` = 'bar value'
limit 1
load more v
75%

You can get table name simply call getTable() of model object, so how to call this method as bellow example.,Laravel Soft Delete Example,Example of Cron Job in Laravel 5,Laravel Eloquent Delete Record By ID Example

$item = new Item;
$table = $item - > getTable();
print_r($table);
40%

A Model can have properties like table, fillable, hidden, etc which defines properties of the table and model., 8. How to put Laravel applications in maintenance mode? ,Which command is used to flush the application cache?, 1. What is the latest Laravel version?

For example, one of the most used packages for authentication will be Passport, for including that into your project, you can run the below command on your terminal:

composer requires laravel / passport

It generates a file(composer.json) in your project directory to keep track of all your packages. A default composer.json file of your laravel project will look something like below:

{
   "name": "laravel/laravel",
   "type": "project",
   "description": "The Laravel Framework.",
   "keywords": [
      "framework",
      "laravel"
   ],
   "license": "MIT",
   "require": {
      "php": "^7.3|^8.0",
      "fideloper/proxy": "^4.4",
      "fruitcake/laravel-cors": "^2.0",
      "guzzlehttp/guzzle": "^7.0.1",
      "laravel/framework": "^8.12",
      "laravel/tinker": "^2.5"
   },
   "require-dev": {
      "facade/ignition": "^2.5",
      "fakerphp/faker": "^1.9.1",
      "laravel/sail": "^1.0.1",
      "mockery/mockery": "^1.4.2",
      "nunomaduro/collision": "^5.0",
      "phpunit/phpunit": "^9.3.3"
   }
}
load more v
22%

Now let’s go back to our model and add those attributes to the $fillable field so that we can use them in our Article::create and Article::update models:,The -m option is short for --migration and it tells Artisan to create one for our model. Here’s the generated migration:,The seeders will be located in the /database/seeds directory. Here’s how it looks like after we set it up to create a few articles:,Resources will be the targets of the actions, in our case Articles and Users, and they have their own endpoints:

As with all modern PHP frameworks, we’ll need Composer to install and handle our dependencies. After you follow the download instructions (and add to your path environment variable), install Laravel using the command:

$ composer global require laravel / installer
load more v

Other "created-request" queries related to "Get the $request object in Laravel's model events (ex. created, updated etc.)"