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\
?– Diego Souza
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 ....
– Junior Morais
Do you have a file called
FornecedorController
?– Diego Souza
Yes, but there is still no method because I am implementing Servicocontroller first.
– Junior Morais
Try putting in the Model
Servico.php
, a namespace so there at the beginning of the code:use App\Fornecedor;
. Missing a semicolon...– Diego Souza
nothing either... I also tried to change my namespace with the
php artisan app:name protocolo
and put in Model Servicouse protocolo\Fornecedor
. I tried everything here and nothing rs. I believe it is a configuration in the Laravel or a very rough thing– Junior Morais
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)
– Junior Morais
It’s because on that line:
return view('servico.view',['servico'=>$servico]);
, you have to put so:return view('servico.view')->withServico($servico);
– Diego Souza