Return data from different tables in a view - Laravel (Solved)

Asked

Viewed 106 times

-1

Problem solving: In this example, I am returning in the user view, data referring to 2 different tables (Intro, About), which can be retrieved from a @foreach

Example: View user.index

@foreach($intros as $intro)
    {{$intro->title_intro}}
    {{$intro->desc_intro}}
@endforeach
@foreach($abouts $about )
    {{$about ->title_about}}
    {{$about ->desc_about}}
@endforeach

User controller:


namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Intro;
use App\About;

class UserController extends Controller
{ public function index() { return view('user.index', [ 'intros' => Intro::paginate(5), 'abouts' => About::paginate(5) ]); } }
  • return view('caminho.nomeDaView')->with(['info1' => model::metodo($dado), 'info2' => model::metodo($dado2) ... ]); , and so on

  • Wait, I still can’t understand... Sorry rs after ->with, this 'info1' would refer to what? And after the model:: this part: method($given) would refer to what? Thanks for the help

  • info1 refers to the name of the array that you will see in the view, the method, is the po... How much you know of object orientation?

  • Basic/Intermediate... only, I don’t have much experience with the Aravel I was confused about which method I would call there... Thus, the information is given that the intro variable is undefined Return view('user.index')->with( [ 'intros' => Intro::index($intro), 'abouts' => About:::index($abouts) ]);

  • 1

    just do it like this: return view('user.index', ['intros' => Intro::paginate[5],'abouts' => About::paginate[5], 'skills' => Skill::paginate[5]]); of course can put all this in a variable and pass there also better including. The method is also valid, but, I think so better (pure opinion)

  • I just tested it here and it worked out that way Virgilio, thank you very much! And Hanania too, for taking a little time to try to help me... :)&#Xa

Show 1 more comment

1 answer

0

Your return must be so:

return view('user.index', 
    [
        'intros' => Intro::paginate(5), 
        'abouts' => About::paginate(5),
        'skills' => Skill::paginate(5)
    ]);

For a better understanding, read about in the documentation of Laravel 5.7 Then, in the view, just access the data desired by the Key of this array.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.