【问题标题】:I'm trying to get 'experiencias` from my data base with relationship one to many, but it returns empty collection, how can I fix it?我试图从我的数据库中以一对多的关系获取“experiencias”,但它返回空集合,我该如何解决?
【发布时间】:2019-08-21 08:10:36
【问题描述】:

我正在使用 Laravel 做一个小 Web 应用程序,我将信息直接放在我的数据库中,我的表 Experiencia 我有:

id  matricula_id    nombre_Emp  puesto  
1   201618131       1           1   

这是我的Usuario 模型:

protected $table = 'usuarios';

protected $fillable = [
 'id',
 'matricula_id',
 'nombres',
 'email',
 'password',
 'facultad',
];

 public function experiencia()
 {
   return $this->hasMany(Experiencia::class,'matricula_id');
 }

还有我的Experiencia 模特:

protected $table = 'experiencias';
protected $fillable = [
    'id',
    'matricula_id',
    'nombre_Emp',
    'puesto',
    'fecha_Ini',
    'fecha_Fin',
];

public function usuario()
{
     return $this->belongsTo(usuarios::class,'matricula_id');
}

两个模型都有可填写的matricula_id

而我的controller我要恢复的函数是:

public function index($matricula)
{
     $usuario = usuarios::where('matricula_id', $matricula)->with('experiencia')->get();
     dd($usuario->all());
}

当我做 dd 我的结果是这样的:

array:1 [▼

  0 => usuarios {#282 ▼

    #table: "usuarios"

    #fillable: array:20 [▶]

    #hidden: array:2 [▶]

    #casts: array:1 [▶]

    #connection: "mysql"

    #primaryKey: "id"

    #keyType: "int"

    +incrementing: true

    #with: []

    #withCount: []

    #perPage: 15

    +exists: true

    +wasRecentlyCreated: false

    #attributes: array:24 [▶]

    #original: array:24 [▶]

    #changes: []

    #dates: []

    #dateFormat: null

    #appends: []

    #dispatchesEvents: []

    #observables: []

    #relations: array:1 [▼ 

      "experiencia" => Collection {#279 ▼

        #items: [] <<<<-------- Here is my problem

      }

    ]

    #touches: []

    +timestamps: true

    #visible: []

    #guarded: array:1 [▶]

    #rememberTokenName: "remember_token"

  }

]

我希望我的数据库中有一个 experiencia 数组。

【问题讨论】:

    标签: laravel eloquent laravel-5.8


    【解决方案1】:

    您实际上可能调用了错误的模型。

    public function index($matricula)
    {
        $experiencia = Experiencia::with('usuario')->whereHas('usuario', function($usuario) use($matricula) {
            return $usuario->where('matricula_id', $matricula);
        })->get();
    
        dd($experiencia->all());
    }
    
    

    你的模型关系也错了

    public function usuario()
    {
         return $this->belongsTo(usuario::class);
    }
    

    【讨论】:

    • 嘿,谢谢你的回答,我正在尝试你的但我得到了'未定义的变量:矩阵'
    • @Jaime 我已经更新了我的代码。我错过了刚才的use()
    • 我的输出是 '[ ]'
    • @Jaime 似乎您在 Experiencia 模型上的关系是错误的。它应该是belongsTo。我更新了我的答案。
    • 如果我这样做了,那么我的模型搜索'usuarios.usuario_id',但我认为不正确,因为我正在搜索我的外键'usuarios.matricula_id',我用我的可填充变量更新我的帖子
    【解决方案2】:

    试试下面这段代码;

    public function show($param = null)
    {
        if (!$param) {
            return abort(404, "there is no id present");
        }
        $param = (int) $param;
        $usuario = usuarios::with('experiencia')->where('matricula_id', $param)->first();
    
         dd($usuario->experiencia);
    }
    

    index函数不需要参数,如果参数没有发送,请保护你的代码,不要运行其余代码。

    【讨论】:

    • 嘿,谢谢你的回答,我正在尝试你的但我开始'“这个集合实例上不存在属性 [experiencia]。”'
    • @Jaime 你能更新你的两个模态 $fillable 字段吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-02-06
    • 2020-02-23
    • 2021-01-31
    • 2017-10-28
    • 2019-06-22
    • 2021-11-26
    • 2019-11-08
    相关资源
    最近更新 更多