List results in laravel 5.4

1

Could you help me with this case? I saw that this is done but I get the error that I attached so I do not know what I'm doing wrong /:

public function  index()
{

    // Inicializa @rownum

    DB::statement(DB::raw('SET @rownum = 0'));

    // Realiza la consulta

    $tops = DB::table('articles')
            ->select(DB::raw('id','head','description', '@rownum := @rownum + 1 as rownum'))
            ->where('approve', '=', 'Aprobado')
            ->where('important','Destacado')
            ->orderBy('id', 'DESC')
            ->paginate(5);
    return view('index',compact('tops'));
}

Now I have the problem when executing the query, this says to me:

  

Undefined property: stdClass :: $ head (View:   C: \ xampp \ htdocs \ IndieSonico \ resources \ views \ index.blade.php)

This way I am calling you in my view:

@foreach($tops as $top)


    <div id="tops0" class="card" style="border:none !important; margin: 0;">
        <a href="{{ route('show',$top->id.$top->head) }}">
            <img  class="card-img-top" src="../images/{{$top->path}}" alt="Card image cap" style="height: 112px">
        </a>
        <a href="{{ route('show',$top->id.$top->head) }}" class="a-corregido2">
            <span id="">{{$top->rownum}}</span><span class="title-tops">{{ $top ->head }}</span>
        </a>
    </div>


@endforeach

    
asked by jorge luis vazquez martinez 22.05.2018 в 04:13
source

1 answer

0

Although the answer is already given in the comments, I will put it here so that other people can see more easily.

The query should be:

->select('id','head','description',DB::raw('@rownum := @rownum + 1 as rownum'))

Since, as it is in the problem, the error is made of entering the columns to be displayed within DB::raw()

    
answered by 22.05.2018 / 06:03
source