Php 显示所有有标签的博客
这是我的继续。我正在学习将联接用于模型,但在使它们工作时遇到了困难 以下是我的表格结构: 这是我的控制器:Php 显示所有有标签的博客,php,mysql,laravel,laravel-4,Php,Mysql,Laravel,Laravel 4,这是我的继续。我正在学习将联接用于模型,但在使它们工作时遇到了困难 以下是我的表格结构: 这是我的控制器: public function Tag($data=NULL) { $BlogData = Tag::with('blogs')->where('Name', $data)->get(); return $last_query = end($BlogData); return View::make('tag')->with('BlogData',
public function Tag($data=NULL)
{
$BlogData = Tag::with('blogs')->where('Name', $data)->get();
return $last_query = end($BlogData);
return View::make('tag')->with('BlogData', $BlogData);
}
这是我的标签型号:
<?php
class Tag extends Eloquent
{
protected $table = 'tags';
public static $rules = array(
'BlogTitle' => array('required'),
'BlogBody' => array('required')
);
protected $fillable = array('BlogTitle', 'BlogBody');
public function blogs(){
return $this->belongsToMany('Blog');
}
}
您可以通过与的特定关系过滤模型,其中有
:
$BlogData = Blog::with('tags')->whereHas('tags', function($q) use $data){
$q->where('Name', $data);
})->get();
呃…你的问题是什么?我已经把问题从你的标题复制到你的问题中;记住,你应该总是在你的问题中问一个问题!
@extends('layout.master')
@section('body')
<div class="jumbotron">
@foreach ($BlogData as $Blog)
Tag :{{$Blog->Name}}
{{$Blog->tags;}}
@endforeach
</div>
@stop
$BlogData = Blog::with('tags')->whereHas('tags', function($q) use $data){
$q->where('Name', $data);
})->get();