Quadram

Quadram

Member Since 1 Year Ago

Experience Points 5,520
Experience Level 2

4,480 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 53
Lessons
Completed
Best Reply Awards 0
Best Reply
Awards
  • Start Your Engines Achievement

    Start Your Engines

    Earned once you have completed your first Laracasts lesson.

  • First Thousand Achievement

    First Thousand

    Earned once you have earned your first 1000 experience points.

  • One Year Member Achievement

    One Year Member

    Earned when you have been with Laracasts for 1 year.

  • Two Year Member Achievement

    Two Year Member

    Earned when you have been with Laracasts for 2 years.

  • Three Year Member Achievement

    Three Year Member

    Earned when you have been with Laracasts for 3 years.

  • Four Year Member Achievement

    Four Year Member

    Earned when you have been with Laracasts for 4 years.

  • Five Year Member Achievement

    Five Year Member

    Earned when you have been with Laracasts for 5 years.

  • School In Session Achievement

    School In Session

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

  • Welcome To The Community Achievement

    Welcome To The Community

    Earned after your first post on the Laracasts forum.

  • Full Time Learner Achievement

    Full Time Learner

    Earned once 100 Laracasts lessons have been completed.

  • Pay It Forward Achievement

    Pay It Forward

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

  • Subscriber Achievement

    Subscriber

    Earned if you are a paying Laracasts subscriber.

  • Lifer Achievement

    Lifer

    Earned if you have a lifetime subscription to Laracasts.

  • Laracasts Evangelist Achievement

    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 Achievement

    Chatty Cathy

    Earned once you have achieved 500 forum replies.

  • Laracasts Veteran Achievement

    Laracasts Veteran

    Earned once your experience points passes 100,000.

  • Ten Thousand Strong Achievement

    Ten Thousand Strong

    Earned once your experience points hits 10,000.

  • Laracasts Master Achievement

    Laracasts Master

    Earned once 1000 Laracasts lessons have been completed.

  • Laracasts Tutor Achievement

    Laracasts Tutor

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

  • Laracasts Sensei Achievement

    Laracasts Sensei

    Earned once your experience points passes 1 million.

  • Top 50 Achievement

    Top 50

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

30 Jul
9 months ago

Quadram started a new conversation Using A Function In V-model To Bring Old Values To An Edit Form

I'm trying the following in VueJS:

I have an edit form to edit something (in this case a Beer) in different languages:

UpdateProfile.vue

<!--Beer Copy-->
<div class="field">
    <label class="label">{{data.name}}</label>
</div>

<!-- languages -->
<div v-for="language in languages" class="field has-addons">
    <p class="control">
        <a class="button is-static">{{language}}</a>
    </p>
    <p class="control is-expanded">
        <input class="input" type="text" v-model="getBeerTranslated[language]">
    </p>
</div>

So, for example, if Spanish and English languages are chosen I would like to have:

spanish|Cerveza Artesanal

english|Craft Beer

that returns me the values of that beer in different languages, and then I would edit it (the model is called Copy.php)

But now I'm having: spanish|(nothing)... (and so on with the rest of the languages).

I'm trying to build a function (getBeerTranslated) that for a given language (for example, Spanish) will return the beer translated to the input value.

inside methods() I wrote:

 getBeerTranslated(language){
        this.api.get('/translated/' + data.id, language).then(//what should I have here?);
    },

where I define the routes I have:

Route::get('/translated/{beer}', '[email protected]');

and in my BeersController:

public function getBeerTranslated(Beer $beer, $language) { $beerInThatLanguage = Copy::where('key', $beer->getOriginal('name'))->value($language);

return $beerInThatLanguage;

}

I don't how to make my function works in Vue.js ,

Would you be so kind to give me some tips?

Thank you!!

26 Jun
11 months ago

Quadram started a new conversation "is My Pub Currently Open?". Problems Using Conditions In A Query Statement In Laravel

Hi there!!

I'm trying the following:

I have two models: Pubs and Schedules (where I store statically the pubs schedules). They 're related one to each other as follows:

Pub:

/**

  • @return \Illuminate\Database\Eloquent\Relations\HasMany */ public function pubSchedules() { return $this->hasMany( Schedule::class ); }

Schedule:

/**

  • @return \Illuminate\Database\Eloquent\Relations\BelongsTo */ public function pub() { return $this->belongsTo(Pub::class); }

Table schedules has the following fields:

id| pub_id | week_day | opening_time | closing_time

I'm developing a filter that shows only open pubs in current moment.

The first function I made works perfectly:

public static function isPubCurrentlyOpen($pubs) {

$pubs->whereHas('pubSchedules', function ($pubs) {
    $pubs->where('week_day', Carbon::now()->dayOfWeek)
        ->whereRaw(
            "'" . Carbon::now('Europe/Madrid')->format("H:i:s") .
                "' BETWEEN opening_time AND closing_time

                "
        );
} );

}

It returns a query that can join with other filters.

The problem is when I have a pub with this kind of schedule: 09:00 - 03:00 in the same day. Of course 03:00 is in the following day, but we chose this possibility to make easier for the client to store the schedules.

Well, in this example it makes that "Carbon::now('Europe/Madrid')->format("H:i:s")" is not between opening_time and closing_time, so this pub it's not going to appear as opened (when it is).

Making some tests on MySql I tried something like this:

SELECT *, CONCAT(YEAR(NOW()), '-', MONTH(NOW()), '-', week_day, ' ', opening_time) AS openingDateTime, IF(closing_time < opening_time, CONCAT(YEAR(NOW()), '-', MONTH(NOW()), '-', week_day + 1, ' ', closing_time), CONCAT(YEAR(NOW()), '-', MONTH(NOW()), '-', week_day, ' ', closing_time) ) AS closingDateTime FROM tappear_borrador.schedules ;

It works perfectly setting "one more day" to the closing_time when it's greater than 00:00, but I'm having problems to set something like thar in my function:

$pubs->whereHas('pubSchedules', function ($pubs) { $pubs->where('week_day', Carbon::now()->dayOfWeek) ->whereRaw( "'" . Carbon::now('Europe/Madrid')->format('Y-m-d H:i:s') . "' BETWEEN CONCAT(YEAR(NOW()), '-', MONTH(NOW()), '-', ".Carbon::now()->dayOfWeek.", ' ', opening_time) AND IF(closing_time < opening_time, CONCAT(YEAR(NOW()), '-', MONTH(NOW()), '-', ".Carbon::now()->dayOfWeek." + 1, ' ', closing_time), CONCAT(YEAR(NOW()), '-', MONTH(NOW()), '-', ".Carbon::now()->dayOfWeek.", ' ', closing_time) )

                "
        );
} );

This I made it's not returning anything (any error too). ((I also checked that json_die(Carbon::now()->dayOfWeek+1) doesn't return one more value to current day...may it be one of the problems?)),

Check that changed current moment to compare years/month/h:i:s with opening and closing times.

So friends: I need your help to improve my last code to make if closing_time < opening_time, closing_time will be in the following day and then compare if current moment is/not between.

I'd like to make something like this:

public static function isPubCurrentlyOpen($pubs) {

$pubs->whereHas('pubSchedules', function ($pubs) {
    $pubs->where('week_day', Carbon::now()->dayOfWeek)
        ->whereRaw(
            "'" . Carbon::now('Europe/Madrid')->format("H:i:s") .
                "' BETWEEN openingDateTime AND closingDateTime
                "
        );
} );

}

where:

openingDateTime = " CONCAT(YEAR(NOW()), '-', MONTH(NOW()), '-', week_day, ' ', opening_time) ";

closingDateTime = "IF(closing_time < opening_time, CONCAT(YEAR(NOW()), '-', MONTH(NOW()), '-', week_day + 1, ' ', closing_time), CONCAT(YEAR(NOW()), '-', MONTH(NOW()), '-', week_day, ' ', closing_time) )" ;

Any ideas? Thanks a lot!!

18 May
1 year ago

Quadram started a new conversation How Not To Use DB In The Following Query Having A Subquery

I'm having the following function to calculate distances between two points using Haversine formula:

public static function getByDistance($distance) { $pubsFiltered = DB::select( 'SELECT * FROM (SELECT *, (6371 * acos(cos(radians(40.4169473)) * cos(radians(latitude)) * cos(radians(longitude) - radians(-3.7035285)) + sin(radians(40.4169473)) * sin(radians(latitude)))) AS distance FROM pubs) AS distances WHERE distance < ' . $distance . ' ORDER BY distance ; ');

return $pubsFiltered;

} This is returning an array instead of a collection, that is what I need.

If I change DB to Pub it returns an error because of the subquery where I calculate "distance" using my table "pubs".

How can I change the whole function or simply the subquery to have an instance of my Pub model?...Do I have to use set/declare variables in mysql?

Thanks a lot!!

16 May
1 year ago

Quadram left a reply on Querying Relationship Existence, How To Add Conditions Depending On An Array Length?

I know what you mean, but may I didn't explain right when I made the question: I would like to get an item or more (a pub/pubs) that has only those characteristics (tapps) I choose.

Of course I've tested with whereIn, but result was lots of pubs that has at least one of the tapps selected, but not all and I wanted them to satisfy all.

The only way I found was doing queries from results of last queries.

Testing using my solution I have only two pubs (what I wanted), but with whereIn I had more than ten.

Thanks a lot for trying to help me ;)

Quadram left a reply on Querying Relationship Existence, How To Add Conditions Depending On An Array Length?

Finally it worked for me:

public function pubsFilteredByTapps(FilteredTappsPubsRequest $request) { $tapps_chosen = $request->get('tapps'); $tapps_chosen = is_string($tapps_chosen) ? explode(',', str_replace('"', '', $tapps_chosen)) : $tapps_chosen;

    $query = Pub::query();

    foreach ($tapps_chosen as $tappId) {
        $query->whereHas('pubTapps', function($query) use($tappId) {
            $query->where('tapp_id', $tappId);
        });
    }

    return $query->get();
}
14 May
1 year ago

Quadram started a new conversation Querying Relationship Existence, How To Add Conditions Depending On An Array Length?

I would like to filter an item (pubs, in this case) by some characteristics that are stored in a separate table (tapps, in this case), and both are related by pub_tapps.

I have the following tables: pubs, tapps, pub_tapps(pub_id, tapp_id)

The relation between Pub and Tapp is the following:

public function pubTapps() { return $this->belongsToMany(Tapp::class, 'pub_tapps'); }

in my Pub model I tried the following testing for an array $request=[5,8, 7]:

public function pubsFilteredByTapps(FilteredTappsPubsRequest $request) { $tapps_chosen = $request->get('tapps');

$tapps_chosen = is_string($tapps_chosen) ? explode(',', str_replace('"', '', $tapps_chosen)) : $tapps_chosen;

return Pub::whereHas('pubTapps', function($query) use($tapps_chosen) {
    $query->where('tapp_id', $tapps_chosen[0]);
})
    ->whereHas('pubTapps', function($query) use($tapps_chosen) {
        $query->where('tapp_id', $tapps_chosen[1]);
    })
    ->whereHas('pubTapps', function($query) use($tapps_chosen) {
        $query->where('tapp_id', $tapps_chosen[2]);
    })
    ->get();

}

This is working perfectly, but for a given 3 dimensional array...

How can I do for a given array of an n length??

->whereHas('pubTapps', function($query) use($tapps_chosen) { $query->where('tapp_id', $tapps_chosen[n-1]); })

I tried this, but doesn't work at all (returns an empty array):

return $pubs = Pub::whereHas('pubTapps', function ($query) use ($tapps_chosen) {

    foreach ($tapps_chosen as $tappId) {
        $query->where('tapp_id', $tappId);
  }
   })->get();

What would I have to do??? Any ideas to make it work??

Thanks a lot!