Skip to content

Method chaining indentation #20

Closed
Closed
@khalyomede

Description

@khalyomede

Currently I will write method chaining like this:

final class User
{
  public function scopeAuthor(Builder $query): Builder
  {
    return $query->whereIn("status", [
        User::STATUS_SUSPENDED, 
        User::STATUS_BANNED, 
      ])
      ->where("active", true) 
      ->whereHas("books");
  } 
} 

I know I was writing the methods chaining like this a while ago

final class User extends Model
{
  public function scopeAuthor(Builder $query): Builder
  {
    return $query->whereIn("status", [
      User::STATUS_SUSPENDED, 
      User::STATUS_BANNED, 
    ])
    ->where("active", true) 
    ->whereHas("books");
  } 
}

I've seen on Twitter people use a trick when the first method call is multiline

final class User
{
  public function scopeAuthor(Builder $query): Builder
  {
    return $query->new()
      ->whereIn("status", [
        User::STATUS_SUSPENDED, 
        User::STATUS_BANNED, 
      ])
      ->where("active", true) 
      ->whereHas("books");
  } 
} 

Maybe it's worth considering adding a word about it.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions