Laravel References 2e en 3th level - php

I have 3 tables.
Table news (hasMany Writers)
id
name
Table Writers (hasMany News and BelongsTo Country)
id
Name
country_id
Table Countries (hasMany Writers)
id
name
Now I want to display this in a view:
Name(news), Name(Writer), Name(Country)
I only can display:
Name(news), Name(Writer), country_id
How do I display the country name?

In your TableWriter model you should have a function 'country'.
public function country() {
return $this->belongsTo('TableWriter');
}
The the country_id should be the id of the country and you can do $tableWriter->country()->name where $tablewriter is an instance of your TableWriter.
So with the above in app/TableWriter.php you should be able do do something like:
$tableWriter = AppName\TableWriter::first();
echo $tableWriter->country()->name;
This should echo the counter name of the first TableWriter.

Related

Laravel filament, is there any way to mutate TextColumn value in table?

How to get only one value in the filament table for with hasMany relationship?
I have two DB tables:
products
id
sku
1
SKU_1
2
SKU_2
product_descriptions
id
product_id
translation_id
name
1
1
1
Opel
2
1
2
Vauxhall
In my Product model I have hasMany relationship
public function productDescriptions(): HasMany
{
return $this->hasMany(ProductDescription::class);
}
When I do Tables\Columns\TextColumn::make('productDescriptions.name') it return all values separated by comma. In my example "Opel, Vauxhall"
Is there any way to manipulate/mutate return value using callback? Let say, return only first value "Opel"?
You can use calculated states.
Tables\Columns\TextColumn::make('productDescriptions.name')
->getStateUsing( function (Model $record){
return $record->productDescriptions()->first()?->name;
});

Check if given date is available from multiple table

I am working on online reservation system where customer will search for room available with time and number of people. My database table looks like this:
Table: Property
id
property_name
property_number
1
abc property
AB-123
2
def property
DF-343
property_number is unique field
Table: Rooms
id
Room Name
Capacity
property_number
1
Room one
150
AB-123
1
Room two
500
AB-123
2
Room one
500
DF-343
property_number is foreign key from table property
Table: Booking
id
property_number
room_id
book_status
book_start
book_end
1
AB-123
1
active
2021-06-06 10:00:00
2021-06-06 18:00:00
property_number is foreign key from table property
room_d is foreign key from table Room
book_status : completed, pending, running, cancelled. If booking status is completed or cancelled than user should be able to book.
User Search Field
Reservation Date
Reservation Start Time
Reservation End Time
Total no of People
User Submits Specific Property Number
Solution Tried
$property = Property:::where('status','active')->where('property_number',$property_number)->firstOrFail();
$bookings = $property->bookings()
->whereBetween('event_date_start',[$event_start,$event_end])
->orWhereBetween('event_date_end',[$event_start,$event_end])
->orWhere( function ( $query ) use ($event_start,$event_end) {
$query->where('event_date_start' , '<', $event_start)->where('event_date_end','>',$event_end);
})
->get();
Model Structure
Table: **Property**
Class Property extends Model {
public function bookings(){
return $this->hasMany(Bookings::class,'property_number','property_number');
}
public function rooms(){
return $this->hasMany(Rooms::class,'property_number','property_number');
}
}
Table: **Rooms**
Class Rooms extends Model {
public function property(){
return $this->belongsTo(Property::class,'property_number','property_number');
}
public function bookings(){
return $this->hasMany(Bookings::class,'room_id');
}
}
Table: **Bookings**
Class Bookings extends Model {
}
This way it checks only property not every room associated with the property.
How to check if any room is available for booking for provided property.*

Laravel joining table relationship

I'm wanting to do a joining table but I'm not sure how to set up the relationship in laravel.
I have 3 tables, products, categories, and product_categories. product_categories will consist of the product ID and the category ID. How would I join theses up in a relationship in laravel so I can just call $product->categories() like a normal relationship?
Your relationship will be belongsToMany.
Your products table would be
id | name
Your categories table would be
id | name
your product_categories table would be
id | product_id | category_id
As per relationship in Laravel
Product Model
class Product extends Model
{
protected $table = 'products';
public function categories()
{
return $this->belongsToMany('App\Category','product_categories','product_id','category_id');
}
}
Category Model
class Category extends Model
{
protected $table = 'categories';
public function products()
{
return $this->belongsToMany('App\Product','product_categories','category_id','product_id');
}
}
In controller now
Product::with('categories')->get();
This is a many-to-many relationship.
You can use belongsToMany on both products and categories and it would work. However you also should rename product_categories to follow the rule
use singular table names in alphabetical order
this would be category_product

How to get related rows from attributes table assigned to a certain attribute in Laravel

I have category table, attribute table and attribute_value table,
Category:
id | Catgeory Name
Attribute:
id| cat_id | Attribute Name
Attribute values Table:
id | attr_id | attr_value
Now i want to Display it like this :
Category Name
Attribute Name 1
Attribute Val 1
Attribute val n
Attribute Name 2
Attribute Val 1
Attribute Val n
..
..
I'm using following model
Attribute:
public function attr_values()
{
return $this->hasMany(AttributeValue::class);
}
Category:
public function categoryAttributes()
{
return $this->hasMany(Attribute::class, 'category_id');
}
In controller i'm getting data using following:
Category::with(['categoryAttributes','attrValues'])->get()->find($categoryId);
But i'm unable to get the data attributes that are linked to Category and its attributes Values.
Result:
you can join your tables and select the columns you want:
Category::whereIn('id', $ids)->join('attribute', 'attribute.cat_id', '=', 'category.id')
->join('attribute_value', 'attribute_value.attr_id', '=', 'attribute.id')
->select('category.category_name', 'attribute.attribute_name', 'attribute_value.attr_value')->get();
using relations:
Category:: with(['categoryAttributes'=>function($query){
$query->addSelect('attribute.cat_id','attribute.attribute_name')
->with(['attr_values'=>function($query){
$query->addSelect('attribute_value.attr_value','attribute.attr_id');
}]);
}])->whereIn('id', $ids)->get();
please note that adding foreign key relation column to select statement is necessary so laravel get the right object

Laravel - Has Relationship Through

Let's say I have three tables:
1. Server
-------
id
name
2. Player
-------
id
name
ServerId
3. Activity
-------
id
PlayerId
action
time
How would I use Laravel's hasManyThrough to able to get a list of activity from a server's players like so $system->activity?
You can use it as follows:
// this should be inside your Server model.
public function activities()
{
return $this->hasManyThrough(
Player::class,
Activity::class,
'PlayerId', // Foreign key on activities table...
'ServerId', // Foreign key on players table...
'id', // Local key on servers table...
'id' // Local key on activities table...
);
}
This will give you all the server activities through the player.

Categories