Operation inside the view in the laravel

懵懂的女人 提交于 2019-12-24 14:20:17

问题


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>

回答1:


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



回答2:


There isn't really any logic that you could put inside the controller here. As long as its loaded with with(), it should be okay to put in the view.

Try this...

{{ $Blog->BlogTag->title }}


来源:https://stackoverflow.com/questions/28374027/operation-inside-the-view-in-the-laravel

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!