Как я могу объединить две таблицы Laravel?
Используя приведенный ниже код, я не вижу никаких ошибок, но на веб-странице, где должно быть электронное письмо?Ничего не отображается.
Все остальное, что берется из таблицы сообщений, корректно, но данные о взаимоотношениях не отображаются.
ViewPostsController.php
use App\Post;
use Illuminate\Support\Facades\DB;
use Illuminate\Http\Request;
class ViewPostsController extends Controller
{
public function posts()
{
$posts = Post::latest()->get();
$email = DB::table('posts')->join('users', 'posts.user_id', '=', 'users.id')->get();
return view('posts', compact('posts', 'email'));
}
}
posts.php
@extends('layouts.app')
@section('content')
<div class="container">
@foreach($posts as $post)
<div class="jumbotron">
<div class="col-4">
<img src="/storage/{{ $post->image }}" class="w-100">
</div>
<p> Kategorija: {{ $post->category }}</p>
<p> Apraksts: {{ $post->description }}</p>
<p> Cena: {{ $post->price }}</p>
<p>e-pasts:{{ $post->email }}</p></div>
@endforeach
</div>
@endsection
Структура базы данных сообщений
Schema::create('posts', function (Blueprint $table) {
$table->bigIncrements('id');
$table->unsignedBigInteger('user_id');
$table->string('category');
$table->string('description');
$table->decimal('price',5,2);
$table->string('image');
$table->timestamps();
$table->index('user_id');
});
Структура базы данных пользователей
Schema::create('users', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('name');
$table->string('email')->unique();
$table->timestamp('email_verified_at')->nullable();
$table->string('password');
$table->rememberToken();
$table->timestamps();
});