Operation inside the view in the laravel


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

<h2>Update :</h2>

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

<img alt="enter image description here" class="b-lazy" data-src="https://i.stack.imgur.com/92EQI.png" data-original="https://i.stack.imgur.com/92EQI.png" src="https://etrip.eimg.top/images/2019/05/07/timg.gif" />

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

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

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


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 <em>eager load</em> 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 <a href="http://laravel.com/docs/4.2/eloquent#relationships" rel="nofollow">relationship</a> section in the docs about Eloquent.


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


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


  • Reverse scroll direction with AngularJS
  • How to stream zipped file (on the fly) via Play Framework 2.5 in scala?
  • Play Framework won't run inline javascript
  • Cant Run Default ASP.NET Web App In Visual Studio Code for Mac
  • How to run xUnit 2.1.0-beta-* for DNX projects with ReSharper
  • Why shared pointer assignment does 'swap'?
  • Identity Server by leastprivilege doesn't work properly on Azure
  • What does 'this' mean in a c# constructor? [duplicate]
  • Rewrite apply function to use recursion instead
  • how to exclude java sources from a third party jar?
  • Bootstrap Popover showing at wrong place upon zoom in/out or resizing Browser window
  • Does Context/Scoping of a SQLAlchemy Session Require Non-Automatic Object/Attribute Expiration?
  • Plotting Route with Multiple Points in iOS
  • Excel distinct count with conditions
  • ASPNetCore MVC Routing Let Server Handle Specific Route
  • Laravel 4 routing not working due to .htaccess file?
  • Iron Router: How do I send data to the layout?
  • Can I have a variable number of URI parameters or key-value pairs in Laravel 4?
  • How do I remove all but some records based on a threshold?
  • CakePHP ACL tutorial initDB function warnings
  • Admob requires api-13 or later can I not deploy on old API-8 phones?
  • Sencha Touch 2.0 Controller refs attribute not working?
  • bootstrap to use multiple ng-app
  • Windows forms listbox.selecteditem displaying “System.Data.DataRowView” instead of actual value
  • Benchmarking RAM performance - UWP and C#
  • Acquiring multiple attributes from .xml file in c#
  • How to CLICK on IE download dialog box i.e.(Open, Save, Save As…)
  • LevelDB C iterator
  • apache spark aggregate function using min value
  • How can I remove ASP.NET Designer.cs files?
  • unknown Exception android
  • Reading document lines to the user (python)
  • Binding checkboxes to object values in AngularJs
  • How can i traverse a binary tree from right to left in java?
  • failed to connect to specific WiFi in android programmatically
  • java string with new operator and a literal
  • Net Present Value in Excel for Grouped Recurring CF
  • How can I use threading to 'tick' a timer to be accessed by other threads?
  • jQuery Masonry / Isotope and fluid images: Momentary overlap on window resize
  • How to load view controller without button in storyboard?