2014-07-21 4 views
-1

ID 문서로 이미지를 가져와야합니다. 즉, 대시 보드에서 제목 표시 기사를 클릭 한 후 기사 표시 내용 기사를 클릭합니다. 쇼 이미지를 제외한 모든 것이 좋습니다.Laravel : ID 문서로 이미지 가져 오기

나는 데이터베이스에 3 개 테이블이 있습니다 사용자 - 나는 여기 사용자 ID와 이름 등 기사가 - 나는 여기 내용의 기사 (제목, 주요 내용, 날짜) 이미지를 가지고 - 여기에 내가 이미지를 저장할 수 있습니다 각 기사에 대한 (컬럼 : 아이디, image_id, 이미지) 내가 모델 기사, 이미지, 기사의 사용자 내가 가지고있는 ..next

: 이미지의

<?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'); 
    } 
} 

내가 가진 :

0123을

내 컨트롤러 : view.blade에서

$images = $this->data->itemsApp(); // here is maybe problem with show images from model Image 
$articles = Articles::find($id); 
return View::make('users.articles') 
->with('images', $images) 
->with('articles', $articles) 

내가 foreach 문을 사용하고 그 결과 인 경우 이미지가없는 내용 만 기사를 보여줍니다. 감사합니다.

+1

이것은 질문과 관련이 없지만 Image 클래스에 User/Reminable 특성을 사용하는 이유는 무엇입니까? – Jono20201

+0

이전 모델의 템플릿으로 사용했습니다. (카피 ..) 모델 나는 이미 바로 잡았다. ;) – TomasM

답변

1

여기에는 모든 Illuminate 클래스와 인터페이스가 필요하지 않을 수도 있습니다. 단수 이름으로 모델의 이름을 지정하십시오.

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'); 
    } 
} 

당신의 images 테이블에 당신이 얻으려면

$articles = Articles::with('images')->find($id); 

return View::make('users.articles')->with('articles', $articles) 

에 의해 필드 정수 article_id, 당신은 모든 데이터를 가져올 수있을 것입니다 추가 이미지 사용자 호출

$image = Image::find($imageIdHere)->article()->user; 
+0

모든 단계를 수행했지만 그 결과가 DB의 전체 행에 표시됩니다 (img src에 나열). (모든 행에서 하나의 열만 있으면 모든 경로 이미지가 필요합니다.) – TomasM

+0

문제가 해결되었습니다. view.blade에서 이미지 src $ images-> image (foreach 사용)를 추가했습니다. 모든 조언을 주셔서 감사합니다. – TomasM