Saving data to database in unit testing - laravel

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

Related

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

CakePHP 3 sessions table in database

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.

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

Laravel 5 creating custom registration and login from default Authentication

After 2 years experience in cakePhp , recently i started learning laravel framework. i already created the database for my new project and create a default authentication system with laravel 5 using the
php artisan make:auth
but i cant modify it according to my database design
i have a
users table(id,username,password,remember_token)
user_profiles table (id,first_name,last_name etc)
group table(id,group_name)
group_users(group_id,users_id)
users_user_profiles (user_id,user_profile_id)
so using the above table i want to implement a group based user management system.
if i use the default authentication system ,
how can i store login and registration details into two tables with a single registration from? and how can i insert the id's into pivot table users_user_profiles table automatically ?
How to create a view for the registration which contain both the login details and registration details, how can i separate them and how to access the request data separately for storing into two separate tables?
if anybody can provide a detailed description on this, it will help all laravel 5 beginners, i think its a common problem for all newbies..
please help me
Thanks
First define good your models, so you can use eloquent, for example making connection with user_profiles:
public function profiles()
{
return $this->belongsToMany('UserProfile');
}
(It will be possible only when you get UserProfile model)
Then you extend your register form by adding first_name, last_name, etc. In auth controller after creating user
$user = User::create([
'name' => $data['name'],
'email' => $data['email'],
'password' => bcrypt($data['password']),
]);
Add code that create user profile:
$userProfile = UserProfile::create([
'first_name' => $data['name'],
'last_name' => $data['email']
]);
Then add it to user by attach command (if you have many of them you can use sync) :
$user->userProfiles()->attach($userProfile->id);
More info about how user Authentication work you can find exterminating file: \vendor\laravel\framework\src\Illuminate\Foundation\Auth\RegistersUsers.php

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