TristanKobalt

TristanKobalt

Member Since 1 Month Ago

Experience Points
100
Total
Experience

4,900 experience to go until the next level!

In case you were wondering, you earn Laracasts experience when you:

  • Complete a lesson — 100pts
  • Create a forum thread — 50pts
  • Reply to a thread — 10pts
  • Leave a reply that is liked — 50pts
  • Receive a "Best Reply" award — 500pts
Lessons Completed
0
Lessons
Completed
Best Reply Awards
0
Best Reply
Awards
  • start your engines Created with Sketch.

    Start Your Engines

    Earned once you have completed your first Laracasts lesson.

  • first-thousand Created with Sketch.

    First Thousand

    Earned once you have earned your first 1000 experience points.

  • 1-year Created with Sketch.

    One Year Member

    Earned when you have been with Laracasts for 1 year.

  • 2-years Created with Sketch.

    Two Year Member

    Earned when you have been with Laracasts for 2 years.

  • 3-years Created with Sketch.

    Three Year Member

    Earned when you have been with Laracasts for 3 years.

  • 4-years Created with Sketch.

    Four Year Member

    Earned when you have been with Laracasts for 4 years.

  • 5-years Created with Sketch.

    Five Year Member

    Earned when you have been with Laracasts for 5 years.

  • school-in-session Created with Sketch.

    School In Session

    Earned when at least one Laracasts series has been fully completed.

  • welcome-newcomer Created with Sketch.

    Welcome To The Community

    Earned after your first post on the Laracasts forum.

  • full-time-student Created with Sketch.

    Full Time Learner

    Earned once 100 Laracasts lessons have been completed.

  • pay-it-forward Created with Sketch.

    Pay It Forward

    Earned once you receive your first "Best Reply" award on the Laracasts forum.

  • subscriber Created with Sketch.

    Subscriber

    Earned if you are a paying Laracasts subscriber.

  • lifer Created with Sketch.

    Lifer

    Earned if you have a lifetime subscription to Laracasts.

  • evangelist Created with Sketch.

    Laracasts Evangelist

    Earned if you share a link to Laracasts on social media. Please email [email protected] with your username and post URL to be awarded this badge.

  • chatty-cathy Created with Sketch.

    Chatty Cathy

    Earned once you have achieved 500 forum replies.

  • lara-veteran Created with Sketch.

    Laracasts Veteran

    Earned once your experience points passes 100,000.

  • 10k-strong Created with Sketch.

    Ten Thousand Strong

    Earned once your experience points hits 10,000.

  • lara-master Created with Sketch.

    Laracasts Master

    Earned once 1000 Laracasts lessons have been completed.

  • laracasts-tutor Created with Sketch.

    Laracasts Tutor

    Earned once your "Best Reply" award count is 100 or more.

  • laracasts-sensei Created with Sketch.

    Laracasts Sensei

    Earned once your experience points passes 1 million.

  • top-50 Created with Sketch.

    Top 50

    Earned once your experience points ranks in the top 50 of all Laracasts users.

Level 1
100 XP
Aug
05
1 month ago
Activity icon

Replied to Using Model Variables In A Relationship Function

I've tried a couple of things, but can't seem to crack it.

First I've tried directly referencing the table and id as you suggested, but that results in an error (the reason for which I've outlined below).

Second, I've read the article you suggested, but am unsure how to translate its contents to my current use case.

So the code I'm trying to get working is essentially this:

$cinema->rooms->bookableMovies;

With my work-in-progress bookableMovies relationship (which doesn't check if the active movie license actually belongs to the cinema which this room is a part of yet) looking like this:

	public function bookableMovies() {
		return $this->belongsToMany('App\Movie')->whereHas('movie_licenses', function ($query) {
            $query->active();
        });
	}

When I run that, for a cinema with 3 rooms (with id's 1,2 and 3), it generates this query:

select `movies`.*, `movie_room`.`room_id` as `pivot_room_id`, `movie_room`.`movie_id` as `pivot_movie_id` from `movies` inner join `movie_room` on `movies`.`id` = `movie_room`.`movie_id` where exists (select * from `movie_licenses` where `movies`.`id` = `movie_licenses`.`movie_id` and `from` <= 2020-08-05 00:00:00 and `to` >= 2020-08-05 00:00:00) and `movie_room`.`room_id` in (1, 2, 3))

It looks like the rooms table is not included in the query at all, so I can't check if the arcade_id of the room, matches the arcade_id of the active movie_license.

I'm really quite stuck. I could probably write the intended query in pure SQL, put would really prefer to have it as part of my model definition.

Aug
02
1 month ago
Activity icon

Replied to Using Model Variables In A Relationship Function

You're right. A Cinema with a License for a certain movie can show that movie in any amount of Rooms.

I've included the fields / table structures that are likely relevant below:

        Schema::create('movie_licenses', function (Blueprint $table) {
            $table->id();
	    $table->foreignId('cinema_id');
            $table->foreignId('movie_id');
            $table->date('from');   // date at which the license starts
            $table->date('to');     // date at which the license will expire                        
            $table->timestamps();
        });
        Schema::create('rooms', function (Blueprint $table) {
            $table->id();
            $table->string('name', 100);   
            $table->foreignId('cinema_id');
            $table->timestamps();
        });

Thank you for your second suggestion as well. I'm going to try out a couple of things, and get back here with the results.

Jul
30
1 month ago
Activity icon

Replied to Using Model Variables In A Relationship Function

Yes, a CinemaRoom can belong to one, and only one Cinema.

If I understand correctly, your suggestion is to do something like this:

class CinemaRoom {
	...

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

But wouldn't that also return movies, which are scheduled for that room, but to which another cinema happens to hold an active license?

Jul
29
1 month ago
Activity icon

Replied to Using Model Variables In A Relationship Function

Yeah, it's a bit of a freaky thing I'm trying to do here.

So, in natural language here are my relationships:

A cinema has multiple rooms. A cinema has multiple movie licenses. A cinema has scheduled multiple movies in each room.

I want to ask for a specific room, which movies with an active license (on the current date) are scheduled there. (I call these "the bookable movies", i.e. the movies in that room for which tickets can be legally sold by the cinema.)

So it's quite close to what you're both suggesting.

Activity icon

Replied to Using Model Variables In A Relationship Function

I understand your suggestion @nakov. However, I'm looking for just the bookableMovies for this specific Room, instead of all the bookable movies for the entire cinema.

Activity icon

Started a new Conversation Using Model Variables In A Relationship Function

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?