1
I have a model for my post table, it looks like this:
$post = Post::where('titulo_url', $titulo_url)->first();
//$post->id
//$post->categoria_id
//$post->titulo_url
//$post->titulo
//$post->texto
As you might have imagined, categoria_id is a Relationship with another model/table, the 'categories'
class Post extends Model{
public function categoria(){
return $this->belongsTo('App\Categoria');
}
}
Through the $post->categoria
can return all category data such as $post->categoria->nome
, for example...
I also have the need to have the return of a next post in my post, I got it doing this in the controller:
$post = Post::where('titulo_url', $titulo_url)->first();
$post->proximo = DB::table('posts')->where('id', '>', $retorno->id)->orderBy('id', 'asc')->first();
With that, I get through the $post
, also have return from the next post... Like this:
$post->proximo->id;
$post->proximo->nome;
almost a simulation of kinship.
How to do directly inside the model this "Relationship" with the next post?