Ad

Syntax Error, Unexpected '->' Laravel Eloquent

- 1 answer

I have a:

$questions = Question::where('isenabled', '=', 'Yes')->orderBy('sortorder', 'asc')->get();

$mutuals = Mutual::all();

foreach ($questions as $key => $value) 
{
    $mutuals = Mutual::where('question_id_1', '=', $value->id)->orWhere('question_id_2', '=', $value->id)->first();
    if(count($mutuals) >= 1)
    {
        echo "Matched->";
        $filtered = $questions->reject(function ($questions) use ($value->id) {
            return $questions->id ==  $value->id;
        });
    }

The error is on the line of $questions->reject . Am I doing wrong? I'm just following

https://laravel.com/docs/5.1/eloquent-collections

Basically that will remove a certain element in the collection/array based on $questions->id

Ad

Answer

The problem seems to be with this line

$filtered = $questions->reject(function ($questions) use ($value->id) {

which results in

Parse error: syntax error, unexpected '->' (T_OBJECT_OPERATOR), expecting ',' or ')'

if you change it to

$filtered = $questions->reject(function ($questions) use ($value) {

it should work with what I'm seeing via syntax check.

Seems your wanting to access the object properties anyway so this should be fine in your case.

Ad
source: stackoverflow.com
Ad