TristanKobalt
1 month ago
459
10
Eloquent

Using model variables in a relationship function

Posted 1 month ago by TristanKobalt

I am trying to constrain a relationship based on another relationship from that same model.

class CinemaRoom extends Model {

	public function cinema() {
		return $this->belongsTo(Cinema::class);
	}


	public function bookableMovies() {
		$cinema_id = $this->cinema_id;	// $this->cinema_id appears not to be set at this point

		return $this->belongsToMany(Movie::class)
			->whereHas('movie_licenses', function ($query) use($cinema_id) {
            			$query->where('cinema_id', $cinema_id)->active();
	        });
	}
}

However, the cinema_id attribute doesn't appear to be accessible. What would be a better way to expose this business logic from the model?

Please sign in or create an account to participate in this conversation.