显示具有关系的两个表中的数据

时间:2019-06-24 13:26:45

标签: laravel

如何连接两个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();
            });

3 个答案:

答案 0 :(得分:1)

Laravel中更好的使用关系-https://laravel.com/docs/5.5/eloquent-relationships#one-to-many

这很简单!

答案 1 :(得分:1)

您可以使用雄辩的关系。 在您的Post模型中添加它。

public function user()
{
    return $this->belongsTo('App\User');
}

然后在您的控制器中

$posts = Post::with('user')->latest()->get();

然后在您的视图文件中,您可以使用

@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->user->email }}</p></div>
            @endforeach
        </div>
    @endsection

您可以在此处了解有关关系的更多信息

https://laravel.com/docs/5.8/eloquent-relationships#one-to-one

答案 2 :(得分:0)

假设您的 $ posts = Post :: latest()已经返回了显示所需帖子所需的查询。您只需要加入users表并告诉查询要选择的字段即可。

示例:

$query = Post::latest();
$query->join('users', 'posts.user_id', '=', 'users.id')
    ->select(['posts.*', 'users.email']);
$posts = $query->get();

这样,您的 $ posts 变量应包含具有所需信息的对象数组。

相关问题