I am finding some difficulties to bind one relationship table to my second database.
In "db_main" I have tables, with the basic details about each object and than I have other tables: "db_site1", "db_site2" etc, in which tables I save detailed information about the same objects.
Everything is working find with that schema, except one situation in that I have relationship table (bookmaker_games), which must be only on "db_main". The table save the relations between bookmakers and games.
On my website1 I want to list the games of a bookmaker with the following models:
class Bookmaker extends Model {
public function games() {
// here the database is "db_site1"
return $this->belongsToMany('App\Models\Game', 'bookmaker_games', 'bookmaker_id', 'game_id');
}
}
class Game extends Model {
public function bookmakers() {
// here the database is "db_site1"
return $this->belongsToMany('App\Models\Bookmaker', 'bookmaker_games', 'game_id', 'bookmaker_id');
}
}
class BookmakerGame extends Model {
protected $connection = 'db_main';
}
When I try to return all games, it is thinking that "bookmaker_games"-table is on database "db_site1", which is wrong.
What is the best way to tell that relationship to look at the correct database ?
Declare BookmakerGame as a subclass of Illuminate\Database\Eloquent\Relations\Pivot
Then define the relationships to use it.
Prefixing the connection name
//...
use Illuminate\Database\Eloquent\Relations\Pivot;
class Bookmaker extends Model {
public function games() {
// here the database is "db_site1"
return $this->belongsToMany(
'App\Models\Game', 'db_main.bookmaker_games', 'bookmaker_id', 'game_id')
->using('App\Models\BookmakerGame');
}
}
class Game extends Model {
public function bookmakers() {
// here the database is "db_site1"
return $this->belongsToMany(
'App\Models\Bookmaker', 'db_main.bookmaker_games', 'game_id', 'bookmaker_id')
->using('App\Models\BookmakerGame');
}
}
class BookmakerGame extends Pivot {
protected $connection = 'db_main';
}
Using the class path to the pivot
use Illuminate\Database\Eloquent\Relations\Pivot;
class Bookmaker extends Model {
public function games() {
// here the database is "db_site1"
return $this->belongsToMany(
'App\Models\Game', 'App\Models\BookmakerGame', 'bookmaker_id', 'game_id');
}
}
class Game extends Model {
public function bookmakers() {
// here the database is "db_site1"
return $this->belongsToMany(
'App\Models\Bookmaker', 'App\Models\BookmakerGame', 'game_id', 'bookmaker_id');
}
}
class BookmakerGame extends Pivot {
protected $connection = 'db_main';
protected $table = 'bookmaker_games';
}
Related
For example, there are four models as follows.
class User extends Authenticatable
{
public function orders(): MorphMany
{
return $this->morphMany(Order::class, 'orderable');
}
}
class Partner extends Model implements Authenticatable
{
public function orders(): MorphMany
{
return $this->morphMany(Order::class, 'orderable');
}
}
class Order extends Model
{
public function orderable(): MorphTo
{
return $this->morphTo();
}
public function projects(): BelongsToMany
{
return $this->belongsToMany(Project::class)->withPivot('amount', 'price');
}
}
class Project extends Model
{
public function orders(): BelongsToMany
{
return $this->belongsToMany(Order::class)->withPivot('amount', 'price');
}
}
User、Partner and Order are one to many polymorphic relations。
Order and Project are many to many relations。
How to elegantly get all projects enrolled by a given user?
Thanks!
I have existing data in the database, and I want to create a one-to-many relationship. How can I limit or simulate a one-to-many relationship when I'm using a pivot table?
I want to get the data from the pivot table, so I thought it could be done like this.
class Asset extends Model {
public function publisher() {
return $this->belongsTo(Publisher::class, 'asset_publisher');
}
}
-
class Publisher extends Model {
public function assets() {
return $this->hasMany(Asset::class, 'asset_publisher');
}
}
A possible way to simulate the one-to-many is creating a model for the pivot table "AssetPublisher" so you will now have
class Publisher extends Model {
public function assets() {
return $this->belongsToMany(Asset::class, 'asset_publisher');
}
}
class AssetPublisher extends Model {
public function publisher() {
return $this->belongsTo(Publisher::class);
}
}
class Asset extends Model {
public function assetPublisher() {
return $this->hasOne(AssetPublisher::class)->with('publisher');
}
public function getPublisherAttribute(){
return $this->assetPublisher->publisher;
}
}
You can now call $asset->publisher to give you the Publisher instance and $publisher->assets()->get() to get all assets instances
I have a recently started learning Laravel 5.4, I am having trouble with my 3 way table relationship, I've looked at a few articles online regarding many to many, but this relationship is just a "hasOne" on both sides.
Could anyone give me a helpful hint as to how to structure my table relationship, here is the PK/FK relationship:
Users table (id)
Listings table (id, user_id)
Insights table (id, listing_id) - one insight row per listing only.
And the models below:
Users Model
class User extends Model
{
public function listing()
{
return $this->belongsTo('App\Listing');
}
}
Listing Model
class Listing extends Model
{
public function insight()
{
return $this->hasOne('App\Insight');
}
}
Insight Model
class Insight extends Model
{
public function listing()
{
return $this->hasOne('App\Listing');
}
}
And what I am trying to achieve is to query the users own listings, with each listings current insights.
Thanks a bunch.
Simon.
User model
class User extends Model
{
public function listing()
{
return $this->hasOne('App\Listing');
}
}
Listing Model
class Listing extends Model
{
public function insight()
{
return $this->hasOne('App\Insight');
}
public function user()
{
return $this->belongsTo('App\User');
}
}
Insight Model
class Insight extends Model
{
public function listing()
{
return $this->belongsTo('App\Listing');
}
}
And if you want query users with Listing and Insight
$users = User::with(['listing', 'listing.insight'])->get();
foreach($users as $user) {
$user->listing->insight;
}
class User extends Model
{
public function listing()
{
return $this->hasMany(Listing::class);
}
}
class Listing extends Model
{
public function insight()
{
return $this->hasOne(Insight::class);
}
public function user()
{
return $this->belongsTo(User::class);
}
}
class Insight extends Model
{
public function listing()
{
return $this->belongsTo(Listing::class);
}
}
$users = User::with('listing.insight')->get();
foreach($users as $user) {
$user->listing->insight;
}
Using Laravel 4.2, how can I set a hasOne relation on a model to an instance of a new model without touching the database?
I want to do the following, but Laravel is treating the instance as a property, not a child relation.
class ParentClass extends \Eloquent {
public function child() {
return $this-hasOne('ChildClass', 'child_id', 'parent_id');
}
}
$parent = new ParentClass();
$parent->child = new ChildClass();
To get through my problem, I have created a Trait class that I added to my models that allows me to set a relation. Doing this does not automatically set my parent/child relation values so I have to be careful to do this manually before I save/push.
trait ModelSetRelationTrait
{
public function setRelation($key, $model)
{
$this->relations[$key] = $model;
}
}
class ParentClass extends \Eloquent {
use ModelSetRelationTrait;
public function child() {
return $this-hasOne('ChildClass', 'child_id', 'parent_id');
}
}
I have different Database tables and below is the DB Diagram.
I am trying to make a many to many polymorphic relationship, but no luck, Please guide me that what i need to do, means where i need to add which type relationship/code?
Course Model
class Course extends Eloquent {
public function quizzes()
{
return $this->morphToMany('Quiz', 'quizzable');
}
}
Chapter Model
class Chapter extends Eloquent {
public function quizzes()
{
return $this->morphToMany('Quiz', 'quizzable');
}
}
Lesson Model
class Lesson extends Eloquent {
public function quizzes()
{
return $this->morphToMany('Quiz', 'quizzable');
}
}
Quiz Model
class Quiz extends Eloquent {
public function courses()
{
return $this->morphedByMany('Course', 'quizzable');
}
public function chapters()
{
return $this->morphedByMany('Chapter', 'quizzable');
}
public function lessons()
{
return $this->morphedByMany('Lesson', 'quizzable');
}
}
Source: http://laravel.com/docs/4.2/eloquent#many-to-many-polymorphic-relations
I would change the quizzes_assigned table to:
Table name: quizzables
quiz_id - integer
quizzable_id - integer
quizzable_type - string