Join in Laravel 5.x in two tables?

Asked

Viewed 994 times

3

I have two tables that are already listed on MySQL and use Laravel, I am already persisting the information in the bank. When I record a news, it is already assigned to the author normally (tables autor and news).

When it comes to listing news, I did the following method on NewsControllers

public function listar()
{
        $news= News::all();
        return view('listar')->with('news', $news);
}

There when I want to display them in the views, I do

@foreach ($news $n)
    <tr>
        <td>{{ $n->id}}</td>
        <td>{{ $n->conteudo}}</td>
    </tr>
@endforeach

And you work perfectly.

My question would be, how to display the author’s name in that listing and in that listing method?

[news]

id
titulo
conteudo
autor (FK com id de autores)

[authors]

id
nome
  • I put an answer, but what I needed was to see the two models you quoted in your question, have how to put them?

  • But his model is automatic, in my bag

  • Or what it’s called when my model has just this: <?php namespace App; use Illuminate Database Eloquent Model; class News extends Model { protected $table = 'news'; public $stamptimes = true; }

  • the class you inherit from Eloquent for CRUD (INSERT, UPDATE, DELETE and DELETE) operations, that’s all you have?

  • Yeah, that’s all!

  • what is the class of Authors?

  • I realized that all your questions have answers and that they were not accepted by you, it would be nice @Roberta to do this, read this: https://answall.com/help/privileges/vote-up and https://answall.com/help/why-vote please do not fail to accept the answers if they suit you!

Show 2 more comments

2 answers

3

To use all resources of Eloquent shall add the relationship as follows:

<?php namespace App;     

   use Illuminate\Database\Eloquent\Model; 

   class News extends Model 
   {
        protected $table = 'news'; 
        public $timestamps = true; 
        public function autor()
        {       
            return $this->belongsTo('App\Autores', 'autor', 'id');      
        }
    }

In the method of controller call the relationship with with:

public function listar()
{
    $news= News::with('autor')->get();
    return view('listar')->with('news', $news);
}

And in his view:

@foreach ($news as $n)
    <tr>
        <td>{{$n->id}}</td>
        <td>{{$n->conteudo}}</td>
        <td>{{$n->autor->nome}}</td>
    </tr>
@endforeach
  • If I had no relationship, this form would serve?

  • $news= News::with('author')->get()()?

  • @Roberta this is all about relationships, if you don’t have just put, but as I said in the question comment has how you put both models please?

  • $news= News::with('autor')->get(); Ela se equievale ao método all()? when you’re in Harvard the method is get(), that actually has the same result.

  • What would the view look like? <td>{{ $n->author->name}}</td> ??

  • @Roberta I made another edition, just need the layout of the authors class to know if it is right... if you can add in the question?

  • Call to Undefined method Illuminate Database Query Builder::author()

  • @Roberta I need the layout of the two tables and the classes you made for them to make the example according to your reality, so there may be errors, see for sure if you didn’t see my answer because this method existed in my example ...

Show 3 more comments

3

That’s how it works too:

public function listar()
{
        $news= News::join('autor', 'news.autor', '=', 'autor.id')
            ->select('news.id', 'titulo', 'conteudo', 'nome')
            ->get();
        return view('listar')->with('news', $news);
}

Browser other questions tagged

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