Laravel Nova 2 models in 1 resource - laravel

In my case i have a User
Schema::create('users', function (Blueprint $table) {
$table->increments('id');
$table->unsignedInteger('address_id')->nullable();
$table->foreign('address_id')->references('id')->on('addresses');
$table->string('first_name');
$table->string('insertion')->nullable();
$table->string('last_name');
$table->string('email')->unique();
$table->timestamp('email_verified_at')->nullable();
$table->string('password');
$table->string('phone_number');
$table->string('avatar')->nullable();
$table->rememberToken();
$table->timestamps();
$table->softDeletes();
});
And i have a Address
Schema::create('addresses', function (Blueprint $table) {
$table->increments('id');
$table->string('city');
$table->string('street');
$table->string('zip_code');
$table->string('state');
$table->string('house_number');
$table->timestamps();
});
Is it possible to create a new user and add a address to it in the same Nova User Resource?
What can we add in the fields function of the Resource?
Do i need to overwrite the default create method? Or can this be fixed with a belongsTo ?
Currently i got it to work with using
morhpOne
method but this is not what i want. I first have to create a User and after that i can add an Address.

This should be simple, in your Nova User model add
BelongsTo::make('Address');
The Address field should represent the function in your User model that returns the Address relation
So in your user there should be
public function address()
{
return $this->belongsTo(Address::class);
}
I hope that that is what you are looking for, I will be happy to update my answer if given more information.

Related

Assigning one or more clinicians (user role_id =2) to patient in Laravel (Many to Many)

My app have a few misunderstanding i need help with. These are listed as follows:
A clinician can be assigned to one or more patient/s by the admin.
In a clinician backend he/she can only view profiles of the clients assigned by the admin.
IN User Model
public function patients()
{
return $this->belongsToMany('App\Patient')->withTimestamps();
}
In Patient Model
public function users()
{
return $this->belongsToMany('App\User')->withTimestamps();
}
In User Table
public function up()
{
Schema::create('users', function (Blueprint $table) {
$table->id();
$table->string('username')->unique();
$table->string('slug')->unique();
$table->string('first_name');
$table->string('last_name');
$table->string('email')->unique();
$table->string('phone')->unique();
$table->string('address');
$table->string('gender');
$table->integer('role_id')->default(2);
$table->string('password');
$table->rememberToken();
$table->timestamps();
});
}
In Patient Table
public function up()
{
Schema::create('patients', function (Blueprint $table) {
$table->id();
$table->string('username')->unique();
$table->bigInteger('user_id')->unsigned();
$table->string('slug')->unique();
$table->string('profilePic');
$table->string('first_name');
$table->string('last_name');
$table->string('dob');
$table->string('gender');
$table->string('parent_name');
$table->string('parent_number');
$table->string('parent_email');
$table->boolean('status')->default(false);
$table->boolean('is_approved')->default(false);
$table->timestamps();
});
Schema::table('patients', function(Blueprint $table) {
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade')->onUpdate('cascade');
});
}
Kindly Assist
This is the example for a pivot table:
Rules:
Need a migration to a third table
The table's name need to be in singular and order by alphabetical order
Ex.: patient_user with patient_id and user_id
No need Model for this table
Add user as patient
$user = User:find(5);
$user->patients()->attach(1);
In your table patient_user you will have a record: user_id

3 Table Laravel Migration need advice

I think I am doing this correctly. I have a users, profile, and application table. Below are the migrations. I think I am linking them correctly in the migration but need advice to make sure. 1 user can have 1 profile but many applications.
User Table
public function up()
{
Schema::create('users', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->string('email')->unique();
$table->timestamp('email_verified_at')->nullable();
$table->string('password');
$table->rememberToken();
$table->timestamps();
});
}
Profile Table
public function up()
{
Schema::create('profile', function (Blueprint $table) {
$table->id();
$table->integer('user_id')->unsigned();
$table->string('address')->nullable();
$table->string('apt')->nullable();
$table->string('city')->nullable();
$table->string('state')->nullable();
$table->string('zipcode')->nullable();
$table->string('homephone')->nullable();
$table->string('mobile')->nullable();
$table->string('occupation')->nullable();
$table->boolean('over18')->nullable();
$table->string('homechurch')->nullable();
$table->string('homechurchcity')->nullable();
$table->string('pastor')->nullable();
$table->string('howoftenattend')->nullable();
$table->timestamps();
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
});
Application
public function up()
{
Schema::create('pilgrim_application', function (Blueprint $table) {
$table->id();
$table->string('besttimetocall');
$table->string('nickname');
$table->foreignId('user_id')->constrained('users')->onDelete('cascade');
$table->foreignId('profile_id')->constrained('profile')->onDelete('cascade');
$table->timestamps();
});
Does your migration work at the moment?
First check your migration file order, for example
0000_user
0001_profile
1000_ pilgrim_application
1001_app_two
in case of multiple applications you could begin the migration with a different number for grouping files in a list, this can be handy if you want to create multiple applications.
Also, is it necessary to specify the relations user and profile in the pilgrim_application migration?
Through relationships in your models you can also access the pilgrim_application from the user model and vice versa.
$user->profile->pilgrim_application

Laravel combine multiple collections into one query

I'm wondering if this is possible. I have 3 models.
Users
TenantPreferances
PropertyAdverts
I'm trying to find out if I can do a query like so.
Find all tenants, whose preferences, match the currently signed in users properties.
The 3 databases are like so
User Model
Schema::create('users', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->string('email')->unique();
$table->string('userType');
$table->string('password');
$table->rememberToken();
$table->timestamps();
});
PropertyAdverts
Schema::create('property_adverts', function (Blueprint $table) {
$table->increments('id');
$table->string("photo");
$table->string('address');
$table->string('county');
$table->string('town');
$table->string('type');
$table->string('rent');
$table->string('date');
$table->string('bedrooms');
$table->string('bathrooms');
$table->string('furnished');
$table->longText('description');
$table->integer('user_id'); //Landlord ID
$table->timestamps();
});
Tenant Preferances
Schema::create('tenant_preferances', function (Blueprint $table) {
$table->increments('id');
$table->string('county');
$table->string('type');
$table->string('rent');
$table->string('bedrooms');
$table->string('bathrooms');
$table->boolean('status')->default('0');
$table->integer('user_id'); //Tenant ID
$table->timestamps();
});
Yes, that is possible. You need to dig in on relations. You can then create a function to define the relation on you model:
function propertyAdverts() {
return $this->hasMany(PropertyAdverts::class);
}
You can access the relation from the user model by using $user->propertyAdverts. If you want to eager load them you can do so:
User::with('propertyAdverts')->find(3);
But notice that Laravel does not do a regular join by default. It first fetches all users, and then fetches all propertyAdverts using a single query using a in statement.

Relationship between Users, Settings, Setting_types Eloquent Laravel 5.4*

How would you go to create a relationship between the folowing tables:
Users table:
Schema::create('users', function (Blueprint $table) {
$table->increments('id');
$table->string('firstname');
$table->string('surename');
$table->string('address')->nullable();
$table->string('city')->nullable();
$table->string('country')->nullable();
$table->string('postcode')->nullable();
$table->string('email')->unique();
$table->string('password');
$table->rememberToken();
$table->timestamps();
});
Setting_type table:
Schema::create('setting_types', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->timestamps();
});
Settings table:
Schema::create('settings', function (Blueprint $table) {
$table->increments('id');
$table->string('value');
$table->timestamps();
});
Setting_type_user table:
Schema::create('setting_type_user', function (Blueprint $table) {
$table->integer('user_id')->unsigned();
$table->integer('type_id')->unsigned();
$table->integer('setting_id')->unsigned();
$table->foreign('user_id')->references('id')->on('users');
$table->foreign('type_id')->references('id')->on('setting_types');
$table->foreign('setting_id')->references('id')->on('settings');
$table->timestamps();
});
This is the result I want to get:
{"id":1,"value":"578943205.jpg","created_at":"2017-07-18 00:00:00","updated_at":null,"pivot":{"setting_id":1,"user_id":1,"type_id":1}}
It depends on many things, but it looks like you want to use many-to-many relationship here.
First, in settings pibot table change it to:
$table->unsignedInteger('user_id');
$table->unsignedInteger('type_id');
It's also a good idea to add foreign key constraints to the table:
$table->foreign('user_id')->references('id')->on('users');
$table->foreign('type_id')->references('id')->on('setting_types');
Then define belongsToMany() relationship in both User and SettingType models. Since you don't follow naming conventions, you have to define foreign keys manually. In the User model:
public function settingTypes()
{
return $this->belongsToMany('App\SettingType', 'settings', 'user_id', 'type_id');
}
And in the SettingType model:
public function users()
{
return $this->belongsToMany('App\User', 'settings', 'type_id', 'user_id');
}

Additional fields on polymorphic many to many table map to Eloquent model

I'm trying to create a ploymorphic many-to-many relationship that also includes an additional relationship. I can't seem to figure out how to get Eloquent to map the additional field to a model.
I have projects, users on those projects, and project roles that dictate permissions for that user on the project.
My tables:
Schema::create('projects', function(Blueprint $table){
$table->increments('id');
$table->timestamps();
$table->softDeletes();
$table->string('name');
$table->integer('possessor_id');
$table->string('possessor_type');
});
Schema::create('users', function (Blueprint $table) {
$table->increments('id');
$table->timestamps();
$table->softDeletes();
$table->string('email');
$table->string('name');
$table->string('username');
});
Schema::create('project_accessors', function(Blueprint $table){
$table->increments('id');
$table->timestamps();
$table->integer('project_id')->unsigned();
$table->foreign('project_id')->references('id')->on('projects');
$table->integer('project_role_id')->unsigned();
$table->foreign('project_role_id')->references('id')->on('project_roles');
$table->integer('entity_id')->unsigned();
$table->string('entity_type');
});
Schema::create('project_roles', function(Blueprint $table){
$table->increments('id');
$table->timestamps();
$table->softDeletes();
$table->string('name');
$table->string('permissions');
$table->integer('project_id')->unsigned();
$table->foreign('project_id')->references('id')->on('projects')->onDelete('cascade');
});
I define projects on users like this:
public function projects()
{
return $this->morphToMany('App\Project', 'entity', 'project_accessors')->withTimestamps()->withPivot('project_role_id');
}
I define users and roles on projects like this:
public function roles()
{
return $this->hasMany('App\ProjectRole');
}
public function users()
{
return $this->morphedByMany('App\User', 'entity', 'project_accessors')->withTimestamps()->withPivot('project_role_id');
}
Is there a way to map project_role_id to an actual instance of my Project Role model?

Resources