にリンクする、より良い方法I有しIDにより画像を取得し、次のコード:Laravel画像
このモデルnamespace App;
use Illuminate\Database\Eloquent\Model;
/**
* @property string path
* @property string name
* @property string alt
* @property string caption
* @property int width
* @property int height
*/
class Image extends Model
{
//
}
からグラブ
//concertscontroller
foreach($concerts as $concert){
$concert->image = Image::find($concert->image_id);
}
-
/**
* @property string name
* @property string address_1
* @property string address_2
* @property string address_3
* @property float(10,6) coordinates
* @property string copy
* @property int imageId
* @property string ticket_link
* @property string info_link
* @property DateTime datetime
*/
class Concerts extends Model
{
/**
* @return \Illuminate\Database\Eloquent\Relations\HasOne
*/
public function image()
{
return $this->hasOne('App\Image','id','imageId');
//I don't think this does anything
}
}
私はかなり前にそれを書きましたが、私は覚えていませんが、hasOneの関係は、私がLaravelファサードを使ってイメージを取得できるようにするべきだと思います自動的に。誰もこれを行う方法を知っていますか?もし私がそれをやっているなら、それは私にもあまり教えてください!
return $ this-> hasOne( 'App \ Image'); ' – GabMic
'は、$ this-> hasOne( 'App \ Image'、 'id'、 'imageId');これはデータベースにも依存し、外部キーの設定も必要です。 – CUGreen