0

I am having trouble with my code. I have two tables Stocks and StockLists however, I want to get all the related stock in the stocks list based on the stock code. But the result is repeated in each stocks.... Here is the link of the image https://www.screencast.com/t/rHdT2gigh

public function index()
{
    $stocks = Stock::all();
     foreach ($stocks as $stock) {
        $stockInfos = DB::table('stocksList')
        ->where('stockCode', $stock->stockCode)
        ->take(3)
        ->get();
     }
     return view('welcome', compact('stocks', 'stockInfos'));
}

Here is my blade.php

@forelse($stocks as $stock)
                    <tr align="left">
                        <th scope="row">{{ $stock->stockCode }}</th>
                        <td><strong>{{ $stock->stockName }}</strong></td>
                        <td><strong>
                            @forelse($stockInfos as $stockInfo)
                            {{ $stockInfo->stockDate }} : {{ $stockInfo->stockPrice }}
                            <br>
                            @empty
                            <em>No Data</em>
                            @endforelse                            
                        </strong></td>    
                    </tr>
                    @empty
                    @endforelse
4
  • $stockInfos[] = DB::table('stocksList')... Commented Sep 6, 2017 at 10:25
  • can you please give details of stock and stocklist table? Commented Sep 6, 2017 at 10:29
  • @Steve what will I do on my blade.php upon using this? Commented Sep 6, 2017 at 10:33
  • @Nikita stock table has only stock code, while the stocklist has the list of prices every day.. link Commented Sep 6, 2017 at 10:34

2 Answers 2

1

You are overwritting the $stockInfos variable try with

public function index()
{
    $stocks = Stock::all();
    $stockInfos = [];
     foreach ($stocks as $stock) {
        array_push($stockInfos,DB::table('stocksList')
        ->where('stockCode', $stock->stockCode)
        ->take(3)
        ->get());
     }
     return view('welcome', compact('stocks'))->with('stockInfos',collect($stockInfos));
}

And in your view :

@forelse($stocks as $stock)
      <tr align="left">
          <th scope="row">{{ $stock->stockCode }}</th>
          <td><strong>{{ $stock->stockName }}</strong></td>
          <td><strong>
                            @forelse($stockInfos->where('stockCode',$stock->stockCode) as $stockInfo)
                            {{ $stockInfo->stockDate }} : {{ $stockInfo->stockPrice }}
                            <br>
                            @empty
                            <em>No Data</em>
                            @endforelse                            
                        </strong></td>    
                    </tr>
                    @empty
                    @endforelse
Sign up to request clarification or add additional context in comments.

3 Comments

Thank you but I get an error "Call to a member function where() on array"
I tried your updated code it works but it shows no data :(
do dd($stocks,$stockInfos right before the return in your controller and look for the data there, maybe the problem is in the query
0

Try this :

$stockInfos = DB::table('Stock')
             ->join('stocksList','stocksList.stockCode','=','Stock.stockCode')
             ->get();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.