user860511
user860511

Reputation:

Laravel - Get data from pivot table only if it appears in there

A criteria belongsToMany Alerts, and vice versa.

They are connected through a pivot table - alert_criteria. But, a criteria and alert doesn't have to be in the pivot table. They are added if other criteria are met.

The columns are:

alert_id and criteria_id.

Currently, this gets all criteria related to the user:

public function getIndex()
    {
        $alerts = Criteria::with('coordinate', 'alerts', 'alertsCount', 'bedrooms')
        ->where('user_id', '=', Auth::user()->id)
        ->get();

        $this->layout->content = View::make('users.alert.index', 
            array('alerts' => $alerts));
    }

What I want to do is

get all criteria relating to the user that appears in the pivot table of alerts_criteria.

Any help would be hugely appreciated.

Upvotes: 1

Views: 213

Answers (2)

Joe
Joe

Reputation: 15802

I believe you actually want something like this:

Auth::user() // the current user...
    ->criteria() // get any related criteria
    ->has( 'alerts' ) // which have alerts
    ->get();

Or if your User isn't related to Criteria:

Criteria::whereUserId( Auth::id() ) // Criteria where user_id = Auth::id()
        ->has( 'alerts' ) // which have alerts
        ->get();

Upvotes: 1

user860511
user860511

Reputation:

I solved this by using:

Criteria::find(Auth::user()->id)
->alerts()
->get();

Upvotes: 0

Related Questions