Laravel Eloquent how to make one-to-any relationship - php

I have model 'Transaction' which represent a message to be send, and the transaction shall have a relationship called 'recipients', each recipient might be a 'Group' Or a 'Person'.
The relationship is (One transaction has many recipients)
What type of eloquent relationship should I used?
How to implement that?
Transaction:
Schema::create('transactions', function (Blueprint $table) {
$table->id();
$table->enum('destination_type', DestinationType::getValues());
$table->foreignId('user_id')->constrained('users');
$table->timestamps();
});
Recipient:
Schema::create('recipients', function (Blueprint $table) {
$table->id();
$table->integer('recipient_id');
$table->string('recipient_type');
$table->timestamps();
});
Profile:
Schema::create('profiles', function (Blueprint $table) {
$table->id();
$table->enum('type', ProfileType::getValues());
$table->string('username');
$table->string('arabic_name')->nullable();
$table->string('english_name');
$table->string('email')->nullable()->unique();
$table->string('mobile')->nullable();
$table->string('avatar')->nullable();
$table->timestamps();
});
Group:
Schema::create('groups', function (Blueprint $table) {
$table->id();
$table->foreignId('user_id')->constrained('users');
$table->string('name');
$table->timestamps();
});

Related

Can't create table `clothing`.`clothes` (errno: 150 "Foreign key constraint is incorrectly formed")") Laravel 7

Can't create table clothing.clothes (errno: 150 "Foreign key constraint is incorrectly formed")") Laravel 7
Can not assert foreign keys.
Bands
Schema::create('bands', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->string('band_origin');
$table->text('band_details');
$table->timestamps();
$table->softDeletes();
});
category
Schema::create('category', function (Blueprint $table) {
$table->id();
$table->string('category_name');
$table->string('category_description');
$table->timestamps();
$table->softDeletes();
**gender**
Schema::create('gender', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->string('description');
$table->timestamps();
$table->softDeletes();
**colors**
Schema::create('colors', function (Blueprint $table) {
$table->id('idColor');
$table->String('color_name');
$table->String('color_description')->nullable();
$table->timestamps();
$table->softDeletes();
});
**ERROR HERE TABLE CLOTHES WHERE I WANT to ACQUIRE those field in this table**
Schema::create('clothes', function (Blueprint $table) {
$table->id('clothesID'); //PK
$table->bigInteger('bandID_fk')->unsigned();
$table->bigInteger('categoryID_fk')->unsigned();
$table->bigInteger('genderID_fk')->unsigned();
$table->bigInteger('colorID_fk')->unsigned();
$table->tinyInteger('onStock');
$table->integer('price');
$table->integer('discount')->default(0);
$table->string('description')->nullable();
$table->integer('shoeSize')->nullable();
$table->integer('stock');
$table->string('photo1');
$table->string('photo2')->nullable();
$table->string('photo3')->nullable();
$table->timestamps();
$table->softDeletes();
// ERROR
$table->foreign('bandID_fk')->references('id')->on('bands');
$table->foreign('categoryID_fk')->references('id')->on('category');
$table->foreign('genderID_fk')->references('id')->on('gender');
$table->foreign('colorID_fk')->references('idColor')->on('users');
});
}
This is the right answer
Thank you!
Schema::create('clothes', function
(Blueprint $table) {
$table->id('clothesID'); //PK
$table->bigInteger('bandID_fk')->unsigned();
$table->bigInteger('categoryID_fk')->unsigned();
$table->bigInteger('genderID_fk')->unsigned();
$table->bigInteger('colorID_fk')->unsigned();
$table->tinyInteger('onStock');
$table->integer('price');
$table->integer('discount')->default(0);
$table->string('description')->nullable();
$table->integer('shoeSize')->nullable();
$table->integer('stock');
$table->string('photo1');
$table->string('photo2')->nullable();
$table->string('photo3')->nullable();
$table->timestamps();
$table->softDeletes();
$table->foreign('bandID_fk')->references('id')->on('bands');
$table->foreign('categoryID_fk')->references('id')->on('category');
$table->foreign('genderID_fk')->references('id')->on('gender');
$table->foreign('colorID_fk')->references('idColor')->on('colors');

Laravel find records through intermediate table

I have 3 models:
User
Company
Enquiry
A user can own more than one company, a company can only belong to one user. An enquiry can belong to many companies and a company can have many enquiries.
The migrations look as follows:
User migration
Schema::create('users', function (Blueprint $table) {
$table->increments('id');
$table->string('first_name');
$table->string('last_name');
$table->string('email')->unique();
$table->timestamp('email_verified_at')->nullable();
$table->string('contact_number');
$table->longText('address')->nullable();
$table->integer('postal_code');
$table->string('activation_token')->nullable();
$table->boolean('active')->default(false);
$table->enum('type', ['Admin', 'End User', 'Service Provider', 'Broker']);
$table->string('password')->nullable();
$table->rememberToken();
$table->timestamps();
$table->softDeletes();
});
Company Migration
Schema::create('companies', function (Blueprint $table) {
$table->increments('id');
$table->unsignedInteger('service_provider_id')->nullable();
$table->unsignedInteger('broker_id');
$table->string('name');
$table->string('email')->unique()->nullable();
$table->string('contact_number');
$table->longText('address')->nullable();
$table->integer('postal_code');
$table->enum('status', ['Confirmed', 'Declined', 'New'])->default('New');
$table->timestamps();
$table->softDeletes();
});
Schema::table('companies', function (Blueprint $table) {
$table->foreign('service_provider_id')->references('id')->on('users');
$table->foreign('broker_id')->references('id')->on('users');
});
Enquiries Migration
Schema::create('enquiries', function (Blueprint $table) {
$table->increments('id');
$table->unsignedInteger('end_user_id');
$table->unsignedInteger('category_id');
$table->string('title');
$table->longText('description');
$table->integer('radius');
$table->enum('status', ['New', 'In Progress', 'Complete'])->default('New');
$table->timestamps();
});
Schema::table('enquiries', function (Blueprint $table) {
$table->foreign('end_user_id')->references('id')->on('users');
$table->foreign('category_id')->references('id')->on('categories');
});
CompanyEnquiry Migration
Schema::create('company_enquiry', function (Blueprint $table) {
$table->integer('company_id')->unsigned()->index();
$table->foreign('company_id')->references('id')->on('companies')->onDelete('cascade');
$table->integer('enquiry_id')->unsigned()->index();
$table->foreign('enquiry_id')->references('id')->on('enquiries')->onDelete('cascade');
$table->primary(['company_id', 'enquiry_id']);
});
I have set up the various relationships in their respective models.
What I'm trying to achieve is query the database to retrieve only those enquiries which belong to the user via the company.
How can I achieve this?
Something like this (add the real model, relation names ans columns):
$enquiries = Enquiries::whereHas(['companies' => function($query){
$query->whereHas(['user' => function($query){
$query->where('id', $user_id);
}]);
})
->get();
You could do it this way with shorter code.
$enquiries = Enquiries::whereHas('companies.user', function($query) use($user_id){
$query->where('id', $user_id);
})->get();

Foreign key not being created

I'm trying to create a foreign key in the teachers table. after migration, all the columns were there, but the foreign key was not created
First, I have created two tables, which are the users table and the courses table.
if(! Schema::hasTable('users')) {
Schema::create('users', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->string('email');
$table->string('password');
$table->string('remember_token')->nullable();
$table->timestamps();
});
}
if(! Schema::hasTable('courses')) {
Schema::create('courses', function (Blueprint $table) {
$table->increments('id');
$table->string('title');
$table->string('slug')->nullable();
$table->text('description')->nullable();
$table->decimal('price', 15, 2)->nullable();
$table->string('course_image')->nullable();
$table->date('start_date')->nullable();
$table->tinyInteger('published')->nullable()->default(0);
$table->timestamps();
$table->softDeletes();
$table->index(['deleted_at']);
});
}
Then i created another table called 'teachers' with foreign keys
if(! Schema::hasTable('teacher')) {
Schema::create('teacher', function (Blueprint $table) {
$table->increments('id');
$table->integer('user_id')->unsigned();
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
$table->integer('course_id')->unsigned();
$table->foreign('course_id')->references('id')->on('courses')->onDelete('cascade');
$table->string('teachers_image')->nullable();
$table->text('education')->nullable();
$table->text('contact')->nullable();
$table->timestamps();
$table->softDeletes();
});
}
after the migration i can see the tables was there but the foreign key was not created
try this
if(! Schema::hasTable('teacher')) {
Schema::create('teacher', function (Blueprint $table) {
$table->increments('id');
$table->unsignedInteger('user_id');
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
$table->unsignedInteger('course_id');
$table->foreign('course_id')->references('id')->on('courses')->onDelete('cascade');
$table->string('teachers_image')->nullable();
$table->text('education')->nullable();
$table->text('contact')->nullable();
$table->timestamps();
$table->softDeletes();
});
}

Base table or view not found laravel pivot table

Posts table looks like.
Schema::create('posts', function (Blueprint $table) {
$table->increments('id');
$table->integer('user_id')->unsigned();
$table->string('title');
$table->string('slug')->unique();
$table->string('image')->default('default.png');
$table->text('body');
$table->boolean('is_approved')->default(false);
$table->boolean('status')->default(false);
$table->integer('view_count')->default(0);
$table->foreign('user_id')
->references('id')->on('users')
->onDelete('cascade')->unsigned()->index();
$table->timestamps();
});
User table.
Schema::create('users', function (Blueprint $table) {
$table->increments('id');
$table->integer('role_id')->default(2);
$table->string('name');
$table->string('username')->unique();
$table->string('email')->unique();
$table->string('image')->default('default.png');
$table->timestamp('email_verified_at')->nullable();
$table->string('password');
$table->text('about')->nullable();
$table->rememberToken();
$table->timestamps();
});
When run php artisan:migrate got the error.
I can't find it.
There is my post_users table.
Schema::table('post_users', function (Blueprint $table) {
$table->integer('post_id')->unsigned();
$table->integer('user_id');
$table->foreign('post_id')
->references('id')->on('posts')
->onDelete('cascade')->unsigned()->index();
});
enter image description here
In PostUsers Model define table name
class PostUsers extends Model {
public $table = "post_users";
Update:
Shouldn't
Schema::table('post_users', function (Blueprint $table) {
$table->integer('post_id')->unsigned();
$table->integer('user_id');
$table->foreign('post_id')
->references('id')->on('posts')
->onDelete('cascade')->unsigned()->index();
});
be
Schema::create('post_users', function (Blueprint $table) {
$table->integer('post_id')->unsigned();
$table->integer('user_id');
$table->foreign('post_id')
->references('id')->on('posts')
->onDelete('cascade')->unsigned()->index();
});
If you're going to create a new table you must use Schema::create

Using Laravel schema builder and migrations

I am new to Laravel PHP framework. I am confused with how Laravel schema builder and migrations work. I created two tables using Laravel migrations. Below are my two tables:
public function up()
{
Schema::create('hotels', function(Blueprint $table)
{
$table->increments('id');
$table->integer('hotel_id');
$table->string('name');
$table->string('address')->nullable();
$table->string('city')->nullable();
$table->string('province')->nullable();
$table->string('country')->nullable();
$table->timestamps();
});
}
This is my other table:
public function up()
{
Schema::create('rooms', function(Blueprint $table)
{
$table->increments('id');
$table->integer('hotel_id');
$table->string('name');
$table->timestamps();
});
}
My question is how can can I make relations with two tables using Foreign keys? If I have to use Schema Builder where do I have to put the schema file?
You can do it in the very same migrations, using Schema Builder. This is how you create a foreign key using migrations:
public function up()
{
Schema::create('rooms', function(Blueprint $table)
{
$table->increments('id');
$table->integer('hotel_id');
$table->string('name');
$table->foreign('hotel_id')
->references('id')
->on('hotels')
->onDelete('cascade');
$table->timestamps();
});
}

Categories