Skip to content
Advertisement

call value from another table when the value is not null

I want to get the value from table stage_cdl if it has value and from tale stage_vdl if it has value.

Before this, I used this code (below) but I only receive the array until value 5 not include the table stage_vdl_id.

 $data = DB::table('stud_stage_cdl as ssc')
        ->join('license_cdl_module as lcm', 'lcm.id', '=', 'ssc.l_cdl_m_id')
        ->join('invoice_stud as is', 'is.id', '=', 'ssc.inv_stud_id')
        ->where('ssc.stud_id', $postData['stud_id'])
        ->select(
            'lcm.class_code',
            'is.invoice_num',
            'is.total',
            'is.created_at'
        )
        ->get();

The output:

    (
        [0] => stdClass Object
            (
                    [class_code] => D
                    [invoice_num] => W1007INV20051
                    [total] => 1484.00
                    [created_at] => 2020-05-15 10:18:38
            )

        [1] => stdClass Object
            (
                [class_code] => B2
                [invoice_num] => W1007INV20052
                [total] => 1484.00
                [created_at] => 2020-05-15 10:18:55
            )

        [2] => stdClass Object
            (
                [class_code] => DA
                [invoice_num] => W1007INV20053
                [total] => 1484.00
                [created_at] => 2020-05-15 13:44:54
            )

        [3] => stdClass Object
            (
                [class_code] => B2
                [invoice_num] => W1007INV20054
                [total] => 1484.00
                [created_at] => 2020-05-15 13:48:18
            )

        [4] => stdClass Object
            (
                [class_code] => B2
                [invoice_num] => W1007INV20055
                [total] => 1484.00
                [created_at] => 2020-05-15 14:00:54
            )

    )

invoice_stud table

Figure 1

stud_stage_cdl table

Figure 2

stud_stage_vdl table

Figure 3

license_cdl_module table

Figure 4

license_vdl_module table

Figure 5

Here what I’ve done so far, there is no error but no value output. I’m very new with sql. As you can see my code, I’m trying to get the value from the lcm and lvm.

$data = DB::table('invoice_stud as is')
            ->join('stud_stage_vdl as ssv', 'is.stud_id', '=', 'ssv.stud_id')
            ->join('stud_stage_cdl as ssc', 'is.stud_id', '=', 'ssc.stud_id')
            ->join('license_cdl_module as lcm', 'ssc.l_cdl_m_id', '=', 'lcm.id')
            ->join('license_vdl_module as lvm', 'ssv.l_vdl_m_id', '=', 'lvm.id')
            ->where('is.stud_id', $postData['stud_id'])
            ->select(
                'lcm.class_code',
                'lvm.class_code',
                'is.invoice_num',
                'is.stage_cdl_id',
                'is.stage_vdl_id',
                'is.total',
                'is.created_at'
            )
            ->get();

Advertisement

Answer

I have solved this by making them as an array.

   $data['LicenseCDL'] = DB::table('stud_stage_cdl as ssc')
        ->join('license_cdl_module as lcm', 'lcm.id', '=', 'ssc.l_cdl_m_id')
        ->join('invoice_stud as is', 'is.id', '=', 'ssc.inv_stud_id')
        ->where('ssc.stud_id', $stud_id)
        ->select(
            'lcm.class_code',
            'is.total',
            'is.created_at'
        )
        ->get();

    $data['LicenseVDL'] = DB::table('stud_stage_vdl as ssv')
        ->join('license_vdl_module as lvm', 'lvm.id', '=', 'ssv.l_vdl_m_id')
        ->join('invoice_stud as is', 'is.id', '=', 'ssv.inv_stud_id')
        ->where('ssv.stud_id', $stud_id)
        ->select(
            'lvm.class_code',
            'is.total',
            'is.created_at'
        )
        ->get();

error_log($data); //Will receive all of them 
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement