Error fetching data for a view [closed]

-2

I'm trying to bring data to a view to do some testing on Laravel but it's not working.

Controller

class ProfileController extends Controller
{
private $aluno;
private $request;



public function __construct(Aluno $aluno, Request $request)
{

    $this->aluno = $aluno;
    $this->request = $request;



}


   public function index()
    {
        $id = '39';


        $alunos = $this->aluno
            ->select('*')
            ->where('id', '=', $id)
            ->get();


        return view('profile.index', compact('alunos'));

    }

Model

<?php

namespace App\Models\Profile;

use Illuminate\Database\Eloquent\Model;



class Aluno extends Model
{
    protected $table = 'aluno';
}
    
asked by anonymous 12.05.2016 / 14:45

1 answer

2

In the view you should be trying to access the property directly $aluno->name , however the get() method returns a collection ( Illuminate\Database\Eloquent\Collection ).

To return a result only, you should use the method first or find .

Try this:

$aluno = $this->aluno->select('*')->where('id', '=', $id)->first();

Or simply:

$aluno = $this->aluno->find($id);
    
12.05.2016 / 19:24