how to check if user id exists in foreign table - laravel

I need to check if user id exists in foreign table. I have two three tables which are staffs, packaging and cutting. Both the tables packaging and cutting has staff_id column I need to check if the staff belongs to packaging or the cutting. So how do I achieve this.
Packaging table
Schema::create('packaging', function (Blueprint $table) {
$table->increments('id');
$table->integer('staff_id');
$table->string('business_name');
$table->string('tax_id');
$table->string('phone_number');
$table->timestamps();
});
Cutting table
Schema::create('cutting', function (Blueprint $table) {
$table->increments('id');
$table->integer('staff_id');
$table->string('business_name');
$table->string('tax_id');
$table->string('phone_number');
$table->timestamps();
});
I tried check using belongsTo and hasOne relation in Staff Model. but it didn't work.
public function packaging()
{
return $this->hasOne(\App\Models\Admin\PackagingCompany::class,'staff_id');
}

You should apply foreign key constraints. See docs
Schema::create('packaging', function (Blueprint $table) {
$table->increments('id');
$table->foreign('staff_id')->references('id')->on('staff');
$table->string('business_name');
$table->string('tax_id');
$table->string('phone_number');
$table->timestamps();
});
This will prevent you from being able to save a packaging record without a valid staff_id
You can also apply validation anywhere an input stores a staff member against a packaging model. This would go in your form request injected into your controller.
public function rules()
{
return [
'staff_is' => 'exists:staff',
];
}
You also mentioned that you need to check if the staff member is associated with the packaging or cutting table.
With the kind of relationship you have in the migrations you've given, a staff member could belong to both packaging AND cutting. Is that what you want?
In which case you can create packaging and cutting relationships on your User model.
public function packaging()
{
$this-> hasMany(Packaging::class)
}
And then query users that have any packaging records by doing:
// Retrieve all users that have at least one packaging record...
$users = User::has('packaging')->get();

Related

Is there a way to create dynamic migrations in Laravel?

I want to create the migrations file in dynamic. I developed a product management system and there are multiple stores in the system. I want to create product tables for the store when registering the store.
my table names should like
store1_products
store2_products
store3_products
table structure are same
want to create these migration files in store create function
I all ready tried schema function on controller .
Schema::create($tableName, function (Blueprint $table) {
$table->string('email')->index();
$table->string('token');
$table->timestamps();
});
It created the table on the database but I want to create the migration file too
I suggest to just create a one table for the store and one table for the product. Then the product table must have a column that identify what store it is belongs to.
Stores Table.
Schema::create('stores', function(Blueprint $table) {
$table->id();
$table->string('store_name');
$table->timestamps();
});
Products Table
Schema::create('products', function(Blueprint $table) {
$table->id();
$table->string('product_name')->unique();
$table->unsignedFloat('price');
$table->unsignedTinyInteger('quantity');
$table->foreignId('store_id')
->constrained('stores')
->cascadeOnDelete();
$table->timestamps();
});
Then you can refer to Laravel Eloquent Relationship to know how to define their relationship.
Just make sure that you have model for them

Laravel database relationship in migration vs in class - Confused - Both of them are need?

I am confused about Laravel database relationship (unsigned id, foreign references, cascade).
Do I have to use relationship in class (like hasMany, hasOne) and in table migration (like foreign, references); for both of them too?
I had read some articles but they are not clear for me. what is the best way for best developing on an example?
As an example for category and blog post; how should it be or your best example please?
create_categories_table migration:
public function up()
{
Schema::create('categories', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->string('slug');
$table->timestamps();
});
}
create_posts_table migration:
public function up()
{
Schema::create('posts', function (Blueprint $table) {
$table->id();
$table->string('title');
$table->string('slug');
$table->longText('description');
$table->timestamps();
});
}
and Post Model:
public function category()
{
return $this->belongsTo(Category::class);
}
thank you.
Technically you don't have to define relationships between related data in your database, however, it is good practice to do so and provides benefits such as data integrity and cascading. There are also tools that can reverse engineer your database to generate a visual representation of its structure including relationships if they have been defined.
Adding relationships in your migration files creates that relationship at the database level, informing the engine that there is a logical relationship between data. You can define a foreign key constraint in your migrations in many ways, however, as of Laravel 7.x there is a forieignId method and a constrained method providing a simplified way of defining relationships from the previous method.
So for a basic example, to define a simple one-to-one relationship you might do:
public function up()
{
Schema::create('child', function (Blueprint $table) {
$table->foreignId('parent_id')->constrained();
});
}
Similarly, using Eloquent relationships to define relationships at the application level is not required. However, these helper methods provide a consistent and simplified implementation for managing related data that would otherwise require you to write additional code.
At least one but ideally both of the above would be used.
Update
if we need set a foreign key as a different key the using is just like
->constrained('privateName_categories'). two basic questions: * how Laravel understands that the reference table is 'categories' on your
example without writing its name?
The argument provided to constrained is not the name of the foreign key field but the name of the table the foreign key references. This is used in instances that either your foreign key or the table it references differ from the standard Laravel naming conventions and therefore cannot be inferred.
Laravel uses naming conventions to make connections between things. For foreign keys that convention has the format {table}_{id}. Laravel inspects the foreign key name and then uses the {table} element of the convention to create the relationship behind the scenes. If you're not using conventions you need to inform Laravel.
The following uses conventions to automatically create a relationship between the posts and categories table based on the value of the foreignId method argument.
public function up()
{
Schema::create('posts', function (Blueprint $table) {
$table->foreignId('category_id')->constrained();
});
}
This example doesn't use a standard foreign key naming convention and so the related table needs to be provided to constrained.
public function up()
{
Schema::create('posts', function (Blueprint $table) {
$table->foreignId('another_field')->constrained('categories');
});
}
Similarly, for scenarios in that you're not using conventions, you might need to specify the related column names in your Eloquent Model relationship. The exact syntax for this differs based on the type of relationship (hasOne, hasMany, manyToMany, etc.). You can read up on the different syntaxes for each relationship in Laravels defining relationships documentation.
you mention about reverse engineering tools
DataGrip by JetBrains can inspect the schema of your database and produce a visual representation. Others are also available, just do a search of the internet.
The role of migrations is to build your database schema (INSERT TABLE, ALTER TABLE, FOREIGN KEYS, DROP COLUMN, etc...).
The role of relations in Eloquent Model is to make Model aware of relations with other models. It provides a convenient way to query related models.
Relations will not create foreign keys for you.
For example if a post can be in only one category and a category contains many posts :
public function up()
{
Schema::create('categories', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->string('slug');
$table->timestamps();
});
}
public function up()
{
Schema::create('posts', function (Blueprint $table) {
$table->id();
$table->unsignedInteger('category_id');
$table->string('title');
$table->string('slug');
$table->longText('description');
$table->timestamps();
$table->foreign('category_id')->references('id')->on('categories');
});
}
Then, in your Post model :
public function category()
{
return $this->belongsTo(Category::class);
}
If you need you can also add this to Category Model :
public function posts()
{
return $this->hasMany(Post::class);
}
This will allow you to easily retrieve all posts of a category for example :
public function getPostsByCategory(int $categoryId)
{
$posts = Category::find($categoryId)->posts()->get();
return $posts;
}
EDIT :
I think the main interest is when you want to eager load relations.
For example you have posts in a blog.
Each post have many categories. (a post can be in many categories).
Each category have many posts.
Each post have many comments
In homepage I want to display last 5 posts with for each post their categories and comments.
If I build well designed relations, I can do this:
public function getLatestPosts()
{
return Post::query()
->with(['categories', 'comments'])
->orderBy('created_at', 'desc')
->limit(5)
->get();
}
It will get last 5 posts and it will attach categories and comments to each Post model.

Not sure how to setup these database tables in Laravel

The weekends table will be created first so I am thinking an weekendsteam_id needs to be in weekends and do a select statement. But I am getting really confused. Basically the user will enter a weekend. Then on a different view they will enter the team for that weekend. I need to find a way to tie the two tables together so I can query it for the weekend view on the front end.
Weekends Table
Schema::create('weekends', function (Blueprint $table) {
$table->id();
$table->string('title');
$table->mediumText('verse');
$table->string('songtitle');
$table->string('songvideo');
$table->string('image')->default('default.png');
$table->string('videolink')->nullable();
$table->timestamps();
});
Weekends Team
Schema::create('weekendteam', function (Blueprint $table) {
$table->id();
$table->string('firstname');
$table->string('lastname');
$table->string('position');
$table->timestamps();
});
This sounds like a good use for a one-to-many relationship. Since it looks like you are storing the team members and their positions in the weekendteam table, you can simply add a weekend_id column (I also renamed the table to weekend_team_members):
Schema::create('weekend_team_members', function (Blueprint $table) {
$table->id();
$table->foreignId('weekend_id');
$table->string('firstname');
$table->string('lastname');
$table->string('position');
$table->timestamps();
});
Then, in order to get the team for any weekend, you can use a query like this:
SELECT * FROM weekend_team_members WHERE weekend_id = 1;
Which will fetch all of the team members for the weekend with that ID. If you are using Eloquent models, you can also use their built-in relationships to make querying easier:
class Weekend extends Model {
...
public function weekendTeamMembers() {
return $this->hasMany(WeekendTeamMember::class);
}
}
class WeekendTeamMember extends Model {
...
public function weekend() {
return $this->belongsTo(Weekend::class);
}
}
Once these relationships are defined in the models, you can use them to help you query the data. For example, if you already have a Weekend in the variable $weekend, you can fetch all of the team members like this:
$teams = $weekend->weekendTeamMembers;
Or, if you have a team member, you can get the weekend like this:
$weekendTeamMember->weekend;

Relationship to users table without using the standard 'user_id' column name

I have 2 tables, one has different columns to record different users names based on authorisation level. but i would like to link to two together. at the moment i have tried the following:
User.php
public function approvals()
{
return $this->hasMany(Approval::class);
}
Approval.php
public function qs() {
return $this->belongsTo(User::class, 'id', 'qs');
}
index.blade.php
<td>{{ $approval->qs->name }}</td>
approvals db structure
Schema::create('approvals', function (Blueprint $table) {
$table->bigIncrements('id');
$table->unsignedBigInteger('project_id');
$table->integer('stage');
$table->unsignedBigInteger('qs')->nullable();
$table->unsignedBigInteger('pm')->nullable();
$table->unsignedBigInteger('rcm')->nullable();
$table->unsignedBigInteger('doc')->nullable();
$table->unsignedBigInteger('vpoc')->nullable();
$table->unsignedBigInteger('vpof')->nullable();
$table->timestamps();
});
users db structure
Schema::create('users', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('name');
$table->string('email', 100)->unique();
$table->timestamp('email_verified_at')->nullable();
$table->string('password');
$table->rememberToken();
$table->timestamps();
});
Am i going about this all wrong, the qs table column needs to be linking to the users.id?
It seems qs is the user id of the User model. So the relation to the Approval model is
public function qs()
{
return $this->belongsTo(User::class, 'qs');
}
And in User model
public function approvals()
{
return $this->hasMany(Approval::class, 'qs');
}
Now you can use
{{ $approval->qs->name }}
Eloquent determines the default foreign key name by examining the name of the relationship method and suffixing the method name with a _ followed by the name of the primary key column. However, if the foreign key on the Model is not parent_id, you may pass a custom key name as the second argument to the belongsTo method.
Laravel Documentation
If a parent model does not use id as its primary key, or you want to join the child model to a different column, you may pass a third argument to the belongsTo method:
public function qs() {
return $this->belongsTo(User::class, 'foreign_key_here_from_child_table', 'custom_column_from_parent_table');
}

How to create same foriegn_key for two or multiple tables id in laravel

In my project i have Journals table, Monthlies table, Loan table, Investment table. Journal table has payment_id which i want to give foreign key for the monthlies, loan and investment tables ID. I have tried this but in migration it occurs error "duplicate key on write or update"
what i want to do is , I am trying to insert loan, investment, monthlies id on journals table payment_id when loan, investment, monthlies are created. I can insert only one tables id on journals table payment_id (through foreign key relationship in migration) not multiple...how can i do that?
public function up()
{
Schema::create('journals', function (Blueprint $table) {
$table->increments('id');
$table->double('amount');
$table->integer('payment_id')->unsigned();
$table->enum('payment_format', ['monthly', 'investment', 'loan', 'income', 'expense', 'others']);
$table->string('short_description');
$table->integer('created_by')->unsigned();
$table->integer('updated_by')->unsigned();
$table->dateTime('deleted_at');
$table->timestamps();
$table->foreign('payment_id')->references('id')->on('monthlies')
->onUpdate('cascade')->onDelete('cascade');
$table->foreign('payment_id')->references('id')->on('investments')
->onUpdate('cascade')->onDelete('cascade');
$table->foreign('payment_id')->references('id')->on('loans')
->onUpdate('cascade')->onDelete('cascade');
$table->foreign('created_by')->references('id')->on('users')
->onUpdate('cascade')->onDelete('cascade');
$table->foreign('updated_by')->references('id')->on('users')
->onUpdate('cascade')->onDelete('cascade');
});
}
You can't do that in laravel. The same column can not be used as a foreign key to 3 other tables. However, you can do this in Laravel without using foreign keys on the database level.
Your migration:
Schema::create('journals', function (Blueprint $table) {
$table->increments('id');
$table->double('amount');
$table->integer('payment_id')->unsigned();
$table->enum('payment_format', ['monthly','investment','loan','income','expense','others']);
$table->string('short_description');
$table->integer('created_by')->unsigned();
$table->integer('updated_by')->unsigned();
$table->dateTime('deleted_at');
$table->timestamps();
$table->foreign('created_by')->references('id')->on('users')
->onUpdate('cascade')->onDelete('cascade');
$table->foreign('updated_by')->references('id')->on('users')
->onUpdate('cascade')->onDelete('cascade');
});
In your Journal model, define the relationship to your payment:
public function payment() {
switch($this->payment_format) {
case 'loan':
return $this->belongsTo(Loan::class, 'payment_id');
case 'monthly':
return $this->belongsTo(Monthly::class, 'payment_id');
case 'investment':
return $this->belongsTo(Investment::class, 'payment_id');
default:
// ??
break;
}
}
Now, by calling $journal->payment you get the proper object back based on the value in your payment_format field.
Try this:
$table->unsignedInteger('payment_id');
$table->foreignId('payment_id','monthlies')->.constrained('monthlies');
$table->foreignId('payment_id','items')->constrained('items');
In this code foreignId(column_name,'name_of_key').
Hope this helps.

Resources