is this controller code correct?
$product = Product::find($id)->with('dsa');
using only this
$product = Product::find($id);
It works correctly for me but when I try to add the related table I can't do it and I tried in a thousand ways and nothing... I have related tables both in my innoDB database and in the models...
MODEL
namespace App;
use Illuminate\Database\Eloquent\Model;
class Product extends Model
{
protected $fillable = [
'id', 'nombre', 'apellidos', 'nombreyape', 'nacimiento', 'fallecimiento', 'user_id'
];
public function dsa()
{
return $this->hasMany('App\Dsa', 'di', 'id');
}
}
Dsa model namespace App;
use Illuminate\Database\Eloquent\Model;
class Dsa extends Model
{
protected $fillable = [
'user_id', 'di', 'nombre', 'apellidos', 'enlace'
];
public function product()
{
return $this->belongsTo('App\Product', 'id', 'di');
}
}
using this controller
$product = Product::with('dsa')->find($id);
It works for me to display the entire table using
{{ $product->dsa }}
and it returns me this exactly
[{"id":10,"user_id":21,"di":39,"name":"pepe","surname":"lorenzo","link":"example","created_at":"2018 -10-06 14:50:37","updated_at":"2018-10-06 14:50:37"},{"id":11,"user_id":21,"di":39,name" :"juan","surname":"dented","link":"example","created_at":"2018-10-06 14:51:11","updated_at":"2018-10-06 14: 51:11"}]
but if i try to call something just like the name for example
{{ $product->dsa->nombre }}
I get the following error Property [name] does not exist on this collection instance.
Fixed it was @foreach issue thanks for the help