Wanted to display all the row data inside the table BY ID

Viewed 54

I wanted to fetch multiple row data in a table by ID but this code throwing all rows. IDK how to do that please review my code and give feedback. And also I attached the output image which I'm getting. I want to display all the row data inside the table where is (शिर्षक डेबिट क्रेडिट) these topics. [1]: https://i.stack.imgur.com/cFo7b.png

----modal-----

<script>
        $(document).ready(function() {
            $('.view_btn').click(function(e) {
                e.preventDefault();
                var id = $(this).closest('tr').find('.id').text();
                $.ajax({
                    type: "GET",
                    url: " {{ route('biniyojan_popup_details') }}",
                    data: {
                        'checking_viewbtn': true,
                        'id': id,
                    },
                    //success: function(response){
    
    
                    success: function(data) {
                        var html = '';
                        var biniyojan_popup_details = data.biniyojan_popup_details;
                        if (biniyojan_popup_details.length > 0) {
                            for (let i = 0; i < biniyojan_popup_details.length; i++) {
                                html += `
    
                                        <table border="1" class="table table-hover table-wrapper-scroll-y my-custom-scrollbar">
                                            <thead class="text-white bg-primary" style="font-size: 14px;">
                                                <tr>
                                                <th scope="col">S.N </th>
                                                <th scope="col">मिति </th>
                                                <th scope="col">आ.ब </th>
                                                <th scope="col">स्रोत व्यहोर्ने संस्था</th>
    
                                            </thead>
                                            <tbody class="bbody" style="font-size: 14px;">
                                                <tr>
                                                <td>` + biniyojan_popup_details\[i\]\['biniyojan_id'\] + `</td>
                                                <td>` + biniyojan_popup_details\[i\]\['date'\] + `</td>
                                                <td>` + biniyojan_popup_details\[i\]\['ab'\] + `</td>
                                                <td>` + biniyojan_popup_details\[i\]\['school'\] + `</td>
                                                </tr>
                                            </tbody>
                                        </table>
    
                                        <table id="example" border="1"  style=" width:50% ; " class="table table-hover table-wrapper-scroll-y my-custom-scrollbar">
                            <thead class="text-white bg-primary" style="font-size: 14px;">
                                <tr>
                                    <th>शिर्षक</th>
                                    <th>डेबिट</th>
                                    <th>क्रेडिट</th>
                                </tr>
                            </thead>
                            <tbody class="text-dark" style="font-size: 14px;">
                                <tr>
                                <td>` + biniyojan_popup_details\[i\]\['kriyakalap'\] + `</td>
                                <td id="bujet">` + biniyojan_popup_details\[i\]\['cash'\] + `</td>
    
                                </tr>
                                <tr>
                                <td>` + biniyojan_popup_details\[i\]\['debit_credit_type'\] + `</td>
                                    <td></td>
                                    <td id="">` + biniyojan_popup_details\[i\]\['cash'\] + `</td>
                                </tr>
                                <tr class="text-white bg-secondary" style="font-size: 14px;">
                                    <td style="font-weight:bold;">Total</td>
                                    <td style="font-weight:bold;">` + biniyojan_popup_details\[i\]\['cash'\] + `</td>
                                    <td style="font-weight:bold;">` + biniyojan_popup_details\[i\]\['cash'\] + `</td>
                                </tr>
                            </tbody>
                        </table>
                        
                                        `;
                            }
                        } else {
                            html += `
                                        <tr>
                                        <td colspan = "9" >  Data not Found! </td> 
                                        </tr>
                                        `;
                        }
                        $(".modal-body").html(html);
                        $('#exampleModalCenter').modal('show');
                    }
                })
            });
        });
    </script>
    
    <!-- Modal Data -->][1]

----Controller-----

    public function biniyojan_popup_details()
    {

        $biniyojan_popup_details = DB::table('biniyojan_details')
        ->leftJoin('biniyojan','biniyojan.biniyojan_id','biniyojan_details.biniyojan_id')
        ->get();
        return response()->json(\['biniyojan_popup_details' => $biniyojan_popup_details\]);
    }
1 Answers

Try this slight change. It adds a '=' to define what type of comparison we're making with biniyojan.biniyojan_id and biniyojan_details.biniyojan_id.

    {

        $biniyojan_popup_details = DB::table('biniyojan_details')
        ->leftJoin('biniyojan','biniyojan_details.biniyojan_id','=','biniyojan.biniyojan_id')
        ->get();
        return response()->json(\['biniyojan_popup_details' => $biniyojan_popup_details\]);
    }

You can try adding a where clause to your original code or try an alternative method using Models. This code works perfectly for me in my own project and I adapted it to your table names. I think you're trying to get details by an ID.

$biniyojan_popup_details = Biniyojan::where('biniyojan.biniyojan_id','=',$id)
            ->leftJoin('biniyojan_details','biniyojan.biniyojan_id','=','biniyojan_details.biniyojan_id')
            ->select('binoyojan_details.*','biniyojan.field1')
            ->get();
Related