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;
Related
I've got a problem i can't get through, here are my models:
Cloth.php
public function selling(): BelongsTo
{
return $this->belongsTo(Selling::class);
}
Selling.php
public function clothes(): HasMany
{
return $this->hasMany(Cloth::class);
}
And now it's anything ok and pretty basic... but then came this model:
Accessory.php
public function selling(): BelongsTo
{
return $this->belongsTo(Selling::class);
}
And now it's the problem: I need (i think) a polymorphic relationship but i can't understand how to make it in this specific case.
I have 2 starting models to morph to 1 model but every example i found have 1 starting model to morph to 2 models.
Do i need a polymorphic relationship?
I can't really get out of this.
Thanks!
You are basically looking for a one to many polymorphic relationship. Here is how to do it:
Let's say your tables are structured like bellow;
Schema::create('sellings', function (Blueprint $table) {
$table->id();
$table->integer('relation_id');
$table->string('relation_type');
$table->timestamps();
});
Schema::create('accessories', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->string('details');
$table->timestamps();
});
Schema::create('cloths', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->string('description');
$table->timestamps();
});
Selling.php
public function relation(){
return $this->morphTo();
}
Cloth.php
public function selling(){
return $this->morphOne(Selling::class, 'relation');
}
Accessories.php
public function selling(){
return $this->morphOne(Selling::class, 'relation');
}
Then, you can query using bellow approach;
$selling = Selling::findOrFail(1)->relation;
Now when you dd($selling) you get exactly what you are looking for from a correspondent table;
Please remember that the relation_type field needs to exactly correspond the model. See bellow screenshot for example;
What happens here is when you create a polymorphic function called test the database fields need to follow with test_type corresponding to model and test_id corresponding to the id of the model/database table.
please help.I have a test booking table that looks like this
Schema::create('test_bookings', function (Blueprint $table) {
$table->unsignedInteger('RequestID');
$table->string('bookingDate');
$table->string('timeSlot');
$table->unsignedInteger('nurse_id');
$table->timestamps();
});
and a tests table that looks like this
Schema::create('tests', function (Blueprint $table) {
$table->unsignedInteger('RequestID');
$table->unsignedInteger('patientID');
$table->string('barcode');
$table->string('temperature');
$table->string('pressure');
$table->string('oxygen');
$table->unsignedInteger('nurseID');
$table->timestamps();
});
I want to show the RequestID,bookingDate,timeSlot, name and surname of the nurse only if the test_bookings RequestID is in tests table. This is my nurse table
Schema::create('nurses', function (Blueprint $table) {
$table->unsignedInteger('nurseID');
$table->string('name');
$table->string('surname');
$table->string('idNumber');
$table->string('phone');
$table->string('email');
$table->unsignedInteger('suburb_id');
$table->timestamps();
$table->index('suburb_id');
});
This is the code that i tried
$tests = DB::table('tests')
->select('RequestID','bookingDate','timeSlot','name','surname')
->join('nurses','nurses.nurseID','test_bookings.nurse_id')
->join('test_bookings','test_bookings.RequestID','=','tests.RequestID')
->get();
but when I join the tests table nothing is showing
that because you are using join clause that generate innerJoin statement, and to see the results you should use leftJoin
$tests = DB::table('tests')
->select('RequestID','bookingDate','timeSlot','name','surname')
->leftJoin('nurses','nurses.nurseID','=','test_bookings.nurse_id')
->leftJoin('test_bookings','test_bookings.RequestID','=','tests.RequestID')
->get();
Why you're not using ORM here, a simple one-to-one relationship can do the job perfectly. Here is an example:
class TestBooking extends Model {
# Other code...
public function nurse(){
return $this->belongsTo(Nurse::class);
}
}
class Test extends Model {
# Other code...
public function testBooking(){
return $this->belongsTo(TestBooking::class, 'RequestID','RequestID');
}
}
Now you can get all data like this:
$tests = Test::with("testBooking","testBooking.nurse")->get();
// and get data inside loop like this:
$test->RequestID // to get request ID
$test->testBooking->bookingDate // to get booking date
$test->testBooking->timeSlot // to get timeSlot
$test->testBooking->nurse->name // to get nurse name
$test->testBooking->nurse->surname // to get nurse surename
To know more about relationships read documention.
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();
Hello there I have a Laravel Question. I need to create a table that shows the following data. Client details ( client name, surname, contact numbers, email address, address). The Client should be in two categories which are Cooperate and Individual Client. Here is a snippet of what i what i did in my client migration file. I wanted to ask on how best to distinguish between them using roles.
public function up()
{
Schema::create('clients', function (Blueprint $table) {
$table->increments('id');
$table->string('client_name');
$table->string('client_surname');
$table->smallInteger('phone_number');
$table->string('email_address');
$table->string('address');
$table->timestamps();
});
}
You can create a new column in your table called categorie.
public function up()
{
Schema::table('clients', function (Blueprint $table) {
$table->string('categorie');
});
}
You can return the clients in your controller via a simple query, like this:
$individual_clients = Client::where('categorie', 'individual')->get();
$cooperate_clients = Client::where('categorie', 'cooperate')->get();
I'm very new to Laravel, and am having trouble understanding queries in Eloquent. I have two models - Laptop and Location. Suppose I want to query all the laptops belonging to a location that has a 'stock' value of 1. In SQL this would be a left join, but how do I do this in Eloquent? I've already added the relationships to the two models, so the Laptop model has a 'location' method, but where do I go from there?
You can use query scopes for that. They are thoroughly documented in the Laravel documentation
In the laptop model you can write a scope for the stockcheck:
public function scopeInStock($query)
{
return $query->where('stock', 1);
}
To keep it clean you can then create another relation in the Location model:
public function laptopsInStock()
{
return $this->hasMany('Laptop')->inStock();
// or this way:
// return $this->laptops()->inStock();
}
Now to retreive the results there are a couple of options:
//all laptops
$location->laptops;
// in stock only
$location->laptopsInStock;
// eager loading
$location->with('laptopsInStock')->get();
Since you've not provided any code so I'm going to describe from basic.
Migrations
Locations Table
Schema::create('locations', function(Blueprint $table){
$table->increments('id');
$table->string('name');
$table->timestamps();
});
Laptop Table
Schema::create('laptops', function(Blueprint $table){
$table->increments('id');
$table->string('name');
$table->integer('location_id')->unsigned();
$table->integer('stock');
$table->timestamps();
$table->foreign('location_id')
->references('id')
->on('locations')
->onDelete('cascade')
->onUpdate('cascade');
});
Models
Location Model
class Location extends Model {
public function laptops(){
return $this->hasMany('\App\Laptop');
}
}
Laptop Model
class Laptop extends Model {
public function location(){
return $this->belongsTo('\App\Location');
}
}
If you've created your app like this then there are several way to achieve the goal.
This will query all the laptop belonging to a location and that has a stock value 1.
1.
Laptop::where('location_id','some_location_id')->where('stock',1)->get();
2
Location::where('id','some_location_id')->with(['laptops' => function($query){
$query->where('stock',1);
}])->get();
I hope this would solve your problem. Feel free to comment and ask whatever question you have.