CakePHP 3 sessions table in database - session

I have some questions regarding having CakePHP 3 sessions table stored in the database:
1) Is there a way to remname the table from sessions to a different name? If yes, where should I specify the new name?
2) Similarly to question 1: Is there a way to remname the names of colums in sessions table, so that CakePHP would still operate correctly?
3) Is there a simple way to add even the most basic encription of the data column of sessions table?

So I will cite the database session docs and then also provide some answers. The answers in the docs are more complete.
Specify the model you want to use by including a 'model' key.
'Session' => [
'defaults' => 'database',
'handler' => [
'engine' => 'DatabaseSession',
'model' => 'MyCustomSessions'
]
]
The DatabaseSession handler appears to hard code 'data' and some of the other columns. However, you can (and should) extend that class if you want to do something special
Based on what you want to do it appears creating your own SessionHandler is the way to go. It is quite simple to do and it is outlined in the docs.
My best advice would to take a peek at cake's DatabaseSession. It seems that your requirements are special and it is probably your best course of action.

Related

Saving data to database in unit testing

Why it's impossible to create dummy data in unit testing like below?
DB::table('users')->insert([
[
'name' => 'test',
],
I saw that in order to create data you need to create factory, but when I try to insert data like this:
User::factory()->count(1)->create([
'name' => 'test'
]);
It also don't insert new data.
What is the easiest way to to that? Thank you
No need for a factory, and you dont need to use DB if you a User model setup. Laravel is great for these types of things and I recommend reading the docs here
For you problem its as simple as this
User::create(['name' => 'test']);

Laravel: how to change the data of all models?

Currently the system has a few dozen models, controllers and a few hundred routes.
When carrying out any query in the database, if a certain value is found during that query, I can transform this data into another value.
An example to facilitate understanding is, when performing the query and before presenting the result, a hashtag is found, that hashtag is replaced with another value.
In this example, the difficulty is not to change the value itself (str_replace()), but to be able to intercept any of the results of queries to the database, search for this "keyword" and replace it.
But this change is only visual, it is not replacing the data in the database.
Of course I can do this on each controller, but due to the quantity, I don't think anything is viable
I think I need to somehow be able to intercept all the results of any consultation with the database and make this substitution, but I have no idea if I should use Middleware or another Laravel resource, or even that should be done by a ServiceProvider.
You can achieve this using Laravel's Resources.
You could create a HashtagResource and run all model results through it prior to utilising the data which could then look for the hashtag and replace it within the content as described.
For example, inside of the newly generated resource that wraps around your query, ie HashtagResource::collection(MyModel::all())
public function toArray($request)
{
return [
'id' => $this->id,
'title' => $this->title,
'content' => str_replace('#hashtag','#otherhashtag', $this->content)
];
}

how to override index query during runtime and update the table laravel-nova

in my use case, I need more than one text box for searching in more than two fields in the index view at the same time, because there too much data, laravel-nova provide only one out of the box, so if there is a way that I can add search in a card and update the index query in run time it will be great, and if there are any other solutions for this problem I would appreciate it.
Laravel Migration:
Create the migration files to set up index queries.
In the function, wherever you needed, use the below command to run the migration.
Artisan::call('migrate', array('--path' => 'app/migrations', '--force' => true));
Here, you can also specify the migration file name like,
Artisan::call('migrate', array('--path' => 'app/migrations/create_index.php', '--force' => true));

Eloquent ORM Polymorphic relationship

I'm trying to build a role-based user management but segmented by resource of many types.
I have this relational model:
I need to show in the user profile management view something like this:
But I don't want to make queries like:
RoleUser::where('user_id',1)->get();
I would like to use the many-to-many polymorphic relationship to take advantage of the Eager/Lazy loading of Laravel, but I don't know to do it.
An other interesting feature to take into account, is that I don't want to store in the database the types like App\Models\Event, App\Models\Article or App\Models\Photo but the map is not working well for me (because the relationships aren't set properly).
//AppServiceProvider::boot()
Relation::morphMap([
'event' => Event::class,
'race' => Race::class,
]);
Any idea?
Try this
//AppServiceProvider::boot()
Relation::morphMap([
'event' => App\Models\Event::class,
'race' => App\Models\Race::class,
]);

best way for bulk insertion validation laravel 5

i have problem validating a bulk insertion in laravel 5 the scenario that i have is as following :
a model called department that has many employees when i save a department it may have several employee belongs to it , I'm currently loops the entire employee list and validate each one before insertion is there a way in laravel 5 validator that do this out of the box.
In Laravel 5.2 there's an easy way of taking care of this kind of problems.
Validating array form input fields is much easier in Laravel 5.2. For example, to validate that each e-mail in a given array input field is unique, you may do the following:
$validator = Validator::make($request->all(), [
'person.*.email' => 'email|unique:users'
]);
Likewise, you may use the * character when specifying your validation messages in your language files, making it a breeze to use a single validation message for array based fields:
'custom' => [
'person.*.email' => [
'unique' => 'Each person must have a unique e-mail address',
]
],
This info is clearly explained in the docs.
Source: laravel.com
To my knowledge there is not a way to do this in Laravel out of the box. By that I mean there is no method you can call or class you can use that will accomplish your goal.
You can create one though. Abstract the validation out into it's own class and use that.

Resources