Support the ongoing development of Laravel.io →
Database Eloquent

I have 2 models Crew and Event with the corresponding tables crews and events. Crews have many events, and events have one crew. I set up my models and migration as follows:

        //Crews
	    Schema::connection('scheduling')->create('crews', function ($table) {
    			$table->increments('id');
    			$table->text('name');
    			$table->boolean('solo');
    			$table->boolean('active');
    			$table->text('phone');
    		});
    
    		//Events
    		Schema::connection('scheduling')->create('events', function ($table) {
    			$table->increments('id');
    			// ...
    			$table->integer('crew_id')->unsigned();
    			$table->foreign('crew_id')->references('id')->on('crews');
    			$table->text('notes');
    			// ...
    			$table->timestamps();
    		});
    namespace App\Models\Scheduling;
    
    use Illuminate\Database\Eloquent\Model;
    
    class Crew extends Model {
    
    	public $connection = "scheduling";
    	public $table = "crews";
    
    	public function events() {
    		return $this->hasMany('App\Models\Scheduling\Event', 'id', 'crew_id');
    	}
    
    	public static function active() {
    		return Crew::where('active', 1)->get();
    	}
    
    }    
    namespace App\Models\Scheduling;
    
    use Illuminate\Database\Eloquent\Model;
    
    class Event extends Model {
    
    	public $connection = "scheduling";
    	public $table = "events";
    
    	public function crew() {
    		return $this->belongsTo('App\Models\Scheduling\Crew', 'crew_id', 'id');
     	}
    
    } 

If I run Crew::find(102)->events; I end up with an empty collection. If I run Events::where('crew_id', 102)->get(); I end up with the list of events I expected. Any idea what I'm doing wrong here?

Last updated 3 years ago.
0

If I speak in the tongues of mortals and of angels, but do not have love, I am a noisy gong or a clanging cymbal. And if I have prophetic powers, and understand all mysteries and all knowledge, and if I have all faith, so as to remove mountains, but do not have love, I am nothing. If I give away all my possessions, and if I hand over my body so that I may boast, but do not have love, I gain nothing.Love is patient; love is kind; love is not envious or boastful or arrogant or rude. It does not insist on its own way; it is not irritable or resentful; it does not rejoice in wrongdoing, but rejoices in the truth. It bears all things, believes all things, hopes all things, endures all things.

http://www.fifa16coinsmall.com

http://www.2maplestory.com/

http://www.rsgpfast.com

0

@jjrwin

try Crew::find(102)->events()->get();

0
Solution

I solved it this morning. It should be something like this

return $this->hasMany('App\Models\Scheduling\Event');
return $this->belongsTo('App\Models\Scheduling\Crew');

or alternatively, I had to switch the crew_id and id fields around

return $this->hasMany('App\Models\Scheduling\Event', 'crew_id', 'id');
return $this->belongsTo('App\Models\Scheduling\Crew', 'id', 'crew_id');
0

Sign in to participate in this thread!

PHPverse

Your banner here too?

jirwin jirwin Joined 4 Aug 2015

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2025 Laravel.io - All rights reserved.