在laravel视图里面操作
Operation inside the view in the laravel
这是我的控制器,我正在从博客模型中获取博客详细信息并将其传递给 BlogData
这是控制器
$BlogData = Blog::where('BlogUrl', $data)->get();
return View::make('blogview')->with('BlogData', $BlogData);
在视图中
@foreach ($Blogs as $Blog)
{{ $Blog->BlogTitle }}
{{ $Blog->BlogTag }}
@endforeach
问题是我的 BlogTag 列为 1,3,4
,这意味着它包括第一个、第二个和第三个标签
如何从标签模型中获取标签的标题
注意:考虑 Tag::
是标签的型号,TagName 是标签的名称,例如 Chrome, Google, Internet
这样的
还建议我是应该在视图中还是在控制器本身中执行这些过程
更新:
这是我的Table图像结构,便于快速理解
这是我的代码:
路线:
Route::get('blog', 'HomeController@Blog');
控制器:
public function BlogView($data=NULL)
{
$BlogData = Blog::with('tags')->where('BlogUrl', $data)->get();
Blog::with('tags')->where('BlogUrl', $data)->get();
return View::make('blogview')->with('BlogData', $BlogData);
}
型号:
博客模型:
<?php
class Blog extends Eloquent
{
protected $table = 'blog';
public function tags(){
return $this->belongsToMany('Tag', 'blog_tag', 'blog_id', 'tag_id');
}
}
标签模型:
<?php
class Tag extends Eloquent
{
protected $table = 'tags';
public function tags(){
return $this->belongsToMany('Tag', 'blog_tag', 'blog_id', 'tag_id');
}
}
我多次更改视图,作为评论的效果,
所以我post查看代码在这里
观看次数:
<div class="jumbotron">
@foreach ($Blogs as $Blog)
<div class="well">
<p>
<a href="<?php echo url();?>/blog/<?php echo $Blog->BlogUrl;?>">{{ $Blog->BlogTitle }}</a>
{{ $Blog->tags()->toSql() }}
@foreach($Blog->tags as $tag)
{{ $tag->tags }}
@endforeach
{{ dd($Blog->tags->toJson()) }}
</p>
<div><h4>
Writted on {{ date_format($Blog->created_at, 'F d o') }} | Tagged {{$Blog->Name}}
</h4>
</div>
</div>
@endforeach
</div>
这里实际上没有任何逻辑可以放入控制器中。只要加载了with()
,放到view里应该就可以了
试试这个...
{{ $Blog->BlogTag->title }}
我强烈建议您将标签存储在数据透视表 table 中,而不是逗号分隔的 ID 列表中。这个新的 table 看起来有点像这样:
blog_tags
----------
id (primary key)
blog_id (foreign key referencing blog table)
tag_id (foreign key referencing tag table)
现在您可以继续定义博客和标签之间的关系:
class Blog extends Eloquent {
public function tags(){
return $this->belongsToMany('Tag');
}
}
根据您是否遵循 Laravel 的命名约定,您可能需要指定主元 table 名称和外键。这将是完全指定的关系:
return $this->belongsToMany('Tag', 'blog_tags', 'blog_id', 'tag_id');
之后您可以通过以下方式访问标签:
$Blog->tags
这将为您提供标签模型的集合。然后你可以遍历它们:
@foreach($Blog->tags as $tag)
{{ $tag->name }}
@endforeach
我建议您也 eager load 使用 with()
的关系。所以你不需要 运行 每个博客的数据库查询来获取它的标签:
$BlogData = Blog::with('tags')->where('BlogUrl', $data)->get();
return View::make('blogview')->with('BlogData', $BlogData);
请务必查看文档中关于 Eloquent 的 relationship 部分。
编辑
现在我明白问题出在哪里了。首先,您不需要该行:(虽然不是问题)
$BlogData = Blog::with('tags')->where('BlogUrl', $data)->get();
// Blog::with('tags')->where('BlogUrl', $data)->get(); <<-- remove that
return View::make('blogview')->with('BlogData', $BlogData);
那么,在 Tag
模型中定义关系 tags()
本身就没有多大意义。你宁愿想要相反的关系:
class Tag extends Eloquent
{
protected $table = 'tags';
public function blogs(){
return $this->belongsToMany('Blog', 'blog_tag', 'tag_id', 'blog_id');
}
}
不过这也不是问题的根源。但这是:
@foreach($Blog->tags as $tag)
{{ $tag->tags }}
@endforeach
而不是 tags
您应该访问标签的 Name
并打印:
@foreach($Blog->tags as $tag)
{{ $tag->Name }}
@endforeach
这是我的控制器,我正在从博客模型中获取博客详细信息并将其传递给 BlogData
这是控制器
$BlogData = Blog::where('BlogUrl', $data)->get();
return View::make('blogview')->with('BlogData', $BlogData);
在视图中
@foreach ($Blogs as $Blog)
{{ $Blog->BlogTitle }}
{{ $Blog->BlogTag }}
@endforeach
问题是我的 BlogTag 列为 1,3,4
,这意味着它包括第一个、第二个和第三个标签
如何从标签模型中获取标签的标题
注意:考虑 Tag::
是标签的型号,TagName 是标签的名称,例如 Chrome, Google, Internet
这样的
还建议我是应该在视图中还是在控制器本身中执行这些过程
更新:
这是我的Table图像结构,便于快速理解
这是我的代码:
路线:
Route::get('blog', 'HomeController@Blog');
控制器:
public function BlogView($data=NULL)
{
$BlogData = Blog::with('tags')->where('BlogUrl', $data)->get();
Blog::with('tags')->where('BlogUrl', $data)->get();
return View::make('blogview')->with('BlogData', $BlogData);
}
型号:
博客模型:
<?php
class Blog extends Eloquent
{
protected $table = 'blog';
public function tags(){
return $this->belongsToMany('Tag', 'blog_tag', 'blog_id', 'tag_id');
}
}
标签模型:
<?php
class Tag extends Eloquent
{
protected $table = 'tags';
public function tags(){
return $this->belongsToMany('Tag', 'blog_tag', 'blog_id', 'tag_id');
}
}
我多次更改视图,作为评论的效果,
所以我post查看代码在这里
观看次数:
<div class="jumbotron">
@foreach ($Blogs as $Blog)
<div class="well">
<p>
<a href="<?php echo url();?>/blog/<?php echo $Blog->BlogUrl;?>">{{ $Blog->BlogTitle }}</a>
{{ $Blog->tags()->toSql() }}
@foreach($Blog->tags as $tag)
{{ $tag->tags }}
@endforeach
{{ dd($Blog->tags->toJson()) }}
</p>
<div><h4>
Writted on {{ date_format($Blog->created_at, 'F d o') }} | Tagged {{$Blog->Name}}
</h4>
</div>
</div>
@endforeach
</div>
这里实际上没有任何逻辑可以放入控制器中。只要加载了with()
,放到view里应该就可以了
试试这个...
{{ $Blog->BlogTag->title }}
我强烈建议您将标签存储在数据透视表 table 中,而不是逗号分隔的 ID 列表中。这个新的 table 看起来有点像这样:
blog_tags
----------
id (primary key)
blog_id (foreign key referencing blog table)
tag_id (foreign key referencing tag table)
现在您可以继续定义博客和标签之间的关系:
class Blog extends Eloquent {
public function tags(){
return $this->belongsToMany('Tag');
}
}
根据您是否遵循 Laravel 的命名约定,您可能需要指定主元 table 名称和外键。这将是完全指定的关系:
return $this->belongsToMany('Tag', 'blog_tags', 'blog_id', 'tag_id');
之后您可以通过以下方式访问标签:
$Blog->tags
这将为您提供标签模型的集合。然后你可以遍历它们:
@foreach($Blog->tags as $tag)
{{ $tag->name }}
@endforeach
我建议您也 eager load 使用 with()
的关系。所以你不需要 运行 每个博客的数据库查询来获取它的标签:
$BlogData = Blog::with('tags')->where('BlogUrl', $data)->get();
return View::make('blogview')->with('BlogData', $BlogData);
请务必查看文档中关于 Eloquent 的 relationship 部分。
编辑
现在我明白问题出在哪里了。首先,您不需要该行:(虽然不是问题)
$BlogData = Blog::with('tags')->where('BlogUrl', $data)->get();
// Blog::with('tags')->where('BlogUrl', $data)->get(); <<-- remove that
return View::make('blogview')->with('BlogData', $BlogData);
那么,在 Tag
模型中定义关系 tags()
本身就没有多大意义。你宁愿想要相反的关系:
class Tag extends Eloquent
{
protected $table = 'tags';
public function blogs(){
return $this->belongsToMany('Blog', 'blog_tag', 'tag_id', 'blog_id');
}
}
不过这也不是问题的根源。但这是:
@foreach($Blog->tags as $tag)
{{ $tag->tags }}
@endforeach
而不是 tags
您应该访问标签的 Name
并打印:
@foreach($Blog->tags as $tag)
{{ $tag->Name }}
@endforeach