在里面的操作里面的Laravel [英] Operation inside the view in the laravel

查看:141
本文介绍了在里面的操作里面的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 coloumn作为 1,3,4 这意味着它包括第一个,第二个和第三个标签



如何从标签模型中获取标签的标题



注意:考虑 Tag :: 是标签的模型,TagName是标签的名称,例如 Chrome ,Google,Internet 像这样



还建议我是否在视图或控制器本身执行这些过程



更新:



以下是我的桌面结构,以便快速轻松地了解





这是我的代码:



路线:

  Route :: get('blog ','HomeController @ Blog'); 

控制器:

 code> public function BlogView($ data = NULL)
{
$ BlogData = Blog :: with('tags') - > where('BlogUrl',$ data) - >得到();
Blog :: with('tags') - > where('BlogUrl',$ data) - > get();
return View :: make('blogview') - > with('BlogData',$ BlogData);
}

型号:



博客模型:

 <?php 
class Blog扩展Eloquent
{
protected $ table ='blog';
public function tags(){
return $ this-> belongsToMany('Tag','blog_tag','blog_id','tag_id');
}

}

标签模型:

 <?php 
class标签扩展Eloquent
{
protected $ table ='tags';
public function tags(){
return $ this-> belongsToMany('Tag','blog_tag','blog_id','tag_id');
}

}

我多次更改了视图,作为评论的效果,



所以我在这里发布视图代码



views:

 < 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->标签为$标签)
{{$ tag-> tags}}
@endforeach

{{dd($ Blog-> tags-> toJson())}}
< / p>
< div>< h4>
写在{{date_format($ Blog-> created_at,'F d o')}} |标记为{{$ Blog-> Name}}
< / h4>
< / div>
< / div>
@endforeach
< / div>


解决方案

我强烈建议您将标签存储在数据透视表中而不是逗号分隔的id列表。这个新表会看起来像这样:

  blog_tags 
----------

id(主键)
blog_id(外键引用博客表)
tag_id(外键引用标签表)

现在您将继续定义博客和标签之间的关系:

  class Blog扩展Eloquent {
public function tags(){
return $ this-> belongsToMany('Tag');
}
}

根据您是否遵循Laravel的命名约定,您可能需要指定数据透视表名称和外键。这将是完全指定的关系:

  return $ this-> belongsToMany('Tag','blog_tags','blog_id ','tag_id'); 

之后,您可以通过执行以下操作访问标签:

  $ Blog->标签

给你一个标签模型的集合。您可以循环使用它们:

  @foreach($ Blog->标签为$标签)
{ $ tag-> name}}
@endforeach

我建议你也渴望加载使用 with()的关系。因此,您不会为每个博客运行数据库查询,以获取其标签:

  $ BlogData = Blog :: with标签') - > where('BlogUrl',$ data) - > get(); 
return View :: make('blogview') - > with('BlogData',$ BlogData);

请务必查看关系部分在关于雄辩的文档中。



编辑



现在我看到问题是什么。首先,你不需要这一行:(不是问题)

  $ BlogData = Blog :: with('标签') - > where('BlogUrl',$ data) - > get(); 
// Blog :: with('tags') - > where('BlogUrl',$ data) - > get(); << - 删除该
返回View :: make('blogview') - > with('BlogData',$ BlogData);

然后,定义一个关系 tags()标签模型本身没有什么意义。你想要反向关系:

  class Tag extends Eloquent 
{
protected $ table ='tags ;
public function blogs(){
return $ this-> belongsToMany('Blog','blog_tag','tag_id','blog_id');
}
}

但是这也不是问题的根源。但是这是:

  @foreach($ Blog->标签为$标签)
{{$ tag- > tags}}
@endforeach

而不是 / code>您应该访问您的标签的名称并打印:

  @foreach($ Blog->标签为$标签)
{{$ tag-> Name}}
@endforeach
/ pre>

Here is my controller i am taking the blog details from the Blog model and passing it through BlogData

Here is the controller

$BlogData = Blog::where('BlogUrl', $data)->get();
return View::make('blogview')->with('BlogData', $BlogData);

In the view

@foreach ($Blogs as $Blog)
{{ $Blog->BlogTitle }}
{{ $Blog->BlogTag }}
@endforeach

The Question is i have the BlogTag coloumn as 1,3,4 which means it includes First, Second and Third Tag

How can i fetch the Title of the Tag from the Tag Model

Note : Consider Tag:: is the model of the tag and TagName is the Name of the Tags such as Chrome, Google, Internet like that

Also recommend whether shall i do these process in the view or in controller itself

Update :

Here is my Table Structure in image for quick and easy understanding

Here is my Code :

Route :

Route::get('blog', 'HomeController@Blog');

Controller :

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

Model :

Blog Model :

<?php
class Blog extends Eloquent 
{
    protected $table = 'blog';
      public function tags(){
       return $this->belongsToMany('Tag', 'blog_tag', 'blog_id', 'tag_id');
    }

}

Tag Model :

<?php
class Tag extends Eloquent 
{
    protected $table = 'tags';
      public function tags(){
        return $this->belongsToMany('Tag', 'blog_tag', 'blog_id', 'tag_id');
    }

}

I changed the view multiple times, as the effect of the comment,

So i post the view code here

views :

<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>

解决方案

I highly recommend you store the tags in a pivot table instead of a comma separated list of id's. This new table would look somewhat like this:

blog_tags
----------

id       (primary key)
blog_id  (foreign key referencing blog table)
tag_id   (foreign key referencing tag table)

Now you would go ahead and define a relationship between blog and tag:

class Blog extends Eloquent {
    public function tags(){
        return $this->belongsToMany('Tag');
    }
}

Depending on whether you follow Laravel's naming conventions you might need to specify the pivot table name and the foreign keys. This would be the fully specified relationship:

return $this->belongsToMany('Tag', 'blog_tags', 'blog_id', 'tag_id');

After that you can access the tags by doing:

$Blog->tags

This will give you a collection of tag models. You could then loop over them:

@foreach($Blog->tags as $tag)
    {{ $tag->name }}
@endforeach

I suggest you also eager load the relationship using with(). So you don't run a db query for each blog to fetch it's tags:

$BlogData = Blog::with('tags')->where('BlogUrl', $data)->get();
return View::make('blogview')->with('BlogData', $BlogData);

Be sure to check out the relationship section in the docs about Eloquent.

Edit

Now I see what the problem is. First off, you don't need that line: (not the problem though)

$BlogData = Blog::with('tags')->where('BlogUrl', $data)->get();
// Blog::with('tags')->where('BlogUrl', $data)->get();  <<-- remove that
return View::make('blogview')->with('BlogData', $BlogData);

Then, defining a relationship tags() in the Tag model itself doesn't make much sense. You rather want the inverse relationship:

class Tag extends Eloquent 
{
    protected $table = 'tags';
    public function blogs(){
        return $this->belongsToMany('Blog', 'blog_tag', 'tag_id', 'blog_id');
    }
}

However that also is not the root of the problem. But this is:

@foreach($Blog->tags as $tag)
    {{ $tag->tags }}
@endforeach

Instead of tags you should access the Name of your tag and print that:

@foreach($Blog->tags as $tag)
    {{ $tag->Name }}
@endforeach

这篇关于在里面的操作里面的Laravel的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆