In my application I have several mysql tables: Toronto, Vancouver, Montreal, etc... and I am using the DB-class to work with them, eg.
$data = DB::select('select * from toronto where id = ?', array($id));
What I want to do is to start using Eloquent. I am new to Laravel and was just wondering if its possible to have one model work with several tables, smth like:
class City extends Eloquent {
protected $table_a = 'toronto';
protected $table_b = 'vancouver';
protected $table_c = 'montreal';
}
It cannot, but you can. There are many ways, here's one:
Create your a City model that asks for a table name in its constructor:
class City extends Eloquent {
public function __construct($city, array $attributes = array())
{
parent::__construct($attributes);
$this->table = $city;
}
}
To use it you'll have to instantiate your class using the table name:
$toronto = new City('toronto');
Then you can do anything you want with it:
var_dump( $toronto->where('id',701057)->get() );
You can do $model->setTable('mytable') once you have a model instanciated, but for multiple tables I would rather recommend you make one "base" model with all the functionality you need and then several other classes which extend this class but define their own table with protected $table = 'table'.
However, in your case it sounds like you shouldn't be keeping the data in separate database tables at all. If you can find a way to store the state as a column instead of in separate tables, that would be better.
Related
We have records of different models but they extend from the same parent model.
I have a page where I would like to display a list of all records of that parent. How would one retrieve all records as the parent model?
example:
class LivingBeing extends Model{
public function isAlive(){
return $this->is_alive; //boolean
}
}
class Human extends LivingBeing{
use hasSpine; //eloquent handling for spine data
$table = 'humans';
}
class Cat extends LivingBeing{
use hasSpine; //eloquent handling for spine data
$table = 'cats';
}
class Squid extends LivingBeing{
use hasTentacles; //eloquent handling for tentacle data
$table = 'squids';
}
I would like to retrieve a morphable eloquent collection of data like:
App\Models\LivingBeing //Human 1
App\Models\LivingBeing //Cat 1
App\Models\LivingBeing //Cat 2
App\Models\LivingBeing //Squid 1
is there a way to query all LivingBeings model similar to.. ?
LivingBeing::all()
I only used biological things as example. our system implements the same as documents instead. :)
it depends on how you stored the models in database.
First Approach : Species as a field
if you have one table for living_beings and it has a column name species
you can access all with:
LivingBeing::all();
and in each model you have to implement the scope:
public static function boot()
{
parent::boot();
static::addGlobalScope("specie", function ($query) {
//specify type in child class
//$query->where("specie_type", "cat");
//specify in child if you have a species table
//$query->where("specie_id", 2);
//i think you can do it in parent once but i am not sure
$query->where("specie", this::class.'');
});
}
Second Approach : every Species has a table with living being table
each species has a field name living_being_id
access each species
Cat::all();
or by living being:
LivingBeing::select("*")::join("cats","cats.living_being_id","living_beings.id")->get();
Third Approach : (not recommended - not data normalized ) every species has a table itself.
to get all species, you have to union query:
Cat::all()->union(Dog::all());
Conclusion
The Best is Approach #1
What I need to do is extend all of the functionality of the Spatie permissions package Role model, but use a different table for the derived model.
Right now I have a model SubscriptionPackage that I want to emulate the behavior of a Role such that it can be assigned permissions and in turn this model can be assigned to users. But I wanna keep the Role model intact too.
I have tried extending Yes, but when I create a new SubscriptionPackage, the new record is created in the roles tables instead of subscription_packages table despite specifying the table in my derived Model. As shown below
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use App\Models\Permission; // This extends from Spatie\Permission\Models\Permission
use Spatie\Permission\Models\Role as SpatieRole;
class SubscriptionPackage extends SpatieRole
{
//
protected $guarded = ['id'];
protected $table = 'subscription_packages';
/**
* The permissions that belong to the package.
*/
public function packagePermissions()
{
return $this->belongsToMany(Permission::class);
}
}
With the code above I expect when I create a new SubscriptionPackage, the record should be inserted into the subscription_packages table but in this case it goes to the roles table.
Any pointers on how to go about this will be highly appreciated.
If you have a look at the Role source code you will this inside the __construct method:
public function __construct(array $attributes = [])
{
$attributes['guard_name'] = $attributes['guard_name'] ?? config('auth.defaults.guard');
parent::__construct($attributes);
$this->setTable(config('permission.table_names.roles')); // <-- HERE IS THE PROBLEM!
}
So, if you want that your SubscriptionPackage to write its records in the right table you have to override this behaviour like this:
public function __construct(array $attributes = [])
{
parent::__construct($attributes)
$this->setTable('your_table_name'); // <-- HERE THE SOLUTION!
}
I don't think you can. Spatie already have 5 tables and fetched data from those only. But still if you want to make the change you have make the changes with table and column name in the model
I'm obviously missing something. I thought I was comfortable around laravel relationships...
I've 2 tables, named ratings and ratingdetails. The models are named Rating & Ratingdetail:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Rating extends Model
{
public function ratingdetails()
{
return $this->hasMany('App\Ratingdetail');
}
public function campaigns()
{
return $this->hasMany('App\Campaign');
}
}
and
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
use Spatie\Translatable\HasTranslations;
class Ratingdetail extends Model
{
use HasTranslations;
public $translatable = ['value'];
public function rating()
{
return $this->belongsTo('App\Rating');
}
}
When I try to access to my Rating model it works fine, but I can't access the relationships; the output is the following, despite there should be 4 Ratingdetails rows...:
{"id":1,"description":"fontawesome","created_at":null,"updated_at":null,"deleted_at":null}
Thank you all for your time !
$rating = Rating::find($request->rating_id);
return $rating->toJson();
In the above line of code, you're never accessing the ratingdetails relationship. They are not included by default, and need to be loaded before being available:
$rating = Rating::with(["ratingdetails"])->find($request->rating_id);
return $rating->toJson();
Including it via with() will "Eager load" the relationship and expose it to be accessed via
console.log(rating.ratingdetails);
// Will contain an array of 4 objects
Before converting to json, you'd be able to access $rating->ratingdetails, but once converted, you lose access unless you have previously loaded the relationship.
Actually I can't answer for this question without having the Models' $fillable attributes, or without DB Tables structures. But I think your tables have following columns:
"raitings" -> "id", "description", "created_at", "updated_at", "deleted_at"
"raitingdetails" -> "id", "raiting_id", "value", ...
In normal way, you need to create OneToMany relation for that 2 tables with foreign key. So in your "raitingdetails" migration you need to have something like this:
$table->unsignedBigInteger('raiting_id')->nullable();
$table->foreign('raiting_id')->references('id')->on('raitings')->onUpdate('cascade')->onDelete('cascade');
Your models are correct, but it not just cool now.. You can improve them by adding $fillable columns and FKs of relations (Note: if you're using traditional foreign key concept, like "partents.id"->"childs.partent_id", then you can leave this part too).
For getting all Rating details of 1 Rating, you can do this:
$rating = Rating::find($rating_id);
$rating_details_of_one = $rating->ratingdetails()->get()->toJson();
If you want to have Rating Details for all actions, you can add Accessor in your Rating model and attach that to $appends like this:
protected $appends = [ 'rating_details' ]; public function
public function getRatingDetailsAttribute() {
return $this->ratingdetails;
}
And in logic parts you can access like this:
$ratings = Rating::find($rating_id); // this will get with their "ratingdetails" relation
Or you can attach accessor on the fly without protected $appends and getRatingDetailsAttribute() function like this:
$rating = Rating::find($rating_id);
$rating_details_of_one = $rating->setAppends([ 'rating_details' ])->get()->toJSON();
If you want to have some Ratings with their details, you can use something like this:
$rating_details_of_many = Rating::where('description', 'fontawesome')->with('ratingdetails')->get()->toJson();
I'm having some trouble figuring out the polymorphic relationships.
I've read the documentation but for me it is quite confusing.
Hope anyone has the time to help me a bit to understanding it.
What I'm trying to do is to have a very simple tag system for some wallpapers.
I started a new test project just to get this working.
I have 3 models: Wallpaper, Tag and WallpaperTag
class Wallpaper extends Model
{
protected $primaryKey = 'wallpaper_id';
protected $table = 'wallpapers';
protected $guarded = ['wallpaper_id'];
/**
* Get all the tags assigned to this wallpaper
*/
public function tags()
{
//
}
}
class Tag extends Model
{
protected $primaryKey = 'tag_id';
protected $table = 'tags';
protected $guarded = ['tag_id'];
/**
* Get all wallpapers that have this given tag
*/
public function wallpapers()
{
//
}
}
class WallpaperTag extends Model
{
protected $primaryKey = 'wallpaper_tag_id';
protected $table = 'wallpaper_tags';
protected $guarded = ['wallpaper_tag_id'];
/**
* #return \Illuminate\Database\Eloquent\Relations\BelongsTo
* Wallpaper relation
*/
public function wallpaper()
{
return $this->belongsTo('App\Wallpaper','wallpaper_id');
}
/**
* #return \Illuminate\Database\Eloquent\Relations\BelongsTo
* Tag relation
*/
public function tag()
{
return $this->belongsTo('App\Tag','tag_id');
}
}
The wallpapers table in this test project contains only wallpaper_id
The tags table contanis a tag_id and a tag
The wallpaper_tags table contains a foreign key for both tags.tag_id and wallpapers.wallpaper_id
I've set it up like this so wallpapers can share tags without duplicating them. The problem is that I really dont understand the polymorphic relations and the example in the documentation.
Can anyone here 'spoonfeed' how this would work? :') Thanks in advance for all help.
So you are trying to create a relationship with ManyToMany between 2 tables, which in the DB needs a 3rd table to allow you to create such relationship.
This is due to the fact that one Wallpaper can have many Tag and vice versa! For such you need a 3rd table that holds that information accordingly.
The 3rd table is only holding ids in relationship to your 2 main tables. This allows the flexibility you are looking for, while your Object tables can actually hold information specific to them, without you having to duplicate it.
If you were to store the relationship ids on both tables you would be forced to duplicate your data and that is just something you do not wish on databases! Imagine having to update 1000 rows because it is basically the same wallpaper but with so many different tags.
Anyway, below is the code that should be get you going:
You do need to create a class to represent your relationship table (Kudos on the WallpaperTag class! That is the one!);
You do not touch that class anymore, do not add belongs or any other function!
You create the relationships on the main classes Wallpaper and Tag;
class Wallpaper extends Model
{
...
public function tags()
{
return $this->belongsToMany('App\Tag', 'wallpaper_tag', 'tag_id', 'wallpaper_id');
}
}
class Tag extends Model
{
...
public function wallpapers()
{
return $this->belongsToMany('App\Wallpaper', 'wallpaper_tag', 'wallpaper_id', 'tag_id');
}
}
class WallpaperTag extends Model
{
}
Laravel should create a relationship between your classes and map it accordingly to the correct 3rd table to sort the search for you.
If you follow the semantics all you needed was the class name. If ids are to change, then you will need to start telling Laravel what id column names it should be looking for as you deviate from the normal behaviour. It still finds it, just needs some guidance on the names! Hence why we start adding more parameters to the relationships belongsTo or hasMany etc :)
Pivot Table Migration
You do not need an id for your pivot table since your primary key is a combination of the two foreign keys from the other tables.
$table->bigInteger('wallpaper_id')->unsigned()->nullable();
$table->foreign('wallpaper_id')->references('wallpaper_id')
->on('wallpaper')->onDelete('cascade');
$table->bigInteger('tag_id')->unsigned()->nullable();
$table->foreign('tag_id')->references('tag_id')
->on('tags')->onDelete('cascade');
Let me know if it helped! :3
I'm fairly new with Laravel. I'm still trying to learn it. My question is:
I have 3 tables named
games
game_options
game_platforms
I have 3 Models for those tables
Game Model
class Game extends Eloquent
{
protected $table = 'games';
public function platforms()
{
return $this->hasManyThrough('GamePlatform','GameOptions','id','game_id');
}
}
GamePlatform Model
class GamePlatform extends Eloquent
{
protected $table = 'game_platform';
}
GameOption Model
class GameOptions extends Eloquent
{
protected $table = 'game_options';
}
So when I do
$game = Game::find(1)->platforms;
It only shows,
{"id":1,"platform_id":20,"game_id":1}
{"id":1,"platform_id":21,"game_id":1}
{"id":1,"platform_id":22,"game_id":1}
{"id":1,"platform_id":23,"game_id":1}
{"id":1,"platform_id":24,"game_id":1}
But I need game name and platform names with those ID's. The thing is, I want to do this with eloquent only. I could go with "DB" or oldschool SQL but I want to learn if this way is possible or not.
Also I'm looking for better documentation/books for laravel. Most of what I read were only introduce laravel or far too advanced for me.
I left a comment earlier about this but now I'm pretty sure it's the answer you're looking for: you should use belongsToMany rather than hasManyThrough. So first, might I suggest you rename your tables and models to follow Laravel's conventions (plural snake_case table names, singular snake_case alphabetical order pivot table names, singular StudlyCaps model names), that way you'll have the following situation:
Tables:
games
id
name
game_option
id
game_id
option_id
options
id
option
name
Now you can rewrite your models to conform to the new structure, and use a belongsToMany relationship too:
class Game extends Eloquent
{
public function platforms()
{
return $this->belongsToMany('Option');
}
}
class Option extends Eloquent
{
public function platforms()
{
return $this->belongsToMany('Game');
}
}
Note: you don't have to model the pivot table (game_option) unless you store extra data on the pivot.
Now you should be good to get all options for a given game:
$options = Game::find(1)->options;
Or if you need to get all platforms (though I am trying to infer meaning of your code here regarding options and platforms):
$platforms = Game::find(1)->options()->whereOption('platform')->get();
you can use the with method with eloquent
$game = Game::where('id',1)->with('platforms')->get();
Should return you the game and platforms
For documentation I would first start with the documentation provided (find it to be about 50% complete) and with the api everything else is covered
You would have to model your tables like:
**games**
id
name
**game_options**
id
game_id
name
**game_platform**
id
game_options_id
platform_id /* which i assume you get from a platform master table */
Then in your Game Class:
class Game extends Eloquent
{
protected $table = 'games';
public function platforms()
{
return $this->hasManyThrough('GamePlatform','GameOptions','game_id','game_options_id');
}
}
Now, this would be assuming that Game Platform belongs to Games through Game Options.