Laravel 5.1 ERROR: Fatalerrorexception in Model.php line 852: Class 'App Vendor' not found

Asked

Viewed 85 times

1

good afternoon!

I am trying to relate two tables: Services and Suppliers, follow the templates created:

Model Servico

    use Illuminate\Database\Eloquent\Model;

    class Servico extends Model
    {
        protected $table = 'servicos';

        protected $fillable = [
            'nomeServico',
            'id_fornecedor'
        ];

        public function fornecedores(){
            return $this->belongsTo("App\Fornecedor");
        }
    }

Model Fornecedor

    use Illuminate\Database\Eloquent\Model;

    class Fornecedor extends Model
    {
        protected $table = 'fornecedores';

        protected $fillable = [
            'id',
            'nomeFornecedor'
        ];

        public function servicos(){
            return $this->hasMany('App\Servico');
        }

    }

In this way I am trying to perform the following procedure within the Servicocontroller:

    public function view($id){
            $servico = Servico::find($id);
            return view('servico.view',['servico'=>$servico]);
    }

In the view I am using the following code:

    @extends('layout.principal')
    @section('conteudo')
    <h1>Id Serviço #{{$servico->id}}</h1>
    Serviço: {{$servico->nomeServico}}<br/>
    Fornecedor: {{$servico->fornecedores->nomeFornecedor}}<br/>
    @stop

and I have the following mistake:

    FatalErrorException in Model.php line 852: Class 'App\Fornecedor' not found

In this error screen the ID and Servic data are displayed correctly, however the above error appears in the vendor name display.

Someone can give me a light??

  • Tried to take the App\ ?

  • Yes, I tried to take the App, but also it was not expensive... so I have researched the problem is related to namespace, but still do not know how to solve ....

  • Do you have a file called FornecedorController ?

  • Yes, but there is still no method because I am implementing Servicocontroller first.

  • Try putting in the Model Servico.php, a namespace so there at the beginning of the code: use App\Fornecedor;. Missing a semicolon...

  • nothing either... I also tried to change my namespace with the php artisan app:name protocolo and put in Model Servico use protocolo\Fornecedor. I tried everything here and nothing rs. I believe it is a configuration in the Laravel or a very rough thing

  • I changed the return of the Model Service to return $this->belongsTo('protocolo\Fornecedor'); now the error is another: ;Trying to get property of non-object (View: C:\xampp\htdocs\protocolo\resources\views\servico\view.blade.php)

  • It’s because on that line: return view('servico.view',['servico'=>$servico]);, you have to put so: return view('servico.view')->withServico($servico);

Show 3 more comments

1 answer

0


@Junior Morais

Make sure that Models are at the root of the "app" folder then just put

namespace App;

On Models Service and Supplier before the line:

use Illuminate\Database\Eloquent\Model;

Detail: If your models are in a folder other than the "app" this will have to be adjusted in the above mentioned namespace.

I hope it helps.

Browser other questions tagged

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