Php Laravel:通过id文章获取图像
我需要通过id文章获取图像。换句话说,在仪表板上我有显示标题文章,点击文章后显示内容文章。除了展示图片,一切都很好 我在数据库中有3个表: 用户-这里我有用户id和名称等。 文章-这里我有内容文章(标题、主要内容、日期等) 图片-这里我保存了每篇文章的图片(列:id,image\u id,image) …接下来我有模型、文章、图片和用户 在我的文章中:Php Laravel:通过id文章获取图像,php,laravel,Php,Laravel,我需要通过id文章获取图像。换句话说,在仪表板上我有显示标题文章,点击文章后显示内容文章。除了展示图片,一切都很好 我在数据库中有3个表: 用户-这里我有用户id和名称等。 文章-这里我有内容文章(标题、主要内容、日期等) 图片-这里我保存了每篇文章的图片(列:id,image\u id,image) …接下来我有模型、文章、图片和用户 在我的文章中: <?php use Illuminate\Auth\UserTrait; use Illuminate\Auth\UserInterfa
<?php
use Illuminate\Auth\UserTrait;
use Illuminate\Auth\UserInterface;
use Illuminate\Auth\Reminders\RemindableTrait;
use Illuminate\Auth\Reminders\RemindableInterface;
class Articles extends Eloquent implements UserInterface, RemindableInterface {
use UserTrait, RemindableTrait;
protected $table = 'articles';
public $fillable = array(
'id',
'user_id',
'subject'
);
public function user() {
return $this->belongs_to('User');
}
public function itemsArticle() {
return $this->hasMany('Image', 'id');
}
}
在view.blade中,我使用foreach,如果结果是只显示内容文章而不显示图像。
谢谢你的建议。这里可能不需要所有的类和接口。使用单数名称命名您的模型
class Article extends Eloquent {
public $fillable = array(
'id',
'user_id',
'subject'
);
public function user() {
return $this->belongsTo('User');
}
public function images() {
return $this->hasMany('Image');
}
}
在你的图像模型中
class Image extends Eloquent {
public $fillable = array(
'id',
'image_id',
'image',
);
public function article() {
return $this->belongsTo('Article');
}
}
在您的图像
表中添加字段integerarticle\u id
,然后您将能够通过
$articles = Articles::with('images')->find($id);
return View::make('users.articles')->with('articles', $articles )
如果您想获得image的用户呼叫
$image = Image::find($imageIdHere)->article()->user;
这与问题无关,但为什么要在图像类中使用用户/可分配特征呢?我使用了上一个模型中的模板。(复制…)型号I已更正。;)我执行了所有的步骤,但是结果显示(在imgsrc中列出)整个DB行。(我只需要所有行中的一列,其中是所有路径图像)问题得到解决。在view.blade中,我添加了图像src$images->image(带有foreach)。谢谢你的建议。
$articles = Articles::with('images')->find($id);
return View::make('users.articles')->with('articles', $articles )
$image = Image::find($imageIdHere)->article()->user;