使用Eloquent ORM自定义方法链接?

This is my current query:

$cars = Cars::with('brand')->get();

$cars->map(function($cars){

    $cars->fullName = $cars->brand->brand." ".$cars->name;
    //other manipulation...
    return $cars;
});

I want to manipulate my collection in the model so that I can run something like $cars = Cars::with('brand')->getWithBrand();

How can I do this, so I don't have to write map functions for every time I run the query?

In your particular example, you don't need to use map to modify the Collection at all. You can use an Eloquent accessor to define attributes on a Model that don't exist in the database. In your example, you would define the following method on your Cars model:

public function getFullNameAttribute($value)
{
    // make sure brand exists first
    if ($this->brand) {
        return $this->brand->brand.' '.$this->name;
    }

    // default if brand doesn't exist
    return $this->name;
}

By defining that function on your Model, that function will be called whenever you attempt to use the full_name attribute, as shown in the following code:

$car = Cars::with('brand')->first();

// this will echo the result of the getFullNameAttribute method
echo $car->full_name;

Edit

If you would also like this new attribute to automatically show up in your toArray() or toJson() output, you can add the attribute to the $appends property on your Cars model:

class Cars extends Model
{
    protected $appends = ['full_name'];

    public function getFullNameAttribute($value)
    {
        // make sure brand exists first
        if ($this->brand) {
            return $this->brand->brand.' '.$this->name;
        }

        // default if brand doesn't exist
        return $this->name;
    }
}

Be aware, however, that your custom attribute depends on a related object. So, if you do something that accidentally calls toArray(), toJson(), __toString(), etc on a Collection of Cars that has not eager loaded the brand relationship, this will cause the N+1 query issue.

For example:

// Bad: N+1 issue because each printed Car will execute a
// separate query to get its brand to output full_name.
echo Cars::get();

// Good: No N+1 issue because all brands are already loaded.
echo Cars::with('brand')->get();