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');
}
}
Related
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 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';
}
I can not find why it does not work.
In my Course model I have defined relation:
class Course extends Model {
public function courseDates() {
return $this->hasMany(CourseDate::class, 'course_id');
}
}
And in my CourseDate model this:
class CourseDate extends Model {
public function course() {
return $this->belongsTo(Course::class);
}
}
When I try to access CourseDates from Course I will always get null, but when I access Course from CourseDate, it works and I see all data:
var_dump(CourseDate::where('id', 1)->first()->course->name); => output: "Course 1"
var_dump(Course::where('id', 1)->first()->courseDate); => output: null
And what's strange when I try it with another course (like ID 2) then it works. The data is absolutely the same in the database. Any Ideas?
You shouldent add class like CourseDate::class instead you should add it like below
class Course extends Model {
public function courseDates() {
return $this->hasMany('App\CourseDate', 'course_id');
}
}
And in your CourseDate model
class CourseDate extends Model {
public function course() {
return $this->belongsTo('App\Course');
}
}
And your relationship method for Course model is courseDates so you should use it like below
var_dump(Course::where('id', 1)->first()->courseDates);
Documentation : https://laravel.com/docs/5.7/eloquent-relationships
I have 3 layers of model relationships
the Grandparent Class
class GrandParent extends Model
{
protected $guarded = [];
public function parents()
{
return $this->hasMany('App\Parent');
}
public function childs()
{
return $this->hasManyThrough('App\Childs', 'App\Parent');
}
}
the parent Class
class Parent extends Model
{
public function grandParent() {
return $this->belongsTo('App\GrandParent', 'grandParent_id', 'id');
}
public function child()
{
return $this->hasMany('App\Child');
}
}
The child class
class Child extends Model
{
public function parent() {
return $this->belongsTo('App\Parent', 'parent_id', 'id');
}
}
i want to get list of Child by the grandParent ID
There is any way to get the list by simple Query
i am using laravel 5.5
thanks.
You need to add the grandparent_id and user_id from the user_table like below.
public function childs()
{
return $this->hasManyThrough('App\Childs', 'App\Parent','grandparent_id','parent_id','id','id');
}
In your Controller file.
$data = Grandparent::find($grandparent_id)->childs;
How do you apply belongsTo and hasMany relationship on same model?
For example, I have one User and one Project model. Now Project model has method user() and User model has projects() method.
Now I want a user to share projects with other users. So that I can have methods like users() in Project model and shared_projects() in User model.
How I can achieve that?
Here is my current Project model
class Project extends \Eloquent {
protected $fillable = [];
public function user() {
return $this->belongsTo('User');
}
}
And this my User model
class Project extends \Eloquent {
protected $fillable = [];
public function projects() {
return $this->hasMany('Project');
}
}
In this case, User and Project have Many-to-Many relationship because one user has many projects and one project belongs to many user. Therefore, you should create a third table (project_user) which connect User table and Project table in the database.
And you set Many-to-Many relation to User Model and Project Model.
class User extends Eloquent {
public function projects()
{
return $this->belongsToMany('Project');
}
}
In Project model
class Project extends Eloquent {
public function users()
{
return $this->belongsToMany('User');
}
}
Then you can do something like this
//users in the same project
$users = Project::find($id)->users;
Okay, so here it is how I solved it. I used pivot table as you suggested but I even added hasMany relationship like this way. I am still not sure what I am doing is perfectly correct but it worked. :)
class Project extends \Eloquent {
protected $fillable = [];
public function users() {
return $this->belongsToMany('User');
}
public function user() {
return $this->belongsTo('User');
}
}
And here is the key ingredient in User model
class User extends \Eloquent {
protected $fillable = [];
public function projects_owned() {
return $this->hasMany('Project', 'user_id');
}
public function projects() {
return $this->belongsToMany('Project');
}
}