sraines

sraines

Member Since 1 Year Ago

Experience Points 9,130
Experience Level 2

870 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 86
Lessons
Completed
Best Reply Awards 0
Best Reply
Awards
  • start-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-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-token Created with Sketch.

    Subscriber

    Earned if you are a paying Laracasts subscriber.

  • lifer-token Created with Sketch.

    Lifer

    Earned if you have a lifetime subscription to Laracasts.

  • lara-evanghelist 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.

12 Jul
1 week ago

sraines left a reply on Where With Difference Of 2 Columns

I figured it out with whereRaw:

$quota_date =
    DB::table('deadline_calendar_quota')
        ->select('quota_date')
        ->where('active', '=', 'y')
        ->where('quota_date', '>=', $quota_start_date)
        ->when($embroidery > 0, function($query) use ($embroidery) {
            return $query->whereRaw('emb_quota - emb_units_sold >= ?', [$embroidery]);
        })
        ->when($screen > 0, function($query) use ($screen) {
            return $query->whereRaw('print_quota - print_units_sold >= ?', [$screen]);
        })
        ->orderBy('quota_date', 'ASC')
        ->limit(1)
        ->get();

sraines left a reply on Where With Difference Of 2 Columns

I've added the create code to the original comment. Basically the table is for tracking production day by day, so that we don't sell more units than we can actually produce and get shipped out on time. So there's a date (the quota_date), and embroidery quota, a screen print quota, and then the actual number of embroidery and screen print units sold for that quota date.

I had the orderBy on there because I need to get the earliest available quota date. I guess I wasn't sure how it would be sorted without it, so I didn't want to get, for instance, the latest open quota date.

sraines left a reply on Where With Difference Of 2 Columns

I'm using Validator to check the post data before processing anything, this is what I have for that particular part of the JSON:

    'product_types.embroidery.qty' => 'required|integer|min:0'

Is there anything else I should be doing to avoid SQL injection?

sraines started a new conversation Where With Difference Of 2 Columns

I'm working with Laravel v5.4.36

Like the title says, I'm trying to do something like this:

SELECT *
FROM table
WHERE column1 - column2 > 50

Where 50 would be some variable value in a request object. This is doable in plain SQL, but I can't figure out how to do it with Laravel's query builder. The clause also needs to be conditional, so it needs to be inside of a when(), if that makes a difference. Here's what I have now:

$embroidery = $request->input('product_types.embroidery.qty');
$screen = $request->input('product_types.screen.qty');

// Hardcoded for now
$buffer_days = 17;  

$quota_date =
    DB::table('deadline_calendar_quota')
        ->select('quota_date')
        ->where('active', '=', 'y')
        ->where('quota_date', '>=', "NOW() + INTERVAL $buffer_days DAY")
        ->when($embroidery > 0, function($query) use ($embroidery) {
            return $query->where(DB::raw('emb_quota - emb_units_sold'), '>=', $embroidery);
        })
        ->when($screen > 0, function($query) use ($screen) {
            return $query->where(DB::raw('print_quota - print_units_sold'), '>=', $screen);
        })
        ->orderBy('quota_date', 'ASC')
        ->limit(1)
        ->get();
21 May
1 month ago

sraines started a new conversation Eloquent ORM Nested Eager Loaded Relations

Okay, I have a Laravel app I'm working on that's like a Netflix style streaming service. My main table for movies / TV shows looks like this:

CREATE TABLE `media` (
    `id` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,
    `media_type` ENUM('movie','show') NOT NULL COLLATE 'utf8mb4_unicode_ci',
    `title` VARCHAR(191) NOT NULL COLLATE 'utf8mb4_unicode_ci',
    `summary` VARCHAR(4000) NOT NULL COLLATE 'utf8mb4_unicode_ci',
    `notes` VARCHAR(191) NULL DEFAULT NULL COLLATE 'utf8mb4_unicode_ci',
    `poster` VARCHAR(191) NOT NULL DEFAULT 'missing-poster.jpg' COLLATE 'utf8mb4_unicode_ci',
    `jumbotron` VARCHAR(191) NOT NULL COLLATE 'utf8mb4_unicode_ci',
    `created_at` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
    `updated_at` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    PRIMARY KEY (`id`)
)
COLLATE='utf8mb4_unicode_ci'
ENGINE=InnoDB
AUTO_INCREMENT=176
;

I designed everything with the idea that there could be multiple hard drives with content on them in the future (right now there's only one). So I have a drives table:

CREATE TABLE `drives` (
    `id` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,
    `name` VARCHAR(191) NOT NULL COLLATE 'utf8mb4_unicode_ci',
    `created_at` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
    `updated_at` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    PRIMARY KEY (`id`),
    UNIQUE INDEX `drives_name_unique` (`name`)
)
COLLATE='utf8mb4_unicode_ci'
ENGINE=InnoDB
AUTO_INCREMENT=2
;

I also have an episodes table that looks like this:

CREATE TABLE `episodes` (
    `id` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,
    `media_id` INT(10) UNSIGNED NOT NULL,
    `season` INT(10) UNSIGNED NOT NULL,
    `episode_number` INT(10) UNSIGNED NOT NULL,
    `title` VARCHAR(191) NOT NULL COLLATE 'utf8mb4_unicode_ci',
    `summary` VARCHAR(4000) NOT NULL COLLATE 'utf8mb4_unicode_ci',
    `created_at` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
    `updated_at` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    PRIMARY KEY (`id`),
    UNIQUE INDEX `episodes_media_id_season_episode_number_unique` (`media_id`, `season`, `episode_number`),
    CONSTRAINT `episodes_media_id_foreign` FOREIGN KEY (`media_id`) REFERENCES `media` (`id`) ON DELETE CASCADE
)
COLLATE='utf8mb4_unicode_ci'
ENGINE=InnoDB
AUTO_INCREMENT=616
;

And finally I have the pivot tables, episode_file and file_movie that join a drive with an episode or movie:

CREATE TABLE `episode_file` (
    `episode_id` INT(10) UNSIGNED NOT NULL,
    `drive_id` INT(10) UNSIGNED NOT NULL,
    `filename` VARCHAR(191) NOT NULL COLLATE 'utf8mb4_unicode_ci',
    `created_at` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
    `updated_at` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    UNIQUE INDEX `episode_file_drive_id_episode_id_unique` (`drive_id`, `episode_id`),
    INDEX `episode_file_episode_id_foreign` (`episode_id`),
    CONSTRAINT `episode_file_drive_id_foreign` FOREIGN KEY (`drive_id`) REFERENCES `drives` (`id`) ON DELETE CASCADE,
    CONSTRAINT `episode_file_episode_id_foreign` FOREIGN KEY (`episode_id`) REFERENCES `episodes` (`id`) ON DELETE CASCADE
)
COLLATE='utf8mb4_unicode_ci'
ENGINE=InnoDB
;

CREATE TABLE `file_movie` (
    `media_id` INT(10) UNSIGNED NOT NULL,
    `drive_id` INT(10) UNSIGNED NOT NULL,
    `filename` VARCHAR(191) NOT NULL COLLATE 'utf8mb4_unicode_ci',
    `created_at` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
    `updated_at` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    UNIQUE INDEX `file_movie_drive_id_media_id_unique` (`drive_id`, `media_id`),
    INDEX `file_movie_media_id_foreign` (`media_id`),
    CONSTRAINT `file_movie_drive_id_foreign` FOREIGN KEY (`drive_id`) REFERENCES `drives` (`id`) ON DELETE CASCADE,
    CONSTRAINT `file_movie_media_id_foreign` FOREIGN KEY (`media_id`) REFERENCES `media` (`id`) ON DELETE CASCADE
)
COLLATE='utf8mb4_unicode_ci'
ENGINE=InnoDB
;

What I want is to be able to return from a media controller one record that looks like this as a Javascript object:

{
    id: 1,
    title: 'Alien',
    summary: 'fdsafdsafdsa',
    notes: null,
    poster: 'alien.jpg',
    jumbotron: null,
    created_at: '2019-05-21 00:00:00',
    updated_at: '2019-05-21 00:00:00',
    file: {
        drive_id: 1,
        media_id: 1,
        created_at: '2019-05-21 00:00:00',
        updated_at: '2019-05-21 00:00:00',
        drive: {
            id: 1,
            name: 'hdd1',
            created_at: '2019-05-21 00:00:00',
            updated_at: '2019-05-21 00:00:00',
        }
    }
}

I have it now where I can join the episode_file or file_movie record onto the media record, but I don't have the 'name' field of the drive from the drives table. That's what I want, to be able to get that without a second query or API call. Anyone know how I can do this with Eloquent relationships?

25 Apr
2 months ago

sraines left a reply on Conditionally Transform Collection Data

Annnnd of course I figure out a solution as soon as I post the question.

$qty = 50;
$id = 2;
$a->transform(function($row) use ($qty, $id) {
    if($row['id'] == $id) {
            $row['qty_ordered'] = $qty;
    }
        return $row;
});

Seems like this should have worked with where. That's how I'd prefer to do it, but whatever. This works, so I'll take it.

sraines started a new conversation Conditionally Transform Collection Data

I have an array of associative arrays that I've turned into a collection. Looks something like this:

[
    ['id' => 1, 'customer_order_id' => 1234567, 'qty_ordered' => 15],
    ['id' => 2, 'customer_order_id' => 2345678, 'qty_ordered' => 350],
    ['id' => 3, 'customer_order_id' => 1204578, 'qty_ordered' => 100]
]

I want to transform or update the values on some given key or keys, let's say qty_ordered for example, where the id is equal to some value.

I've tried this:

$qty = 50;
$a->where('id', 2)->transform(function($row) use ($qty) {
        $row['qty_ordered'] = $qty;
        return $row;
});

But when I var_dump($a) the value is not changed. I don't know what I'm doing wrong. When I dd($row) inside the transform callback it shows the correct value, but the collection itself isn't modified.

17 Apr
3 months ago

sraines left a reply on Component Method Not Recognizing Updated Prop Or Data Values

I made a codepen so that people can try it for themselves. I added some console messages when you click the button. If you click it twice, then the video player shows.

https://codepen.io/stevemr/pen/VNQbYe

16 Apr
3 months ago

sraines left a reply on Component Method Not Recognizing Updated Prop Or Data Values

@JOUCKE - Just tried it, but I got the same result. The function is just immediately returning because it thinks src is an empty string.

It works when I trigger the setVideo function a second time, presumably because all the props were still set from the first time.

sraines started a new conversation Component Method Not Recognizing Updated Prop Or Data Values

I have a root Vue instance which maintains the props for a component, VideoPlayer. My root instance has a method called setVideo, which is just assigning some dummy values right now.

setVideo: function() {
    // Get the drive, filename, and mediaType
    this.video.drive = 'hdd1';
    this.video.filename = 'game-of-thrones_s01e04.mp4';
    this.video.mediaType = 'show';

    // Hide all modals and trigger the display of the video player
    Event.trigger('hideModal');
    Event.trigger('displayVideoPlayer');
},

The Event class is just a wrapper for basic Vue events:

window.Event = new class {
    constructor() {
        this.vue = new Vue();
    }

    trigger(event, data = null) {
        this.vue.$emit(event, data);
    }

    listen(event, callback) {
        this.vue.$on(event, callback);
    }
};

Here's the DOM where my VideoPlayer component is initialized:

<video-player
    v-bind:drive="video.drive"
    v-bind:filename="video.filename"
    v-bind:media-type="video.mediaType"
></video-player>

And finally, here's my VideoPlayer component:

<template>
    <div>
        <div id="movie-container">
            <div
                class="video-loader top-most"
                v-if="showVideoPlayer && !loaded"
            ></div>
            <video
                id="video-player"
                ref="video"
                v-if="showVideoPlayer && src !== ''"
                class="top-most"
                v-bind:class="{ hidden: !loaded }"
                v-on:click="togglePlay"
                controls
                autoplay
            >
                <source v-bind:src="src" v-bind:type="videoType"></source>
            </video>
        </div>

        <div id="time-range-container" v-if="showTimeRange">
            <input
                id="time-range"
                ref="timeRange"
                type="range"
                min="0"
                v-bind:max="duration"
                step="30"
                v-model:value="currentTime"
            />
        </div>
    </div>
</template>

<script>
    export default {
        props: [
            'drive',
            'filename',
            'mediaType',
        ],

        data() {
            return {
                currentTime: 0,
                duration: 0,
                loaded: false,
                showTimeRange: false,
                showVideoPlayer: false,
            }
        },

        computed: {
            src: function() {
                if(this.filename !== '') {
                    return
                        '/video/' + this.drive +
                        '/' + this.mediaType +
                        's/' + this.filename;
                }

                return '';
            },

            videoType: function() {
                var ext = this.filename.split('.')[1];
                var type = '';

                switch(ext) {
                    case 'mk4':
                    case 'm4v':
                        type = 'webm';
                        break;
                    case 'avi':
                        type = 'ogg';
                        break;
                    default:
                        type = ext;
                }

                return 'video/' + type;
            },
        },

        created() {
            Event.listen('displayVideoPlayer', this.display);
        },

        methods: {
            display: function() {
                if(this.src === '') {
                    return;
                }

                this.showVideoPlayer = true;
                this.loaded = false;

                var self = this;

                setTimeout(function() {
                    var interval = setInterval(function() {
                        var video = self.$refs.video;
                        if(video.readyState > 0) {
                            self.loaded = true;
                            self.duration = Math.round(video.duration);
                            self.currentTime = video.currentTime;
                            clearInterval(interval);
                        }
                    }, 500);
                }, 800);
            },

            togglePlay: function() {
                var video = this.$refs.video;

                if(video.paused) {
                    video.play();
                }

                if(!video.paused) {
                    video.pause();
                }
            },
        },
    }
</script>

When setVideo is called it should set the VideoPlayer component's props to the dummy values and then the video player should be displayed. But instead when the displayVideoPlayer event is fired, the component props are still set to their default values (empty strings). Most importantly, the src computed property is not being updated before the display method is called, so the display function immediately returns without doing anything. I've even tried making src part of the component's data and setting it in the display function with another function, setSrc. But the same thing happened. It's like my component's props and data aren't being updated, even though I can see with the dev tools that they are. It's like it's just not happening fast enough or something.

14 Mar
4 months ago

sraines left a reply on Sorting Based On Loaded Relationship

@COBS - Well I had originally tried this:

return
    DB::table('media')
        ->join('episodes', 'episodes.media_id', '=', 'media.id')
        ->select('media.*', DB::raw('max(episodes.created_at) as newest_episode'))
        ->where('media.media_type', "'show'")
        ->groupBy(
            'media.id',
            'media.media_type',
            'media.title',
            'media.summary',
            'media.notes',
            'media.poster',
            'media.year_start',
            'media.year_end',
            'media.created_at',
            'media.updated_at'
        )
        ->orderBy('newest_episode', 'desc')
        ->get();

I had to put every column in the group by even though I only really needed to group by id, because Laravel kept giving me a MySQL error. But I think when I tried this query I got no results. This is the raw SQL I came up with to get everything sorted the way I want:

select max(e.created_at) as 'newest_episode', m.*
from media m
left join episodes e on e.media_id = m.id
where
m.media_type = 'show'
group by m.id
order by newest_episode desc

I guess I could always just run that raw query.

sraines started a new conversation Sorting Based On Loaded Relationship

I have a model 'media' which is either a movie or a TV show. The MySQL for the table looks like this:

CREATE TABLE `media` (
    `id` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,
    `media_type` ENUM('movie','show') NOT NULL COLLATE 'utf8mb4_unicode_ci',
    `title` VARCHAR(191) NOT NULL COLLATE 'utf8mb4_unicode_ci',
    `summary` VARCHAR(4000) NOT NULL COLLATE 'utf8mb4_unicode_ci',
    `notes` VARCHAR(191) NULL DEFAULT NULL COLLATE 'utf8mb4_unicode_ci',
    `poster` VARCHAR(191) NOT NULL DEFAULT 'missing-poster.jpg' COLLATE 'utf8mb4_unicode_ci',
    `year_start` INT(10) UNSIGNED NULL DEFAULT NULL,
    `year_end` INT(10) UNSIGNED NULL DEFAULT NULL,
    `created_at` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
    `updated_at` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    PRIMARY KEY (`id`)
)
COLLATE='utf8mb4_unicode_ci'
ENGINE=InnoDB
AUTO_INCREMENT=173
;

I also have an 'episodes' model with a table that looks like this:

CREATE TABLE `episodes` (
    `id` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,
    `media_id` INT(10) UNSIGNED NOT NULL,
    `season` INT(10) UNSIGNED NOT NULL,
    `episode_number` INT(10) UNSIGNED NOT NULL,
    `title` VARCHAR(191) NOT NULL COLLATE 'utf8mb4_unicode_ci',
    `summary` VARCHAR(4000) NOT NULL COLLATE 'utf8mb4_unicode_ci',
    `created_at` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
    `updated_at` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    PRIMARY KEY (`id`),
    UNIQUE INDEX `episodes_media_id_season_episode_number_unique` (`media_id`, `season`, `episode_number`),
    CONSTRAINT `episodes_media_id_foreign` FOREIGN KEY (`media_id`) REFERENCES `media` (`id`) ON DELETE CASCADE
)
COLLATE='utf8mb4_unicode_ci'
ENGINE=InnoDB
AUTO_INCREMENT=616
;

I want to have a section on the site I'm making for new episodes. Basically it's just a list of all TV shows sorted in descending order by which has most recently had some episodes uploaded. My media model has a relationship for episodes like so:

public function episodes() {
        return $this->hasMany('App\Episode', 'media_id', 'id');
}

So how can I get an array of all TV shows sorted by the most recent created_at date of their attached episodes? I had tried this, thinking if I could sort the episodes then I'd be able to do something to sort the TV shows based on that, but I just get an empty array back.

return
            self::where('media_type', '=', '"show"')
                ->with('genres')
                ->with(['episodes' => function($query) {
                    $query->orderBy('created_at', 'desc');
                }])
                ->get();
18 Feb
5 months ago

sraines started a new conversation Algolia Instantsearch - Correlated Multi-index Search With Stats

I have a Laravel and Vue project I'm working on. I just used Laravel Scout to pull in Algolia, and I'm trying to use Algolia's vue InstantSearch on the front end. The way my database is setup, I have a movies table, and a shows table. I want to be able to search movies and TV shows at the same time, so I found this page in Algolia's docs.

This more or less works, my only problem is the results for movies and the results for shows are sort of separate. But now I'd like to use a stats widget to show the total number of results for movies + shows. But since the multi-index search uses two separate index widgets and the stats widget needs to go inside an index... well I'm not sure how to do that.

I'm starting to think my database structure is incorrect. Should movies and shows both go in the same table, like "media" or something? That seems like a bad idea to me but it feels like vue InstantSearch isn't really designed to do what I want to do.

15 Feb
5 months ago

sraines started a new conversation Back End Vs Front End Filtering

I have a small Laravel / Vue project I'm working on. It's a streaming site, like Netflix, that will be filled with content from my blu-ray and DVD collection. The 3 big tables I have in my MySQL database are the movies, shows, and episodes tables. The biggest table is episodes, with 649 records, with movies being the next largest table with 242 records. Shows is just a measly 22 rows. Those numbers will keep going up, but it will happen slowly as I've uploaded most of my collection. Also the data virtually never changes once it's in there. The only thing I might do is correct a typo in a movie or show title, or something like that.

With all that in mind, would it make sense to do any filtering for things like searching, or getting all content of a certain genre in the front end, instead of making very specific endpoints in the back end? Like right now I have an endpoint to get all recent movies, it takes a limit and an offset and it simply returns things from the movies table ordered by the created_at field in descending order. But it would be nice if I could simplify my web and API routes and do that work on the front end.

30 Jun
1 year ago

sraines started a new conversation Add Class To Component Siblings On Mouseover

Basically, I'm trying to achieve the functionality in this article http://eng.wealthfront.com/2015/06/30/implementing-netflix-redesign/

I have a component that represents a row of posters:

<template>
    <div class="container-fluid poster-row px-0 py-3 mb-3">
        <span class="left-arrow" v-on:click="slide('right')">&#8249;</span>
        <span class="right-arrow" v-on:click="slide('left')">&#8250;</span>
        <slot></slot>
    </div>
</template>

<script>
    export default {
        props: ['rowid'],
        methods: {
            slide(dir) {
                //
            }
        }
    }
</script>

And I have a component that represents a poster:

<template>
    <div class="poster-container d-inline-block">
        <img v-bind:src="'/img/posters/' + poster" class="img-fluid" />
        <div class="poster-overlay d-flex flex-column justify-content-around">
            <span class="mx-auto">{{ title }}</span>
            <a v-bind:href="'/theater/movie/' + mediaid" class="mx-auto play text-center">&#9658;</a>
            <button class="mx-auto btn btn-outline-success">More Info</button>
        </div>
    </div>
</template>

<script>
    export default {
        props: ['mediaid', 'title', 'poster']
    }
</script>

My implementation of these components in the HTML looks like this:

<div v-if="recent_movies.length > 0">
    <h4 class="h4 pl-4">Recent Movies</h4>
    <poster-row id="recent-movies" v-bind:rowid="'recent-movies'">
        <poster-container
                v-for="movie in recent_movies"
                v-bind:key="movie.id"
                v-bind:mediaid="movie.id"
                v-bind:title="movie.title"
                v-bind:poster="movie.poster">
        </poster-container>
    </poster-row>
</div>

I have some classes shift-left and shift-right like in the article. The poster-row component will have some ajax loaded posters appended to it as the user clicks the arrows to slide to the left or right, so I need to be able to apply the shift classes in a way that can deal with ajax loaded content. Can anyone help me figure out the best way to do this?

17 Jun
1 year ago

sraines left a reply on Get All From Table2 Where Table1.id = X In Join Table

That did the trick, thanks!

sraines started a new conversation Get All From Table2 Where Table1.id = X In Join Table

I'm working on rebuilding a personal streaming site I made as an excuse to learn Laravel. So let's say I have a few tables: movies, genres, and a join table, movie_genres. Here's how they're setup:

CREATE TABLE `movies` (
    `id` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,
    `title` VARCHAR(191) NOT NULL COLLATE 'utf8mb4_unicode_ci',
    `summary` TEXT NOT NULL COLLATE 'utf8mb4_unicode_ci',
    `notes` VARCHAR(191) NULL DEFAULT NULL COLLATE 'utf8mb4_unicode_ci',
    `year` INT(10) UNSIGNED NOT NULL DEFAULT '0',
    `poster` VARCHAR(191) NOT NULL DEFAULT 'missing-poster.jpg' COLLATE 'utf8mb4_unicode_ci',
    `video` VARCHAR(191) NOT NULL COLLATE 'utf8mb4_unicode_ci',
    `drive` VARCHAR(191) NOT NULL DEFAULT 'hdd1' COLLATE 'utf8mb4_unicode_ci',
    `created_at` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
    `updated_at` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    PRIMARY KEY (`id`)
)
COLLATE='utf8mb4_unicode_ci'
ENGINE=InnoDB
AUTO_INCREMENT=152
;

CREATE TABLE `genres` (
    `id` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,
    `name` VARCHAR(191) NOT NULL COLLATE 'utf8mb4_unicode_ci',
    `created_at` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
    `updated_at` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    PRIMARY KEY (`id`)
)
COLLATE='utf8mb4_unicode_ci'
ENGINE=InnoDB
AUTO_INCREMENT=26
;


CREATE TABLE `movie_genres` (
    `id` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,
    `movie_id` INT(10) UNSIGNED NOT NULL,
    `genre_id` INT(10) UNSIGNED NOT NULL,
    `created_at` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
    `updated_at` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    PRIMARY KEY (`id`),
    UNIQUE INDEX `movie_genres_movie_id_genre_id_unique` (`movie_id`, `genre_id`),
    INDEX `movie_genres_genre_id_foreign` (`genre_id`),
    CONSTRAINT `movie_genres_genre_id_foreign` FOREIGN KEY (`genre_id`) REFERENCES `genres` (`id`) ON DELETE CASCADE,
    CONSTRAINT `movie_genres_movie_id_foreign` FOREIGN KEY (`movie_id`) REFERENCES `movies` (`id`) ON DELETE CASCADE
)
COLLATE='utf8mb4_unicode_ci'
ENGINE=InnoDB
AUTO_INCREMENT=404
;

I want to be able to call a method on my Genre model called movies like so:

App\Genre::find($id)->movies()

This should return info from the movies table for all of the movies in movie_genres with a genre_id of $id. My movies method currently looks like this:

public function movies() {
    return $this->hasMany(MovieGenre::class);
}

But when returning from my API I get a JSON string that looks something like this:

[
    {
        "id": 232,
        "movie_id": 3,
        "genre_id": 13,
        "created_at": "2017-09-10 09:56:12",
        "updated_at": "2017-09-10 09:56:12"
    },
    {
        "id": 233,
        "movie_id": 9,
        "genre_id": 13,
        "created_at": "2017-09-10 11:04:05",
        "updated_at": "2017-09-10 11:04:05"
    },
    {
        "id": 234,
        "movie_id": 10,
        "genre_id": 13,
        "created_at": "2017-09-10 11:08:56",
        "updated_at": "2017-09-10 11:08:56"
    }
]

I don't want to get the data from movie_genres, I want to get the data from movies. Is there an easy Eloquent way to do this?