Search code examples
laraveleloquentlaravel-5.3

Get Images via relationship


From the charts, I want to get each images which trade_id via relationship.

For example, if trade_id is 62, from the charts table, get all images which has 62 in trade_id.

charts table structure:

enter image description here

Trade model:

    public function chart()
{
    return $this->hasMany('App\Chart');
}

@if($trades)
     <tbody>
     @foreach($trades as $trade)
           <tr>
              <td> <img src="{{$trade->chart->file }}" style="width:32px; height:32px; position:absolute; top:10px; left:10px; border-radius:50%"> </td>
          </tr>
     @endforeach
     </tbody>
            @else
            <h1>No trades</h1>
@endif

OUTPUT has error:

Undefined property: Illuminate\Database\Eloquent\Relations\HasMany::$file (View: C:\xampp\htdocs\try\ytl\resources\views\member\add-single-trade\index.blade.php)

CONTROLLER:

    public function index()
{
    $user_id = Auth::user()->id;
    $trades = Trade::where('user_id','=',Auth::id())->get();
    return view('member.add-single-trade.index', compact('trades'));

}

DD($Trade): Result

Trade {#541 ▼
  #dates: array:1 [▼
    0 => "deleted_at"
  ]
  #fillable: array:10 [▼
    0 => "user_id"
    1 => "symbol_id"
    2 => "is_action"
    3 => "rate"
    4 => "tradedate"
    5 => "note"
    6 => "trade_id"
    7 => "exchange_id"
    8 => "market_id"
    9 => "stoploss"
  ]
  #connection: null
  #table: null
  #primaryKey: "id"
  #keyType: "int"
  #perPage: 15
  +incrementing: true
  +timestamps: true
  #attributes: array:14 [▼
    "id" => 3
    "user_id" => 1
    "symbol_id" => 5
    "exchange_id" => 1
    "market_id" => 1
    "is_action" => 1
    "rate" => 5641
    "tradedate" => "2018-04-05 00:00:00"
    "note" => ""
    "created_at" => "2018-04-18 13:00:23"
    "updated_at" => "2018-04-18 13:00:23"
    "deleted_at" => null
    "quantities" => 0
    "stoploss" => 0
  ]
  #original: array:14 [▼
    "id" => 3
    "user_id" => 1
    "symbol_id" => 5
    "exchange_id" => 1
    "market_id" => 1
    "is_action" => 1
    "rate" => 5641
    "tradedate" => "2018-04-05 00:00:00"
    "note" => ""
    "created_at" => "2018-04-18 13:00:23"
    "updated_at" => "2018-04-18 13:00:23"
    "deleted_at" => null
    "quantities" => 0
    "stoploss" => 0
  ]
  #relations: []
  #hidden: []
  #visible: []
  #appends: []
  #guarded: array:1 [▼
    0 => "*"
  ]
  #dateFormat: null
  #casts: []
  #touches: []
  #observables: []
  #with: []
  +exists: true
  +wasRecentlyCreated: false
  #forceDeleting: false
}


Solution

  • You have to loop over the charts :

    @if($trades)
         <tbody>
         @foreach($trades as $trade)
            @foreach($trade->chart as $chart)
               <tr>
                  <td> <img src="{{$chart->file }}" style="width:32px; height:32px; position:absolute; top:10px; left:10px; border-radius:50%"> </td>
              </tr>
            @endforeach
         @endforeach
         </tbody>
                @else
                <h1>No trades</h1>
    @endif
    

    And one more thing the path of the image will not work, assuming you have the images in images folder inside the public one, you have to chage :

    <img src="{{$chart->file}}" style="width:32px; height:32px; position:absolute; top:10px; left:10px; border-radius:50%">
    

    To

    <img src="{{Storage::get('public/charts/', $chart->file) }}" style="width:32px; height:32px; position:absolute; top:10px; left:10px; border-radius:50%">