PiotrG

PiotrG

Member Since 2 Years Ago

Experience Points
22,850
Total
Experience

2,150 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
223
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.

Level 5
22,850 XP
Jul
04
4 months ago
Activity icon

Replied to API Resource Related Model Filtering

@mstrauss Thank you for trying to help, but I've given up on this for now since I can't get it to work as expected.

I've gone ahead and made filters on my Events Endpoint and dropped the User related Events like so:

    public function index()
    {
        $events = QueryBuilder::for(Termin::class)
        ->allowedFilters([
            Filter::exact('category', 'TERMINE_CLASS'),
            Filter::exact('user', 'TERMINE_USER'),
            Filter::scope('starts_before'),
            Filter::scope('starts_after'),
            Filter::scope('starts_between')
        ])
        ->paginate(100);
        
        return TerminResource::collection($events);
    }   

Now I just pass the user and category in my frontend Vue Component and get the data I need.

Jul
03
4 months ago
Activity icon

Replied to API Resource Related Model Filtering

The top one I already had, and that doesn't work as it returns all the events.

The bottom one does the same...

Activity icon

Replied to API Resource Related Model Filtering

select * from [BENUTZER] where exists (select * from [TERMINE] where [BENUTZER].[BENUTZER] = [TERMINE].[TERMINE_USER] and [TERMINE].[TERMINE_CLASS] = ?)

Activity icon

Replied to API Resource Related Model Filtering

Not sure where I could tack that on...

        $user = QueryBuilder::for(Benutzer::class)
            ->allowedIncludes('events')
            ->allowedFilters(Filter::exact('events.category', 'events.TERMINE_CLASS'))
            ->findOrFail($benutzer->BENUTZER_KEY)->toSql();

        return $user;

doing this only shows "select * from [BENUTZER]" in postman

Activity icon

Replied to API Resource Related Model Filtering

@MSTRAUSS - Unfortunately not, if I remove the column alias, I have to use events.TERMINE_CLASS. But even then, I still get ALL the events back, not only the ones I filter by.

Activity icon

Replied to API Resource Related Model Filtering

I've added Spatie Query Builder and edited my show function in my BenutzerController to:

        $user = QueryBuilder::for(Benutzer::class)
            ->allowedIncludes('events')
            ->allowedFilters(Filter::exact('events.category', 'events.TERMINE_CLASS'))
            ->findOrFail($benutzer->BENUTZER_KEY);

        return new BenutzerResource($user);

Hitting endpoint /api/benutzer/7?include=events&filter[events.category]=Meeting

still returns ALL the events...

Activity icon

Started a new Conversation API Resource Related Model Filtering

Hello!

I am having trouble understanding how to filter a model resource relationship.

\Resources\User.php

<?php

namespace App\Http\Resources;

use App\Http\Resources\Termin as TerminResource;
use Illuminate\Http\Resources\Json\JsonResource;

class Benutzer extends JsonResource
{
    /**
     * Transform the resource into an array.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return array
     */
    public function toArray($request)
    {
        return [
            'id' => $this->BENUTZER_KEY,
            'name' => $this->PERSON,
            'email' => $this->EMAIL,
            'username' => $this->BENUTZER,
            'password' => bcrypt($this->PASSWORT),
            'is_admin' => $this->SYSOP ? "true" : "false",
            'events' => TerminResource::collection($this->events)
        ];
    }
}

\Resources\Termin.php

<?php

namespace App\Http\Resources;

use Carbon\Carbon;
use Illuminate\Http\Resources\Json\JsonResource;

class Termin extends JsonResource
{
    /**
     * Transform the resource into an array.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return array
     */
    public function toArray($request)
    {
        // return parent::toArray($request);

        $start_date = Carbon::parse($this->TERMINE_VON_DAT)->format('Y-m-d');
        $start_time = Carbon::parse($this->TERMINE_VON_ZEI)->format('H:i');
        $start = Carbon::create($start_date . $start_time);

        $end_date = Carbon::parse($this->TERMINE_BIS_DAT)->format('Y-m-d');
        $end_time = Carbon::parse($this->TERMINE_BIS_ZEI)->format('H:i');
        $end = Carbon::create($end_date . $end_time);

        $allday = ($start == $end ? true : false);

        return [
            'id' => $this->TERMINE_KEY,
            'user' => $this->TERMINE_USER,
            'title' => $this->TERMINE_BETREFF ? $this->TERMINE_BETREFF : 'Kein Betreff',
            'location' => $this->TERMINE_ORT ? $this->TERMINE_ORT : 'Kein Ort',
            'start' => $start,
            'end' => $end,
            'allDay' => $allday,
            'category' => $this->TERMINE_CLASS ? $this->TERMINE_CLASS : 'Keine Kategorie',
        ];
    }
}

\Controllers\Api\BenutzerController.php

<?php

namespace App\Http\Controllers\Api;

use App\Benutzer;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Http\Resources\Benutzer as BenutzerResource;
use App\Http\Resources\BenutzerCollection as BenutzerCollectionResource;

class BenutzerController extends Controller
{
    public function index()
    {
        return new BenutzerCollectionResource(Benutzer::paginate(100));
    }

    public function show(Benutzer $benutzer)
    {
        return new BenutzerResource($benutzer->with('events'));
    }
}

When accessing the

/api/benutzer/7

endpoint, I get the user and all their events.

Problem is that this returns ALL the events without any filtering or pagination and of course the query takes a very long time.

How would I go about adding filtering on the "events" relationship?

I would like to hit an endpoint like this /api/benutzer/7?include=events&filter[events.category]=Meeting