内联-Laravel

时间:2018-09-21 12:36:41

标签: php laravel

我的数据库中有三个表。用户,雇主和工作。

某些用户是发布了一些职位的雇主。

我正在尝试按用户显示作业。 我的代码: 用户模型

public function jobs(){
        return $this->hasManyThrough('App\Employer','App\Job');
    }

路线:

Route::get('/find_user_jobs',function(){
    $user=User::find(1);
    foreach($user->jobs as $job){
        echo $job->created_at."<br>";
    }
});

但是我得到这个错误

 Column not found: 1054 Unknown column 'jobs.user_id' in 'field list' (SQL: select `employers`.*, `jobs`.`user_id` from `employers` inner join `jobs` on `jobs`.`id` = `employers`.`job_id` where `jobs`.`user_id` = 1)

我得到它试图在工作中找到user_id的信息,但这是我想要它执行的操作

我对程序的渴望 当我提供用户ID时,请转到“雇主”表搜索user_id(如果存在),转到“职位”表并搜索“ loyers_id”并返回所有带有“ loyer_id”的职位。

用户迁移

<?php

use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateUsersTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('users', function (Blueprint $table) {
            $table->increments('id');
            $table->string('name');
            $table->string('email')->unique();
            $table->string('password');
            $table->integer('employer_id');
            $table->rememberToken();
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::drop('users');
    }
}

工作迁移

<?php

use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateJobsTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('jobs', function (Blueprint $table) {
            $table->increments('id');
            $table->integer('employer_id');
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::drop('jobs');
    }
}

雇主迁移

<?php

use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateEmployersTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('employers', function (Blueprint $table) {
            $table->increments('id');
            $table->string('company_name');
            $table->integer('user_id');
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::drop('employers');
    }
}

2 个答案:

答案 0 :(得分:1)

由于EmployeeUser的{​​{1}}人,因此您可以创建模型Jobs来扩展App\Employee模型

App\User

并创建这样的Employee类

class Job { public function employee() { return $this->belongsTo(App\Employee::class,'user_id'); } } 模型中,我将Employee属性设置为$table,当我们进行某些查询时,该查询会将目标表设置为users而不是users表,这是Eloquent的默认行为。

employees

您可以直接使用class Employee extends User { protected $table = "users"; public function jobs() { return $this->hasMany(Job::class, 'user_id'); } } 模型并获得Employee

这是对应的迁移

create_user_table

jobs

create_job_table

class CreateUsersTable extends Migration
{

    public function up()
    {
        Schema::create('users', function (Blueprint $table) {
            $table->increments('id');
            $table->string('name');
            $table->string('email')->unique();
            $table->string('password');
            $table->string('company_name')->nullable();
            $table->rememberToken();
            $table->timestamps();
        });
    }

    public function down()
    {
        Schema::drop('users');
    }
}

答案 1 :(得分:1)

关系参数的顺序错误:

public function jobs(){
    return $this->hasManyThrough('App\Job', 'App\Employer');
}