staudenmeir

staudenmeir

Member Since 2 Years Ago

Munich, Germany

Experience Points 91,500
Experience Level 19

3,500 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 151
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.

22 Jun
3 days ago

staudenmeir left a reply on Laravel Dusk Error On Type

Please post the whole stacktrace.

20 Jun
5 days ago

staudenmeir left a reply on SQLSTATE[42S22]: Column Not Found: 1054 Unknown Column 'issuer' In 'field List'

You can't provide the whole $player object, only its id:

$player->warnings()->create(
    [ 'message' => $faker->text, 'issuer_id' => $player->id ]
);
19 Jun
6 days ago

staudenmeir left a reply on Counting Grandchildren

You can use HasManyThroughby manually adding thelikeable_type` constraint:

class User extends Model
{
    public function forumLikes(): HasManyThrough
    {
        return $this->hasManyThrough('App\Forum\ForumLike', 'App\Forum\ForumThread', null, 'likeable_id')
            ->where('likeable_type', 'App\Forum\ForumThread');
    }
}

User::withCount('forumLikes')->get();

staudenmeir left a reply on Help Me Understand The Problem With This HasOneThrough Relationship!

The inverse of HasOneThrough is not HasOneThrough but two BelongsTo relationships.

You can use HasOneThrough by swapping foreign and local keys:

public function school()
{
    return $this->hasOneThrough('App\School', 'App\Student', 'id', 'id', 'student_id', 'school_id');
}
16 Jun
1 week ago

staudenmeir left a reply on Laravel One-to-many On PostgreSQL Stops Working

Have a logged the executed queries?

14 Jun
1 week ago

staudenmeir left a reply on Foreign Keys Are Dropping Leading Zeros In Relationships

What's your Laravel version? Can you log the executed queries?

staudenmeir left a reply on Setting Up Dusk With Testing Database

What exact version of Laravel are you using?

11 Jun
2 weeks ago

staudenmeir left a reply on Using `whereHas` Across Two Databases

Laravel has no native support for this.

You can use a package: https://github.com/hoyvoy/laravel-cross-database-subqueries

08 Jun
2 weeks ago

staudenmeir left a reply on Performant Way To Mass "update Or Create"

I've created an INSERT & UPDATE (UPSERT) package for all databases: https://github.com/staudenmeir/laravel-upsert

05 Jun
2 weeks ago

staudenmeir left a reply on Multiple Model Updating

Your raw expressions use backticks to quote column names, but that's the MySQL syntax.

The database your using (PostgreSQL/SQLite) requires double quotes:

$neighbours->update([ 'left' => DB::raw('"left"+2'), 'right' => DB::raw('"right"+2'), ]);

03 Jun
3 weeks ago

staudenmeir left a reply on Conditional INSERT

You can remove the FROM clause like this:

$select = static::query()->from(null)

staudenmeir left a reply on Conditional INSERT

You can use selectRaw():

->selectRaw('?, ?, ?', [$id, $bidId, $bid]);

Does the table actually contain data that matches your constraints? What's the result of dd($select->get())?

staudenmeir left a reply on Conditional INSERT

What exactly isn't working?

01 Jun
3 weeks ago

staudenmeir left a reply on Get Latest Value(s) From Every Device Within Tabe

I know these methods by heart, so I'm not sure whether they are all mentioned in the docs.

31 May
3 weeks ago

staudenmeir left a reply on Get Latest Value(s) From Every Device Within Tabe

whereHas() is for relationships. You need to use whereIn() and a raw expression:

$getsensordata = \App\Sensordata::whereIn('id', function ($query)  {
    $query->selectRaw('max(id)')->from('Sensordata')->groupBy('sensormac');
})->get();

staudenmeir left a reply on Colleges - Do They Get Loaded In Memory At Once?

Yes, all replies get loaded into the memory. What would you expect to happen?

staudenmeir left a reply on Get Latest Value(s) From Every Device Within Tabe

What database and version are you using?

25 May
1 month ago

staudenmeir left a reply on Laravel HasOne() Relationship

Did you install the package?

staudenmeir left a reply on Laravel HasOne() Relationship

What does "doesn't work" mean? Is there an error or are the queries the same as before?

Did you change both models?

staudenmeir left a reply on Laravel HasOne() Relationship

Can you post the whole code of both models?

staudenmeir left a reply on Laravel HasOne() Relationship

What are the executed queries?

24 May
1 month ago

staudenmeir left a reply on Laravel HasOne() Relationship

There is no native support for this in Laravel, as the required query is quite complex.

I've created a package for this: https://github.com/staudenmeir/eloquent-eager-limit

Use the HasEagerLimit trait in both the parent and the related model and apply limit(1) to your relationship:

class MessageType extends Model { use \Staudenmeir\EloquentEagerLimit\HasEagerLimit;

public function lastMessage()
{
    return $this->hasOne('App\Message')->latest()->limit(1);
}

}

class Message extends Model { use \Staudenmeir\EloquentEagerLimit\HasEagerLimit; }

staudenmeir left a reply on WithCount() / LoadCount() Problem

Use a closure:

Route::bind('userOrder', function ($value) {
    return \App\Order::where('buyer_id', \Auth::id())
        ->where('id', $value)
        ->with([
            'seller' => function ($query) {
                $query->withCount('salesRelevant');
            },
            'product.sub_category.category:id,slug', 'giftCode'
        ])->firstOrFail();
});
20 May
1 month ago

staudenmeir left a reply on How To Delete More Than One Item?

destroy() accepts an array:

Model::destroy($ids);

staudenmeir left a reply on N-level Category (self Reference Relation)

Make the relationship recursive:

public function children() {
    return $this->hasMany(Category::class, 'parent_id')->with('children');
}
17 May
1 month ago

staudenmeir left a reply on Issue With Eloquent: API Resources - Conditional Relationships

What's the result of dd($this->getRelations()); in UserResource::toArray()?

16 May
1 month ago

staudenmeir left a reply on How Do I Create A Custom Relationship Model?

The relationship I posted is a combination of two many-to-many relationships.

With the actual model, table and column names:

class Property extends Model
{
    use \Staudenmeir\EloquentHasManyDeep\HasRelationships;

    public function amenities()
    {
        return $this->hasManyDeep(
            Amenity::class,
            ['property-room-type', RoomType::class, 'amenity-room-type'],
            [null, 'id', 'room-type_id', 'id'],
            [null, 'room-type_id', null, 'amenity_id']
        );
    }
}
14 May
1 month ago

staudenmeir left a reply on Get Deep Nested Relationship, With Many Levels

Relationships can be recursive:

public function replies()
{
    return $this->hasMany('App\Comment', 'parent_id', 'id')->with('replies');
}

Comment::where('_id', $comment_id)->with('replies')->get();
13 May
1 month ago
12 May
1 month ago

staudenmeir left a reply on Get All Relations Of A M To N - Before Sending To The View

Use load():

$dispatches = $sub_jobs->load('dispatches')
    ->pluck('dispatches')->flatten();

staudenmeir left a reply on Get All Relations Of A M To N - Before Sending To The View

Use eager loading and the Collection methods pluck() and flatten():

$dispatches = SubJob::with('dispatches')->get()->pluck('dispatches')->flatten();
10 May
1 month ago

staudenmeir left a reply on Multiple Levels Down

Please mark the answer as accepted/the best reply (not sure what Laracasts calls it).

staudenmeir left a reply on Multiple Levels Down

Use this:

class Customers extends Model
{
    public function products()
    {
        return $this->belongsToMany(Product::class, 'customer_products', 'customer', 'product');
    }
}

$products = Customer::find(1)->products;
09 May
1 month ago

staudenmeir left a reply on How Do I Create A Custom Relationship Model?

I've created a package for cases like this: https://github.com/staudenmeir/eloquent-has-many-deep

Depending on your model names and database schema, the relationship would look like this:

class Property extends Model
{
    use \Staudenmeir\EloquentHasManyDeep\HasRelationships;

    public function amenities()
    {
        return $this->hasManyDeep(Amenities::class, ['property_room_type', RoomType::class, 'amenities _room_type']);
    }
}
08 May
1 month ago

staudenmeir left a reply on Determining If Multi-level Relationship Records Exist

there are no shows, it's going to throw a PHP notice that there's no "shows" member variable

This method works, have you tried it? The only downside is that it unnecessarily fetches all the user's shows.

07 May
1 month ago

staudenmeir left a reply on Why Don'y Work Relationships?

Did you set the $primaryKey to category_id/language_id in the two models?

02 May
1 month ago

staudenmeir left a reply on How To Get Only One Row Of One To Many With Condition?

What's the result of dd($products->pluck('latestInventory.price'));?

staudenmeir left a reply on How To Get Only One Row Of One To Many With Condition?

You can use a HasOne relationship:

class Product extends Model
{
    public function latestInventory()
    {
        return $this->hasOne(Inventory::class)->orderBy('expiration_date', 'desc');
    }
}

You can't sort the query, only the results:

$products = Product::with(['latestInventory' => function($query) {
    $query->where('count', '>', 0)->orderBy('id');
}])->get();

$products = $products->sortBy('latestInventory.price');
28 Apr
1 month ago

staudenmeir left a reply on Eloquent Find() And Count()

Model::find(id)->count() fetches a single instance and then executes a completely new query that counts all rows in the table.

It's equivalent to Model::find(id)->newQuery()->count(),Model::count() or (new Model)->count().

27 Apr
1 month ago

staudenmeir left a reply on Querying Relations ( 1 To M , M To N)

Laravel has no native support for a direct relationship.

I've created a package for cases like this: https://github.com/staudenmeir/eloquent-has-many-deep

If you are using the default naming conventions:

class Job extends Model
{
    use \Staudenmeir\EloquentHasManyDeep\HasRelationships;

    public function dispatches()
    {
        return $this->hasManyDeep(Dispatch::class, [Sub_Job::class, 'dispatch_sub_job']);
    }
}
25 Apr
2 months ago

staudenmeir left a reply on "SQLSTATE[23000]: Integrity Constraint Violation: 1452 Cannot Add Or Update A Child Row

Does the data_text table already contain data when you add the foreign key?

22 Apr
2 months ago

staudenmeir left a reply on Call To A Member Function AddEagerConstraints() On Integer With Scope Function

Your code doesn't work because sum() already executes the query.

Use withCount() instead:

public function scopeWithSum ($query)
{
    return $query->withCount(['relationship as column_sum' => function ($query) {
        $query->select(DB::raw('sum(column)'));
    }]);
}