Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Kanchan186's avatar

Didn't getting error but not fetch data from tables and join query not works please give me some suggestions where i going wrong

pcview.blade.php

<body>
        <div class="flex-center position-ref full-height">
            

            <div class="content">
                
                   
                  <br><br><br><br><br><br>
                    <table  class="table table-striped">
                        
                        <tr>
                            <th>Product category ID</th>
                            <th>Product ID</th>
                            <th>Product Name</th>
                            <th>Category ID</th>
                            <th>Category Name</th>
                            
                        </tr>


            @foreach($result as $row)
            <tr>
            <td>{{$row->pc_id}}</td>    
            <td>{{$row->p_id}}</td>
            <td>{{$row->p_name}}</td>
            <td>{{$row->c_id}}</td>
            <td>{{$row->c_name}}</td>
        
      <!--      <td><a href="{{url('/')}}/edit/{{$pc->pc_id}}"  class="btn btn-success">Edit</a></td>
            <td><a href="{{url('/')}}/delete/{{$pc->c_id}}" class="btn btn-danger">Delete</a></td> -->
            </tr>
             @endforeach
            
                    </table>

                    <br><hr>


                   
                        
                </div>
      
                
            </div>


       
    </body>

controller

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\prod_cat;
use App\product;
use App\category;
use DB;

class ProdCatController extends Controller
{
     public function store2(Request $request)
    {
        //dd($request->all());
        foreach(request('cat') as $cat_id) 
         {
            prod_cat::create([
            'p_id'=> request('p_id'),
              //'p_name'=>request('p_name'),
            'c_id'=> $cat_id,
             // 'c_name'=>request('c_name'),
            ]);
        }
    
         $product=product::get();
       $category=category::get();
      $prod_cat=prod_cat::get();

        array($request->get('c_id'));

return redirect('viewpc');
   // return view('pcview',compact('product','category','prod_cat'));
    }



public function show2(Request $request)
                 {

dd($request->all());

                 // $product=product::get();
                 // $category=category::get();
                  // $prod_cat= prod_cat::get();

   /*           
   $result=DB::table('products')
         ->join('categories','categories.c_id','=','products.p_id')
         ->join('prod_cats','prod_cats.pc_id','=','products.p_id')
         ->select('prod_cats.pc_id','products.p_id','products.p_name','categories.c_id','categories.c_name') 
         ->get();  

         */


    $result=DB::table('prod_cats')
          ->join('products','products.p_id','=','prod_cats.pc_id')  
          ->join('categories','categories.c_id','=','prod_cats.pc_id') 
          ->select('prod_cats.pc_id','products.p_id','products.p_name','categories.c_id','categories.c_name')->get();      
     


    return view('pcview',compact('result'));


     //  echo"<pre>";
     // print_r($result);

                   //return redirect('pcview');
                  } 
}
0 likes
2 replies
Nakov's avatar

@kanchan186 stucked at the same views for days now, you should learn how to debug your code my friend.

If you don't have an error, but you see the view, and there are no results than obviously there is either no data in your database or your query is wrong.

public function show2(Request $request)
{

    dd($request->all()); // remove this line to see your view

    $result=DB::table('prod_cats')
          ->join('products','products.p_id','=','prod_cats.pc_id')  
          ->join('categories','categories.c_id','=','prod_cats.pc_id') 
          ->select('prod_cats.pc_id','products.p_id','products.p_name','categories.c_id','categories.c_name')->get();      
     
    
    dd($result); // print the result here and make sure that there is data coming from your query.

    return view('pcview',compact('result'));
} 

In your query above what I've noticed is you use prod_cats.pc_id for both getting the products and the categories. Is that maybe wrong? Maybe you need to get the categories from a column in the products table instead..

maybe you need this line?

->join('categories','categories.c_id','=','products.c_id') 
microsoftjulius's avatar

Do you have a Column called prod_cats.pc_id in the Categories table?

If you don't have it, then use ->join('categories','categories.c_id','=','products.c_id') instead of

->join('categories','categories.c_id','=','prod_cats.pc_id') .

Or else, make sure you have Content in your tables that matches all. If any of the tables lacks content and is used in the join, then the result will be empty. Make sure You have content in your tables

1 like

Please or to participate in this conversation.