N
N
NubasLol2019-06-25 16:55:58
Laravel
NubasLol, 2019-06-25 16:55:58

Is it possible to create a custom link?

For example, instead of this, in the Order model

public function order
{
    return $this->belongsTo('App\Order');
}

Something like
public function order
{
    return self::query()->where('cost',  '>',  $this->cost);
}

The example is purely hypothetical. The task is to work with my request, as if with an attitude. Use with, withCount etc

Answer the question

In order to leave comments, you need to log in

2 answer(s)
N
NubasLol, 2019-06-26
@NubasLol

I solved the issue by implementing a custom method that adds a link to the collection of models.
Transmitted in a completely different way

public function relatedOrders()
    {
        return $this->hasMany(self::class, "parent_id", "id")->fromHighPriceOrders();
    }

And everything will work great. Through subqueries for each order, we find the id of orders with a higher price, and make joion of such orders. Then we bind) surprisingly fast

V
Vladislav Nagorny, 2019-06-25
@esvils

I think I understand...
Reading about scope's

public function scopeCost($query,$value)
    {
        return $query->where('cost', '>', $value);
    }

$data = Order::cost(100)->get();

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question