Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/extjs/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Laravel 对关系中的两级父表的LAVEL访问_Laravel_Eloquent - Fatal编程技术网

Laravel 对关系中的两级父表的LAVEL访问

Laravel 对关系中的两级父表的LAVEL访问,laravel,eloquent,Laravel,Eloquent,我使用了laravel,我有这个模型和表之间的关系 客户表 class Customers extends Model { public $primaryKey = 'id'; protected $fillable = [ 'contr_nom', 'contr_cog', 'benef_nom', 'benef_cog', 'email', 'polizza', '

我使用了laravel,我有这个模型和表之间的关系

客户表

class Customers extends Model
{
    public $primaryKey = 'id';
    protected $fillable = [
        'contr_nom',
        'contr_cog',
        'benef_nom',
        'benef_cog',
        'email',
        'polizza',
        'targa',
        'iban',
        'int_iban',
        'cliente',
    ];

    public function claims()
        {
            return $this->hasMany(Claims::class);
        }
        
    public function refunds()
    {
        return $this->hasManyThrough(Refunds::class, Claims::class);
    }

}
索赔表

class Claims extends Model
{
    public $primaryKey = 'id';
    protected $fillable = [
        'dossier',
        'date_cla',
    ];

    public function refunds()
        {
            return $this->hasMany(Refunds::class);
        }           
        
    public function customers()
        {
            return $this->belongsTo(Customers::class,'customers_id');
        }           

}
退款表

class Refunds extends Model
{
    public $primaryKey = 'id';
    protected $fillable = [
        'date_ref',
        'status_ref',
        'disactive',
        'num_pre',
        'date_liq',
    ];

    public function services()
        {
            return $this->belongsToMany(Services::class)
                ->withPivot(['services_id','services_amount','services_status']);
        }       
        
    public function claims()
        {
            return $this->belongsTo(Claims::class,'claims_id');
        }           

}
在控制器中,我完成了这个功能

 public function addDateLiq2(Request $request){
 
      $date_liq = request("date_liq");

      $refunds = Refunds::whereNotNull('num_pre')->get();
      
      foreach ($refunds as $refund) {
        $status_ref= $refund->status_ref;
        if ($status_ref == 5){
          //send mail
          //I need to retrieve mail field from customers table
        }

        $refund->date_liq = $date_liq;
        $refund->save();

        if(!$refund->save()){
          App::abort(500, 'Error');
        }
      }

      return Response::json(array('success' => 'Date salvate massivamente correttamente!'), 200);

    }
在num_pre不为null的所有记录中添加日期。 好的,我还想发送邮件,但邮件字段在客户父表中…我如何访问它?
Thx

好的,我似乎在函数addDateLiq2中找到了一种方法

      $data =  Claims::with(array('customers'=>function($query){
        $query->select('id','email');
      }))
      ->whereHas('refunds', function($query) use($claims_id) {
        $query->where('claims_id', $claims_id);
      })
      ->first();