Laravel Category Model Relationships

Miura-shi

I have the following table structure in my database.

Table Name: tiles

Columns: id, tile_name, tile_thumb, tile_snippet

Table Name: tags

Columns: id, tag_title

Table Name: tile_tags

Columns: id, tile_id, tag_id

Models: Tile, Tag, TileTag

In my main model class for entries I am specifying the following relationship to a model called TileTag which is a pivot table.

<?php namespace Tiles;

use Illuminate\Database\Eloquent\Model;

class Tile extends Model {

    protected $table = 'tiles';


   public function tags() {

        return $this->belongsTo('Tiles\TileTag');

    }
}

During my foreach loop it returns the tile_name and any other columns from my table, except the ones joined by the relatipnship.

@foreach($tiles as $tile)       
        <a href="tile/{{$tile->tile_name}}">
            <li class="col-md-4 mix" data-category="{{ $tile->tags->tag_title }}">
                {!! HTML::image($tile->tile_thumb, null, array('class' => 'img-responsive')) !!}
            </li>
        </a>                
@endforeach 

How can I get my categories/tags linked to my primary entries when they are sorted during each loop?

I try returning the data during the loop by {{ $tile->tags->tag_title }} but it returns an empty string.

Controller Method:

class TileController extends Controller {

/**
 * Display a listing of tiles from the database.
 *
 * @return Response
 */

public function index() {

    // Get tile data from the model
    $tiles = \Tiles\Tile::all();

    return view('pages.index', compact('tiles'));

}

Returned Array: enter image description here

geoandri

I think that you don't have to create a model for Tile_Tag. Laravel can handle ManyToMany relationships out of the box (I suppose that this is the type of the relationship because you use pivot table). Your models should be

class Tile extends Model {

protected $table = 'tiles';


 public function tags() {

    return $this->belongsToMany('Tiles\Tag');
 }
}

and

class Tag extends Model {

protected $table = 'tags';


 public function tiles() {

    return $this->belongsToMany('Tiles\Tile');
 }
}

Laravel will know that you have a pivot table named "tag_tile" with columns "tag_id" and "tile_id". Check the relevant documentation here

Then you can iterate through tags collection for each tile like this

@foreach ($tiles as $tile)
         {!!$tile->tile_name!!} 
         @foreach ($tile->tag as $tag)
           {!!$tag->tag_title!!} 
         @endforeach
@endforeach

Hope it helps.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Reverse model relationships in Laravel

From Dev

Laravel model relationships and model events

From Dev

Laravel model relationships Class not found

From Dev

Laravel - dd() model with all relationships

From Dev

Laravel return model relationships in JSON

From Dev

laravel 4 mockery mock model relationships

From Dev

Laravel eloquent: Update A Model And its Relationships

From Dev

Laravel 5.1 testing with model relationships and factories

From Dev

Laravel 4: Model Relationships Not Working (sort of)?

From Dev

laravel 4 mockery mock model relationships

From Dev

How does it works the Laravel 4 model relationships?

From Dev

Laravel 5.1 testing with model relationships and factories

From Dev

Laravel include relationships with model after update

From Dev

Laravel5: How are Eloquent model relationships expressed in the database?

From Dev

Laravel accessing relationships - check if data exists from within the model

From Dev

Caching relationships eager loaded in the model (protected $with) with Laravel and Eloquent

From Dev

How do I traverse through model relationships in laravel with dot syntax

From Dev

Laravel dynamic relationships - access model attributes on eager load

From Dev

Laravel: Is it good practice to use model relationships within a view?

From Dev

Laravel model create relationships twice between two tables

From Dev

Relationships and with in Laravel?

From Dev

Laravel Relationships (With)

From Dev

Load all relationships for a model

From Dev

Phalcon: Model relationships with Namespaces

From Dev

Complex model relationships in django

From Dev

Model relationships in mgo

From Dev

Core data model - relationships

From Dev

Searching model based on relationships

From Dev

Laravel - Database relationships

Related Related

  1. 1

    Reverse model relationships in Laravel

  2. 2

    Laravel model relationships and model events

  3. 3

    Laravel model relationships Class not found

  4. 4

    Laravel - dd() model with all relationships

  5. 5

    Laravel return model relationships in JSON

  6. 6

    laravel 4 mockery mock model relationships

  7. 7

    Laravel eloquent: Update A Model And its Relationships

  8. 8

    Laravel 5.1 testing with model relationships and factories

  9. 9

    Laravel 4: Model Relationships Not Working (sort of)?

  10. 10

    laravel 4 mockery mock model relationships

  11. 11

    How does it works the Laravel 4 model relationships?

  12. 12

    Laravel 5.1 testing with model relationships and factories

  13. 13

    Laravel include relationships with model after update

  14. 14

    Laravel5: How are Eloquent model relationships expressed in the database?

  15. 15

    Laravel accessing relationships - check if data exists from within the model

  16. 16

    Caching relationships eager loaded in the model (protected $with) with Laravel and Eloquent

  17. 17

    How do I traverse through model relationships in laravel with dot syntax

  18. 18

    Laravel dynamic relationships - access model attributes on eager load

  19. 19

    Laravel: Is it good practice to use model relationships within a view?

  20. 20

    Laravel model create relationships twice between two tables

  21. 21

    Relationships and with in Laravel?

  22. 22

    Laravel Relationships (With)

  23. 23

    Load all relationships for a model

  24. 24

    Phalcon: Model relationships with Namespaces

  25. 25

    Complex model relationships in django

  26. 26

    Model relationships in mgo

  27. 27

    Core data model - relationships

  28. 28

    Searching model based on relationships

  29. 29

    Laravel - Database relationships

HotTag

Archive