Problems obtaining values from a foreach LARAVEL

-2

Entry for characters: Character1, Character2

$personajes = explode(',',$character);
$result = array();

          if (count($personajes)>1) {
//count(personajes) = 2 
            foreach ($personajes as $persona) {

                $obt_id = Tag::where('name', $persona)->get();

                foreach ($obt_id as $id) {
                    //obtengo los id de los personajes
                    echo $id->id;

                    $result[] = FileTags::with('file')->where('tag_id', $id->id)->get();
                }
            }

          } 

The problem when I do echo only returns a single id instead of 2 (1 for each character), therefore my results are only those of a character. I need to be able to get the results of both characters.

EDIT: To explain myself a little better in this result, I bring all the filetags that have the tag_id 1 and 2, as I am using with I bring the data of the files as well.

Then from my table I would get only files 2, 3 and 4.

|tabla filetags|
|  id  | tag_id | file_id |
|  1   |   1    |    2    |
|  2   |   1    |    3    |
|  3   |   2    |    4    |
|  4   |   3    |    1    |

$result = FileTags::with('file')->where('tag_id', $id->id)->get();

TAG model

 public function fileTag(){
      return $this->hasMany('App\FileTags');
    } 

Model Archive

protected $table = 'files';

public function fileTag(){
      return $this->hasMany('App\FileTags');
    }

My FileTags Model

  public function tag(){
      return $this->belongsTo('App\Tag' , 'tag_id');
    }

    public function file(){
      return $this->belongsTo('App\Archive', 'file_id');
    }

I complement the EDIT:

    |table tag|
    | id  |   name      |
    |  1  |  Personaje1 |
    |  2  |  Personaje2 |

I should get the id of Character1 and Character2, that is, 1 and 2, but I'm only getting the id of character 1.

    
asked by Elio 22.07.2016 в 20:14
source

1 answer

0

There are several issues that do not work very well depending on the case and the Laravel documentation.

The models whose relation is hasMany should be plural when defining them:

public function fileTags(){
    return $this->hasMany('App\FileTags');
} 

Taking into account that we are looking for just one id, we should use the first() method instead of get() , since the latter gives us a collection (array):

$obt_id = Tag::where('name', $persona)->first();

If we obtained a single id ( $obt_id ), then we do not need the second foreach:

// foreach ($obt_id as $id) {

     // echo $id->id;


     // esto nos dará una colección
     $result[] = FileTags::with('file')->where('tag_id', $obt_id->id)->get();
// }

In the view, it will be necessary to use foreach to get each element in $result and another foreach to get each element of the result of the query to Filetags :

@foreach ($result as $filetagArray)

    @foreach ($filetagArray as $fileTag)

        {{ $fileTag->file->name }} 

    @endforeach

@endforeach

Apparently you are doing a ManyToMany relationship, for which there are simpler methods, for that you can look in the Laravel documentation: link

    
answered by 23.07.2016 в 01:30