laravel 从子关系中获取父数据

laravel get parent data from child relationship

嗨,我在客户和索赔之间有这种关系

客户模型

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

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);
    }

}

理赔模式

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

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');
        }           

}

我需要将一些来自索赔和客户的信息放入数据table

他们有父子关系作为数据的第一列和第二列table我从索赔 table 中输入了 id 和档案,但是我如何才能得到例如 contr_nom, contr_cog、来自客户 table 的每个档案的电子邮件、polizza 等?

我试过这个控制器

class ComptaController extends Controller
{
    
    public function index(Request $request){

        if ($request->ajax()) {
            $data = Claims::with('customers')->get();

            /*Cannot access to  Customers from $data but i can see in the relationship*/
            dd($data->customers->contr_nom); GIVE ME ERROR

            return Datatables::of($data)
                ->addIndexColumn()
                ->addColumn('action', function($row){
                    $btn = '<a href="javascript:void(0)" class="edit btn btn-success btn-sm">Edit</a> <a href="javascript:void(0)" class="delete btn btn-danger btn-sm">Delete</a>';
                    return $btn;
                })
                ->rawColumns(['action'])
                ->make(true);
        }
    
    return view('pages.compta');

    }
    
}

但我无法访问每个档案的客户数据.. 谢谢

foreach($data->customers as $customer){
    dd($customer->contr_nom);
}

   Claims::with(array('customer'=>function($query){
        $query->select('id','contr_nom');
    }))->get();

使用数据表,我想您可以通过使用闭包方法添加列来添加相关的客户详细信息,例如

return Datatables::of($data)
                ->addIndexColumn()
                ->addColumn('contr_nom',function($row){
                    return $row->customers->contr_nom;
                })
                ->addColumn('contr_cog',function($row){
                    return $row->customers->contr_cog;
                })
                ->addColumn('email',function($row){
                    return $row->customers->email;
                })
                //.. and so on otehr columns
                ->addColumn('action', function($row){
                    $btn = '<a href="javascript:void(0)" class="edit btn btn-success btn-sm">Edit</a> <a href="javascript:void(0)" class="delete btn btn-danger btn-sm">Delete</a>';
                    return $btn;
                })
                ->rawColumns(['action'])
                ->make(true);

我猜你每次索赔都有一个客户,所以如果你在索赔模型中将 customers() 重命名为 customer() 会更好