Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Invalid argument supplied for foreach() #1

Closed
ergonomicus opened this issue Jan 2, 2018 · 4 comments
Closed

Invalid argument supplied for foreach() #1

ergonomicus opened this issue Jan 2, 2018 · 4 comments

Comments

@ergonomicus
Copy link

I have followed the instructions on how to install this package, but I get "Invalid argument supplied for foreach()" error in validateJoinQuery($relatedModel) method call. Any idea?

@fico7489
Copy link
Owner

fico7489 commented Jan 2, 2018

@ergonomicus can you paste your relation functions in models on which you are performing queries, table names of models, and query code?

@ergonomicus
Copy link
Author

@fico7489 I can paste only parts of code, it's an internal app...

Here are my models

// Event model
use Illuminate\Database\Eloquent\Model;
use App\Models\Work;
use Fico7489\Laravel\EloquentJoin\Traits\EloquentJoinTrait;

class Event extends Model
{
    // table 'events'
    use EloquentJoinTrait;

    public function work()
    {
        return $this->belongsTo(Work::class);
    }

    // bunch of other relations and accessors...
}

// Work model
use Illuminate\Database\Eloquent\Model;
use App\Models\Event;

class Work extends Model
{
    // table 'works'

    public function events()
    {
        return $this->hasMany(Event::class);
    }

    // bunch of other relations and accessors...
}

Here is my controller method

    public function index()
    {
        // works table has a boolean field 'is_archived' and datetime field 'start'
        $events = Event::with('eventStatus', 'work.customer.address')->orderByJoin('work.is_archived')->orderBy('start')->get();
        
        return view('events.index', compact('events'));
    }

Laravel 5.5.latest and PHP 7.1.12

Thanks.

@fico7489
Copy link
Owner

fico7489 commented Jan 2, 2018

both models should use EloquentJoinTrait so you need to user EloquentJoinTrait in Work model also, but a better solution would be to create BaseModel model which extends Model, use EloquentJoinTrait in BaseModel and then both models can extend BaseModel, not Model.

PS. I will update the docs so this will be more clear...

@ergonomicus
Copy link
Author

@fico7489 Ok, thank you. I will try again...

fico7489 pushed a commit that referenced this issue Sep 5, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants