2016-08-30 7 views
0

블로그 사이트의 경우 모든 게시물을 & 페이지에 표시하지만 각 카테고리의 게시물을 별도의 페이지로 표시해야합니다.Laravel - id로 카테고리를 표시

'Javascript'카테고리가 있고 페이지에 자바 스크립트 카테고리가있는 게시물 만 표시되도록하고 싶다고 가정 해 보겠습니다.

올바른 코드는 무엇입니까? 예를 들어, 대담한 'javascript'는 올바른 코드로 대체해야하는 것입니다.

- categoriesController.php ---

public function show($id) 
{ 
$post->withCategories($Categories)->$id->($id as **javascript)** 
} 

javascript.blade.php --- --- (대응도) 예를 들어

<tbody> 
@foreach ($categories as $category->$id>**javascript**) 
<tr> 
<th>{{ $category->id }}</th> 
<td>{{ $category->name }}</td> 
</tr> 
@endforeach 
</tbody> 
</table> 
</div> <!-- end of .col 
+0

업데이트 코드 부분 site.com/posts/javascript와'주석으로 info'을 포함하지 코드. 당신이 정확히 가지고있는 코드를 공유하십시오 – C2486

+0

foreach에'$ category -> $ id' 대신에'$ category'가 있어야 할 수도 있습니다 – C2486

+0

현재 @ - foreach ($ categories로 $ categories) 범주가 아닌 내가 Laravel에 새로운 해요하지만 난 얻을 수보다 더 필요 거기에 뭔가 다른 생각 각각 – n31l

답변

1

: post.php 모델 Post 클래스 모델 확장 { protected $ primaryKey = 'id';

function withCategories() { 
     return $this->hasOne('App\Categories', 'id', 'category_id'); 
    } 

    public function show($id){ 
     Post::with('withCategories')->where('category_id', $id)->get(); //the output of articles of the category 
    } 
} 

$ ID가 URL의 매개 변수 : posts.blade.php에서

<table> 
<tbody> 
@foreach ($posts as $post) 
<tr> 
<th>{{ $post->id }}</th> 
<td>{{ $post->name }}</td> 
<td>{{ $post->withCategories->name }}</td> <!-- Category name--> 
</tr> 
@endforeach 
</tbody> 
</table> 
</div> 
+0

, 당신을 감사 그래서 다음 코드로 category.php 모델을 업데이트 한 후 ' hasMany ('App \ Post'); } withCategories() { return $ this-> hasOne ('App \ Categories', 'id', 'category_id'); } public function show ($ id) { self :: with ('withCategories') -> 여기서 ('category_id', $ id) -> get(); } } } 그게 전부입니까? – n31l

+0

category.php 모델을 업데이트하지 않습니다. article.php 모델에 대한 예제 코드 –

관련 문제