Home > Back-end >  How to pass data from Controller to Blade Laravel
How to pass data from Controller to Blade Laravel

Time:05-21

i've been searching the cause of my error. So basicly i want to pass data from Controller to Blade using Compact but turns out the data is sent, but the blade won't read the data, instead it just print like normal print html. Please help me.

This is the controller

    public function detailproduk($id){
        $produk = Produk::all();
        $produk2= Produk::where('id', $id)->first();
        $data = "IPHONE";
        $pro2 = Produk::where('id', $id)->first();


        //dd($produk2);
        return view('homepage/detailproduk', compact('produk2'));
    }

this is the blade

<link href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.0/css/bootstrap.min.css" rel="stylesheet" id="bootstrap-css">
<script src="//maxcdn.bootstrapcdn.com/bootstrap/3.3.0/js/bootstrap.min.js"></script>
<script src="//code.jquery.com/jquery-1.11.1.min.js"></script>
<!------ Include the above in your HEAD tag ---------->

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <meta charset="utf-8">
    <title>Product Detail</title>
    <link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet">
    <link href="https://fonts.googleapis.com/css?family=Open Sans:400,700" rel="stylesheet">
    <link rel="stylesheet" href="<?php echo asset('css/detailproduk.css')?>" type="text/css"> 
  </head>

  <body>
      
    <div >
        <div >
            <div >
                <div >
                    
                    <div >
                        
                        <div >
                        <br>
                          <div  id="pic-1"><img src="http://placekitten.com/400/252" /></div>
                        </div>
                        
                    </div>

                    <div >
                        <h3 >{{$pro2->produk_name}}</h3>
                        <div >
                            <div >
                                <span ></span>
                                <span ></span>
                                <span ></span>
                                <span ></span>
                                <span ></span>
                            </div>
                            <span >{{$produk2->product_rate}}</span>
                        </div>
                        <p >{{$produk2->description}}</p>
                        <h4 >current price: <span>{{$produk2->price}}</span></h4>
                        <p ><strong>91%</strong> of buyers enjoyed this product! <strong>(87 votes)</strong></p>
                        <h5 >Stock:
                            <span  data-toggle="tooltip" title="small">{{$produk2->stock}}</span>
                        </h5>
                        <h5 >colors:
                            <span  data-toggle="tooltip" title="Not In store"></span>
                            <span ></span>
                            <span ></span>
                        </h5>
                        <div >
                            <button  type="button">Buy Now</button>
                            <button  type="button"><span ></span> Add to Chart</button>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
  </body>
</html>

and the result is this enter image description here

CodePudding user response:

Helooww, i hope that's will help you.

It's with compact, but you need to make a foreach in your blade and make sur if detailproduk is a good directory.

    public function detailproduk($id){
        $produk = Produk::all();
        $produk2= Produk::where('id', $id)->first();
        $data = "IPHONE";
        $pro2 = Produk::where('id', $id)->first();
        //dd($produk2);
        return view('homepage/detailproduk', compact(['produk', 'produk2', 'data', 'pro2']));
    }

Like so NB : That's not a good method but it's will work.. if you want to use your compact in blade page make a @foreach($produk as $item). You can also add et key if u need like @foreach($produk as $key => $item)

<div >
    @foreach ($pro2 as $pro2Item)
    <h3 >{{$pro2Item->produk_name}}</h3>
    @foreach ($produk2 as $produk2Item)
    <div >
        <div >
            <span ></span>
            <span ></span>
            <span ></span>
            <span ></span>
            <span ></span>
        </div>
        <span >{{$produk2Item->product_rate}}</span>
    </div>
    <p >{{$produk2Item->description}}</p>
    <h4 >current price: <span>{{$produk2Item->price}}</span></h4>
    <p ><strong>91%</strong> of buyers enjoyed this product! <strong>(87 votes)</strong></p>
    <h5 >Stock:
        <span  data-toggle="tooltip" title="small">{{$produk2Item->stock}}</span>
    </h5>
    <h5 >colors:
        <span  data-toggle="tooltip" title="Not In store"></span>
        <span ></span>
        <span ></span>
    </h5>
    <div >
        <button  type="button">Buy Now</button>
        <button  type="button"><span ></span> Add to Chart</button>
    </div>
    @endforeach
    @endforeach
</div>

CodePudding user response:

when received compact data in blade file, you need to use loop for Retive data,

  • Related