Rachael
Rachael

Reputation: 299

Laravel Joining Tables

I'm not sure if I can explain this right, but it's worth a shot, because I'm really stuck.

What I am trying to do is grab everything from the eventSchedule table that is matching the url id. In eventSchedule table I have created a column called eventSponsors which is a string that has the title to multiple sponsors. I am trying to have the selected eventSchedule pull back only the listed eventSchedule.eventSponosors from the Sponsors table. The issue I am having is that I have multiple sponsors for each eventSchedule and multiple Sponsors being resused in different eventSchedule. So the only common factor I can find between the two table is the sposnors title.

eventScheduleSeeder

array(
        'id' => '1', 
        'eventSponsors' => 'Enterade Hammer_Nutrition Trek_Bicycle_Store Marathon_High Jacksonville_Running_Company Country_Rocks_the_Beach'
      ),

sponsorsSeeder

 array(
            'id' => '7', 
            'title' => 'Hammer_Nutrition', 
            'src' => 'images/sponsors/hammer_logo.png', 
            'alt' => 'Sponsor - Hammer Nutrition', 
            'url' => 'http://www.hammernutrition.com/'
        ),

HTML

@foreach($eventDetails as $info)
 @foreach($eventSponsors as $sponsors)
<? if(strpos($info->eventSponsors, $sponsors->title) !== false){?>
   <p>{{$sponsors->src}}</p>
<?}?>
 @endforeach
@endforeach

The result I am getting back is just the src for the first title, Enterade. I read up on strpos() and I know it only finds the first occurrence or the last occurrence, but I am not sure how to have the string of titles find and pull back everything with the matching title.

Like I said I am not sure if I am explaining what I need right, but I am willing to anwser and explain in greater detail to help try and figure out this issue. I am new to Laravel and could really use the help. Thanks.

Upvotes: 3

Views: 355

Answers (1)

Kylie
Kylie

Reputation: 11749

I'm not sure I really understand your question, or why you aren't using relationships.... But I'm gonna try to answer

Just do something like this...

    $event = EventSchedule::find(1); //get first event

    $sponsors = explode(" ", $event->eventSponsors);

    $eventTitles = array();
    foreach($sponsors as $val){
      $eventTitles[] = Sponsors::where('title','=',$val)->get();
    }

    //Now your eventTitles array has all the associated sponsors for that event.

Or in your view....

     @foreach($event as $val)
     <?php $titles = explode(" ", $val->eventSponsors);
     foreach($titles as $val2){
      $info = Sponsor::where('title','=',$val2)->first();
      echo $info->title;
      echo "<img src='".$info->src."'>";
     }?>

Upvotes: 2

Related Questions