Error picking up data for a view

Asked

Viewed 101 times

-2

I’m trying to bring the data to a view to perform some tests 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';
}
  • You have a variable $alunos and in the Compact is only aluno, what’s the mistake?

  • Excuse was typo error the error that brings is this Undefined Property: Illuminate Database Eloquent Collection::$name (View: C: xampp htdocs course Resources profile index.blade.php)

  • Put the model in

  • I put the Model

1 answer

2


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

To return only one result, you must use the method first or find.

Try this:

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

Or simply:

$aluno = $this->aluno->find($id);
  • It worked Rafael Thanks the second option I had done this way because I think more practical but I put an id of a student and in the View brought another student

Browser other questions tagged

You are not signed in. Login or sign up in order to post.