G
G
gux2016-01-31 15:32:06
MySQL
gux, 2016-01-31 15:32:06

How to display data from two different tables?

Good afternoon, I need to display in one view from two different tables, but when I try to display from the second table, it gives an error Undefined variable
In the controller, both models are connected

<?php
namespace App\Http\Controllers;

use Auth;
use Morelive\Models\User;
use Morelive\Models\Events;
use Morelive\Models\Live;
use Illuminate\Http\Request;

class LiveController extends Controller
{
    public function getLive()
    {
        $games = Live::all();
        return view('live.index', ['games' => $games]);
        
        $events = Events::all();
        return view('live.index', ['events' => $events]);
    }

In view I display like this
@foreach($games as $game) 
       <small class="pull-right">{{ $game->created_at }}</small>
@endforeach

But when I try to withdraw from the second it gives an error
@foreach ($events as $event)
          <p>{{ $event->match }}</p>
@endforeach

Answer the question

In order to leave comments, you need to log in

3 answer(s)
E
Eugene, 2016-01-31
@gux

public function getLive()
    {
        $games = Live::all();
        $events = Events::all();
        return view('live.index', compact('games', 'events'));
    }

S
Stanislav Pochepko, 2016-01-31
@DJZT

This is not a problem with Laravel. You don't know how to program. Get a programming materiel. There should not be two returns in a function on the same execution branch.

M
Miku Hatsune, 2016-01-31
@Hatsune-Miku

As soon as the function returns something, its work seems to stop)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question