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

Time:10-12

I am a beginner of laravel. I want to join some of the tables and getting it displayed in datatables, I am using yajra datatables. I have three tables which are Area, Branch and Branch Manager. My problem is I want to display Branch table in a blade view, but instead of displaying area_id and branch_manager_id, I want it to display area_desc from Area Table and branch_manager_name from Branch Manager Table. I have no ideas how to get it.

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

Branch Manager 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();
        });
    }

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

Area Model

class Area extends Model
{
    use HasFactory;

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

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

Branch Manager Model

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

Brach Model

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 request for Branch datatables

<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>

Branch Controller

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

Any comments will appreciate, thanks

CodePudding user response:

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

In Blade

<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>
  • Related