Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

poxin's avatar
Level 4

calebporzio/sushi API Model

I've been using this package frequently to create Models based on a remote API using the custom getRows method provided in the package.

The problem I'm trying to figure out is in the Model you have getRows(), which queries and API and returns the results. You can then use standard eloquent queries like 'first'. I need to pass a variable into this method so that the API can pull back different results based on its own parameters.

How can I set a variable in the Model to use in getRows() from an Eloquent query. Something like: MyModel::setVariable($var)->first();

Or something similar.. The issue is I need to set this variable before the model returns any results.

class MyModel extends Model
{
    use Sushi;

    public function getRows($var) // how can I set this?
    {
        $data = $api->query()->where(function ($query) use ($var) {
            return $query->where('id', 'eq', $var); // dynamic lookup based on variable
        })->get();

       
        return $data;
    }
0 likes
5 replies
bugsysha's avatar

Something like this should work.

class MyModel extends Model
{
    use Sushi;

	protected static $var;

    public static setVar($value)
    {
        self::$var = $value;
		return self::query();
    }

    public function getRows()
    {
        return $api->query()->where(function ($query) {
            return $query->where('id', 'eq', self::$var);
        })->get();
    }
3 likes
FlorianW's avatar

It works only for the first call, a second Call to this Sushi-Class doesnt work, because Sushi caches the first call

1 like
peydude's avatar

@FlorianW Have you tied adding a sushiShouldCache() method to your model?

protected function sushiShouldCache()
{
    return false;
}

It's in the sushi docs

v-tev's avatar

I am also experiencing the same issue. The second call on the sushi class isn't functioning as expected. Using the cache method doesn't resolve the problem. Have you found a solution?

luiz_p's avatar

@v-tev I just stumbled into this same problem. So I will put here my solution, I do not think is the best one available, but it solved for my necessity.

What it will do is to create a table for every $bar passed as argument when calling bar(). Something like bar('baz') will result into a table called foo_baz

1 like

Please or to participate in this conversation.