So, I am creating a results website for boxing, I am trying to get a boxers past results, but am struggling with relationships...
For example, a boxer can have many events (a fight), and have 1 result (a win, draw or loss), and 1 opponent. I am trying to display all of the boxers results, with the opponents information
So, I have an event with the structure (can add a relationship id if needed)
Schema::create('events', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->date('date');
$table->string('location');
$table->timestamps();
$table->softDeletes();
});
I have a boxer table
Schema::create('boxers', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->string('status');
$table->string('division');
$table->string('stance');
$table->date('dob');
$table->integer('height');
$table->string('nationality');
$table->string('residence');
$table->string('birthplace');
$table->timestamps();
$table->softDeletes();
});
and the results
Schema::create('results', function (Blueprint $table) {
$table->increments('id');
$table->integer('user_id');
$table->integer('event_id');
$table->string('result');
$table->timestamps();
$table->softDeletes();
});
What would I need to add and change to get a relationship to work correctly?
Events typically hasMany fights (many fights on a card), each fight hasMany (two) athletes. So your fight/athlete relationship would be many-to-many (fighters have many fights throughout their career). You'll create a pivot table athletes_fights which will have fight_id and athlete_id columns. Additionally, this pivot table could have a winner_id and a draw column. Draw could default to 0, 1 if the fight is a draw. It could also have win_method (could even create another table for this and make it win_method_id), win_round, win_time.
Check out the relationships section in the Laravel docs, if you haven't already.
So your model would be..
Event
Fight
Athlete
Related
I need to make a HasManyThough relation where the middle Model holds the foreign keys of both Models.
Here is detail:
Migrations:
Schema::create('carriers', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
});
Schema::create('shipping_zones', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
});
Schema::create('shipping_rates', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->integer('shipping_zone_id')->unsigned()->index();
$table->integer('carrier_id')->unsigned()->index();
$table->decimal('rate', 20, 6);
});
Now I need something like
$carrier->shippingZone()
Is there any easy way to get this?
As per your migrations you need Many To Many relation rather than the HasManyThough relation.
Carrier.php
public function shippingZones()
{
return $this->belongsToMany('App\ShippingZone', 'shipping_rates);
}
You can access all shipping zones related to the carrier using, $carrier->shippingZones.
If you need to chain the query you can use $shippingZones = $carrier->shippingZones()->orderBy('id')->get(); here
If you need to use HasManyThough you would have to change the migrations. here
I have rooms in hospitals. Each room has beds with numbers. I want to assign beds to patients. How can I handle this in an easy way:
rooms migration:
Schema::create('rooms', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->integer('floor');
$table->text('description');
$table->timestamps();
});
beds migration:
Schema::create('beds', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->string('quality');
$table->string('charge');
$table->integer('room_id')->unsigned();;
$table->foreign('room_id')->references('id')->on('rooms')
->onUpdate('cascade')->onDelete('cascade');
$table->boolean('status');
$table->timestamps();
});
reservations migration:
Schema::create('reservations', function (Blueprint $table) {
$table->increments('id');
$table->date('date_in');
$table->date('date_out');
$table->boolean('status');
$table->integer('patient_id')->unsigned();
$table->foreign('patient_id')->references('id')->on('patients');
$table->integer('bed_id')->unsigned();
$table->foreign('bed_id')->references('id')->on('beds');
$table->timestamps();
});
How can I check for available beds? Get the available beds? Be sure the bed is available. How can I use events and listeners to make the whole thing work?
pluck() retrieve a Collection containing the values of a single column.
Here, it retrieves a Collection of reservation bed_id:
$reserved = Reservation::pluck('bed_id)->all();
The whereNotIn method verifies that the given column's value is not contained in the given array:
$bed_available = Bed::whereNotIn('id' , $reserved)->get();
This query gives you all the bed whose id is not available in that collection $reserved , which i think is your requirement .
How can i set up this, i'm trying to build a student and teacher lesson schedule, here is my code.
Schema::create('users', function (Blueprint $table) {
$table->increments('id');
$table->text('photo_url')->nullable();
$table->string('firstname');
$table->string('lastname');
$table->string('username')->unique()->nullable();
$table->date('date_of_birth');
$table->string('email')->unique();
$table->string('password');
$table->timeTz('timezone');
$table->integer('is_active')->default(0);
$table->tinyInteger('verified')->default(0);
$table->string('email_token')->nullable();
$table->text('address');
$table->string('district');
$table->rememberToken();
$table->timestamps();
});
And then for the lesson schedules
Schema::create('lessons', function (Blueprint $table) {
$table->increments('id');
$table->integer('status'); //0 - not started, 1 - completed, 2 - no show
$table->dateTime('start_at');
$table->dateTime('end_at');
$table->dateTime('started_at');
$table->dateTime('ended_at');
$table->string('unique_id')->unique();
$table->integer('teacher_id')->unsigned();
$table->foreign('teacher_id')
->references('id')->on('users')
->onUpdate('cascade')
->onDelete('cascade');
$table->integer('student_id')->unsigned();
$table->foreign('student_id')
->references('id')->on('users')
->onUpdate('cascade')
->onDelete('cascade');
$table->integer('completed');
$table->timestamps();
});
if i want to display my data on the view for this. How do i add the relations to the various models and how do i display them to view.
I believe this is a belongsToMany relations
Pleas help!
how do i do this?
Not sure if i clear enough
I think that using a pivot may restrict you somewhat, and make things more complicated.
Instead, why not create a model such as Schedule?
Student has many Schedule
Schedule belongs to a Student
Schedule belongs to a Teacher
Schedule belongs to a Lesson
Student has many Lesson through Schedule
This also allows you to expand on this further since you're dealing with a model specifically for the purpose.
I have 2 tables Manufacturers and Suppliers ,and they have Many-to-Many relation
Schema::create('manufacturers', function (Blueprint $table) {
$table->increments('id');
$table->string('name')->unique();
$table->timestamps();
});
Schema::create('suppliers', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->string('location');
$table->timestamps();
});
so i created a pivot table called "manufacturer_supplier" and it works fine.
Schema::create('manufacturer_supplier', function (Blueprint $table) {
$table->increments('id');
$table->integer('manufacturer_id')->unsigned()->index();
$table->integer('supplier_id')->unsigned()->index();
$table->timestamps();
$table->foreign('manufacturer_id')->references('id')->on('manufacturers')->onDelete('cascade');
$table->foreign('supplier_id')->references('id')->on('suppliers')->onDelete('cascade');
});
My confusion comes from adding a new table "devices"
Schema::create('devices', function (Blueprint $table) {
$table->increments('id');
$table->string('serial')->unique();
$table->string('name');
$table->integer('supplier_id')->unsigned()->nullable();
$table->integer('manufacturer_id')->unsigned()->nullable();
$table->text('notes')->nullable();
$table->timestamps();
$table->foreign('supplier_id')->references('id')->on('suppliers')->onDelete('cascade');
$table->foreign('manufacturer_id')->references('id')->on('manufacturers')->onDelete('cascade');
});
where i want to have a one-to-many relation with each of suppliers and manufacturers , so i would have a select list of manufacturers and then populate the next list from with suppliers from Pivot table that relate to the selected manufacturer.
currently my "device" model has the following relations which it relate them to the original table not the pivot so it doesn't work
//5
public function manufacturer(){
return $this->belongsTo('App\Manufacturer');
}
//6
public function supplier(){
return $this->belongsTo('App\Supplier');
}
I haven't done that before so i'm curious to know what will be the best fix for this to work.
Thanks a lot ,
If you want all suppliers associated with a single manufacturer you could get them using the many-to-many relation.
$suppliersList = $supplier->manufacturer()->suppliers();
howerver I don't understand why a device should be associated with a single supplier .... but that's up to you.
I have a 3 way pivot table that ties a role to a user for a specific organization. Here's how the relevant tables are set up:
//users table
Schema::create('users', function($table)
{
$table->engine = 'InnoDB';
$table->increments('id');
$table->string('username', 30)->index();
//more (irrelevant) fields here
$table->timestamps();
});
//roles table
Schema::create('roles', function(Blueprint $table)
{
$table->engine = 'InnoDB';
$table->increments('id');
$table->string('name', 100)->index();
$table->string('description', 255)->nullable();
$table->timestamps();
});
//organizations table
Schema::create('organizations', function(Blueprint $table)
{
$table->engine = 'InnoDB';
$table->increments('id');
$table->string('name', 255);
$table->string('slug', 100);
$table->text('description');
$table->timestamps();
});
//organization user role table
Schema::create('organization_user_role', function(Blueprint $table)
{
$table->engine = 'InnoDB';
$table->increments('id');
$table->integer('role_id')->unsigned()->index();
$table->foreign('role_id')->references('id')->on('roles')->onDelete('cascade');
$table->integer('user_id')->unsigned()->index();
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
$table->integer('organization_id')->unsigned()->index();
$table->foreign('organization_id')->references('id')->on('organizations')->onDelete('cascade');
$table->timestamps();
});
Each user can belong to one or more organizations, with one or more roles. Roles are tied to both a user and an organization simultaneously. That is to say, a user must have a role in an organization in order to be associated with it.
This doesn't seem to fit the mold of the traditional many-to-many relationship pivot-table that works so well out-of-the-box with Eloquent. Can Eloquent handle this type of relationship, or do I need a new model dedicated to handling the relationship? Can someone please show me what the User, Organization, and Role models would look like to tie the 3-way pivot table relationships together with Eloquent?
Check this out: http://github.com/jarektkaczyk/Eloquent-triple-pivot
It works, however I wouldn't call it complete solution. Still you can build yours on top of that.