Laravel - 如何使用 ajax 显示来自其他 table 的数据

Laravel - How to display data from other table by using ajax

我是laravel的初学者。我想加入一些 tables 并让它显示在 datatables 中,我正在使用 yajra datatables。我有三个 table,分别是区域、分公司和分公司经理。我的问题是我想在 blade 视图中显示分支 table,而不是显示 area_id 和 branch_manager_id,我希望它显示 area_desc 来自 区域Table和branch_manager_name来自分行经理Table。我不知道如何获得它。

面积Table

public function up()
    {
        Schema::create('area', function (Blueprint $table) {
            $table->engine = 'InnoDB';
            $table->bigIncrements('id');
            $table->string('area_code')->unique();
            $table->string('area_desc');
            $table->timestamps();
        });
    }

分行经理Table

    public function up()
    {
        Schema::create('branch_manager', function (Blueprint $table) {
            $table->engine = 'InnoDB';
            $table->bigIncrements('id');
            $table->string('branch_manager_name');
            $table->string('branch_manager_contact_number');
            $table->timestamps();
        });
    }

分支机构Table

public function up()
    {
        Schema::create('branch', function (Blueprint $table) {
            $table->engine = 'InnoDB';
            $table->bigIncrements('id');
            $table->string('branch_name');
            $table->string('branch_address1');
            $table->string('branch_address2');
            $table->bigInteger('area_id')->unsigned();
            $table->bigInteger('branch_manager_id')->unsigned();
            $table->timestamps();
        });

        Schema::table('branch', function (Blueprint $table) {
            $table->foreign('area_id')->references('id')->on('area')->onUpdate('cascade');
            $table->foreign('branch_manager_id')->references('id')->on('branch_manager')->onUpdate('cascade');
        });
    }

面积模型

class Area extends Model
{
    use HasFactory;

    protected $table = 'area';
    protected $fillable = [
        'area_code',
        'area_desc',
    ];

    public function branch()
    {
        return $this->hasMany(Branch::class);
    }
}

分行经理模型

class BranchManager extends Model
{
    use HasFactory;

    protected $table = 'branch_manager';
    protected $fillable = [
        'branch_manager_name',
        'branch_manager_contact_number'
    ];

    public function branch()
    {
        return $this->hasOne(Branch::class);
    }
}

布拉赫模型

class Branch extends Model
{
    use HasFactory;

    protected $table = 'branch';
    protected $fillable = [
        'branch_name',
        'branch_address1',
        'branch_address2',
        'area_id',
        'branch_manager_id',
    ];

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

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

Ajax 请求分支数据tables

<script type="text/javascript">
        $(function() {
            $.ajaxSetup({
                headers: {
                    'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
                }
            });

            var table = $('.table-striped.first').DataTable({
                processing: true,
                serverSide: true,
                ajax: "{{ route('settings.branch.list') }}",
                columns: [{
                        data: 'branch_name',
                        name: 'branch_name'
                    },
                    {
                        data: 'branch_address1',
                        name: 'branch_address1'
                    },
                    {
                        data: 'branch_address2',
                        name: 'branch_address2'
                    },
                    {
                        data: 'area_id',
                        name: 'area_id'
                    },
                    {
                        data: 'branch_manager_id',
                        name: 'branch_manager_id'
                    },
                    {
                        data: 'action',
                        name: 'action',
                        orderable: true,
                        searchable: true
                    },
                ]
            });
</script>

分支控制器

    public function branch(Request $request)
    {
        $branches = Branch::latest()->get();
        $areas = Area::latest()->get();
        $branchManagers = BranchManager::latest()->get();

        if ($request->ajax()) {
            $data = Branch::latest()->get();
            return Datatables::of($data)
            ->addIndexColumn()
            ->addColumn('action', function($row){
                $btn = '<a href="javascript:void(0)" data-toggle="tooltip"  data-id="'.$row->id.'" data-original-title="Edit" class="btn btn-sm btn-outline-light editRecord">Edit</a>';

                $btn = $btn.' <a href="javascript:void(0)" data-toggle="tooltip"  data-id="'.$row->id.'" data-original-title="Delete" class="btn btn-sm btn-outline-light deleteRecord"><i class="far fa-trash-alt btn-outline-danger"></i></a>';

                return $btn;
            })
            ->rawColumns(['action'])
            ->make(true);
        }

        return view('admin.branch', compact('branches', 'areas', 'branchManagers'));
    }

任何意见将不胜感激,谢谢

在BrachController.php

public function branch(Request $request)
    {
        if ($request->ajax()) {
            $data = Branch::with('area','branchManager')->latest()->get();
            return Datatables::of($data)
            ->addIndexColumn()
            ->addColumn('area_desc', function($row){
                return $row->area->area_desc;
             })
            ->addColumn('branch_manager_name', function($row){
                return $row->branchManager->branch_manager_name;
             })
            ->addColumn('action', function($row){
                $btn = '<a href="javascript:void(0)" data-toggle="tooltip"  data-id="'.$row->id.'" data-original-title="Edit" class="btn btn-sm btn-outline-light editRecord">Edit</a>';

                $btn = $btn.' <a href="javascript:void(0)" data-toggle="tooltip"  data-id="'.$row->id.'" data-original-title="Delete" class="btn btn-sm btn-outline-light deleteRecord"><i class="far fa-trash-alt btn-outline-danger"></i></a>';

                return $btn;
            })
            ->rawColumns(['area_desc', 'branch_manager_name', 'action'])
            ->make(true);
        }

        return view('admin.branch');
    }

刀锋

<script type="text/javascript">
        $(function() {
            $.ajaxSetup({
                headers: {
                    'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
                }
            });

            var table = $('.table-striped.first').DataTable({
                processing: true,
                serverSide: true,
                ajax: "{{ route('settings.branch.list') }}",
                columns: [{
                        data: 'branch_name',
                        name: 'branch_name'
                    },
                    {
                        data: 'branch_address1',
                        name: 'branch_address1'
                    },
                    {
                        data: 'branch_address2',
                        name: 'branch_address2'
                    },
                    {
                        data: 'area_desc',
                        name: 'area_desc'
                    },
                    {
                        data: 'branch_manager_name',
                        name: 'branch_manager_name'
                    },
                    {
                        data: 'action',
                        name: 'action',
                        orderable: true,
                        searchable: true
                    },
                ]
            });
</script>