I want to make the following query with Eloquent:
$nota=DB::table('notas')
->join('users', 'users.id', '=', 'notas.id_user')
->select('notas.id','notas.nombre', 'notas.descripcion', 'users.name AS user_name', 'users.email')
->first();
I try to make the relation in the models and called like this, in my controller:
public function show($id)
{
$nota = Nota::with(['user','nota'])->first();
print_r($nota);
return view("notas.detalle", compact("nota"));
}
But i get the follow error:
Illuminate\Database\Eloquent\RelationNotFoundException Call to undefined relationship [nota] on model [App\Nota].
My models looks like this: Nota.php:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Nota extends Model
{
public function user()
{
return $this->belongsTo('App\User');
}
}
User.php:
<?php
namespace App;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
class User extends Authenticatable
{
use Notifiable;
public function notas()
{
return $this->hasMany('App\Nota');
}
}