【问题标题】:Get Images via relationship通过关系获取图像
【发布时间】:2018-11-27 04:14:06
【问题描述】:

charts,我想通过关系获取trade_id 的每个图像。

例如trade_id为62,则从charts表中,获取trade_id中所有62的图片。

charts表结构:

Trade模特:

    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

输出有错误:

未定义属性:Illuminate\Database\Eloquent\Relations\HasMany::$file(查看:C:\xampp\htdocs\try\ytl\resources\views\member\add-single-trade\index.blade.php )

控制器:

    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):结果

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
}

【问题讨论】:

  • 首先dd();您的查询并检查它是否为您提供文件数据?另外,你在交易模型中建立了hasMany关系,那么图表模型呢?我认为有 belongsTo 关系。
  • 这个刀片和桌子,我可以用DD();?@HirenGohel
  • 我还有一个问题,我是否应该将关系保留在chart 表中?
  • 现在,试试这个:src="{{$trade-&gt;chart-&gt;file }}"
  • @KinnariPrajapati 不同之处在于,在您的代码中的chart 之后,您有(),尝试删除它们,所以它只是chart-&gt;

标签: laravel eloquent laravel-5.3


【解决方案1】:

你必须遍历图表:

@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

还有一件事图像的路径不起作用,假设您在公共图像文件夹中有图像,您必须更改:

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

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

【讨论】:

  • 我觉得你很完美。让我试试。
  • 我会在 5 分钟后入住。 @Maraboc
  • 错误:Undefined property: Illuminate\Database\Eloquent\Collection::$chart (View: C:\xampp\htdocs\try\ytl\resources\views\member\add-single-trade\index.blade.php)
  • 这是我的关系代码:public function chart() { return $this-&gt;hasMany('App\Chart'); }
  • 我将charts 文件夹放入public path
【解决方案2】:

在 foreach 中添加关系。

 @foreach($trades->chart as $trade)
           <tr>
              <td> <img src="{{$trade->file }}"> </td>
          </tr>
     @endforeach

【讨论】:

  • 这没什么结果
  • 你好,@Ali Ozen,你知道我该怎么办吗?
【解决方案3】:

型号:

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

查看:

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

【讨论】:

  • 图片可能超过1个,我应该使用哪种模式relationship code
  • 答案会被编辑,以防每笔交易都有很多图表。请再次检查
  • 错误:syntax error, unexpected 'endif' (T_ENDIF)
  • 再次出错:Call to a member function all() on null
  • 你好,你能指导我吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-03-17
相关资源
最近更新 更多