mySun
mySun

Reputation: 1706

Undefined offset: 0 in laravel

My framework is Laravel 5.2, There is no record in the database. But in this Site, it has error.

Error is:

ErrorException in Collection.php line 1187:
Undefined offset: 0

Controller is:

public function index()
{
    $comment = ProviderComment::GetComments($ID);

    return $comment;
}

Model is:

public function scopeGetComments($query, $vendorID)
{
    $join = $query
        -> join('couples', 'couples.id', '=', 'provider_comment.couple_id')
        -> where('provider_comment.vendor_id', '=', $vendorID)
        -> get();
    return $join;
}

Where is my problem?

Upvotes: 3

Views: 9074

Answers (1)

jeanj
jeanj

Reputation: 2176

Controller:

public function index() {
    $comment = ProviderComment::getComments($ID)->get();
    return $comment; }

Model:

public function scopeGetComments($query, $vendorID)
{
    return $query
        ->join('couples', 'couples.id', '=', 'provider_comment.couple_id')
        ->where('provider_comment.vendor_id', '=', $vendorID);
}

Don't use get() directly on scopes.

More about scopes

Upvotes: 3

Related Questions