在Laravel 5.3

时间:2017-01-15 23:38:47

标签: php mysql orm laravel-5.3 entity-relationship

我刚开始使用基于Laravel的项目,我在创建与另一个项目相关的模型实例时遇到了问题。总而言之,我有一个"公司"模型类引用"公司" MySQL表,还有一个" Location"模型类引用"位置"表。两个表都是相关的(公司有很多地点,每个地点都属于一个公司)。到目前为止一切都很好。

我有一个"中间件"机制检查至少一家公司的存在,如果没有公司,我认为这是第一次运行系统,所以我显示"创建公司"控制器/动作,以便用户创建第一家公司。在提交时,这也使用相同的公司信息创建单个位置记录,因此最终数据库中的位置记录应具有" company_id"刚刚创建的公司记录的ID,但这种情况并未发生

让我显示与此问题相关的现有文件和类:

用于创建公司表的迁移文件:

<?php

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

class CreateCompaniesTable extends Migration {
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up() {
        Schema::create('companies', function (Blueprint $table) {
            $table->increments('id');
            $table->string('nit');
            $table->string('name');
            $table->string('contact_name')->nullable();
            $table->string('address')->nullable();
            $table->string('phone')->nullable();
            $table->string('email')->nullable();
            $table->string('website')->nullable();
            $table->timestamps();
            $table->softDeletes();
            $table->integer('created_by')->unsigned()->nullable();
            $table->integer('updated_by')->unsigned()->nullable();
            $table->integer('deleted_by')->unsigned()->nullable();

            $table->foreign('created_by')->references('id')->on('users')
                ->onDelete('cascade');
            $table->foreign('updated_by')->references('id')->on('users')
                ->onDelete('cascade');
            $table->foreign('deleted_by')->references('id')->on('users')
                ->onDelete('cascade');

            $table->index('nit');
            $table->index('name');
            $table->index('created_at');
            $table->index('deleted_at');
        });
    }

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

用于创建位置表的迁移文件:

<?php

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

class CreateLocationsTable extends Migration {
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up() {
        Schema::create('locations', function (Blueprint $table) {
            $table->increments('id');
            $table->integer('company_id')->unsigned()->nullable();
            $table->string('nit');
            $table->string('name');
            $table->string('contact_name')->nullable();
            $table->string('address')->nullable();
            $table->string('phone')->nullable();
            $table->string('email')->nullable();
            $table->string('website')->nullable();
            $table->timestamps();
            $table->softDeletes();
            $table->integer('created_by')->unsigned()->nullable();
            $table->integer('updated_by')->unsigned()->nullable();
            $table->integer('deleted_by')->unsigned()->nullable();

            $table->foreign('created_by')->references('id')->on('users')
                ->onDelete('cascade');
            $table->foreign('updated_by')->references('id')->on('users')
                ->onDelete('cascade');
            $table->foreign('deleted_by')->references('id')->on('users')
                ->onDelete('cascade');

            $table->foreign('company_id')->references('id')->on('companies')
                ->onDelete('cascade');

            $table->index('nit');
            $table->index('name');
            $table->index('created_at');
            $table->index('deleted_at');
        });
    }

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

公司模型类

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;

/**
 * App\Company
 */
class Company extends Model {
    use SoftDeletes;

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'nit', 'name', 'contact_name', 'address', 'phone', 'email', 'website',
    ];

    /**
     * The attributes that should be mutated to dates.
     *
     * @var array
     */
    protected $dates = ['deleted_at'];

    /**
     * Get the users for the company.
     */
    public function users() {
        return $this->hasMany(User::class);
    }

    /**
     * Get the locations for the company.
     */
    public function locations() {
        return $this->hasMany(Location::class);
    }

    /**
     * Get the invoices for the company.
     */
    public function invoices() {
        return $this->hasMany(Invoice::class);
    }

    /**
     * Get the user that created the record.
     */
    public function createdBy() {
        return $this->belongsTo(User::class, 'created_by');
    }

    /**
     * Get the last user that updated the record.
     */
    public function updatedBy() {
        return $this->belongsTo(User::class, 'updated_by');
    }

    /**
     * Get the user that removed the record.
     */
    public function deletedBy() {
        return $this->belongsTo(User::class, 'deleted_by');
    }

    /**
     * Scope a query to only include the first active company.
     *
     * @param \Illuminate\Database\Eloquent\Builder $query
     * @return \Illuminate\Database\Eloquent\Builder
     */
    public function active($query) {
        return $query->orderBy('id')->limit(1);
    }
}

位置模型类:

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;

/**
 * App\Location
 */
class Location extends Model {
    use SoftDeletes;

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'nit', 'name', 'contact_name', 'address', 'phone', 'email', 'website', 'company_id',
    ];

    /**
     * The attributes that should be mutated to dates.
     *
     * @var array
     */
    protected $dates = ['deleted_at'];

    /**
     * Get the company that owns the location.
     */
    public function company() {
        return $this->belongsTo(Company::class);
    }

    /**
     * Get the products for the location.
     */
    public function products() {
        return $this->hasMany(Product::class);
    }

    /**
     * Get the inventory records for the location.
     */
    public function inventories() {
        return $this->hasMany(Inventory::class);
    }

    /**
     * Get the user that created the record.
     */
    public function createdBy() {
        return $this->belongsTo(User::class, 'created_by');
    }

    /**
     * Get the last user that updated the record.
     */
    public function updatedBy() {
        return $this->belongsTo(User::class, 'updated_by');
    }

    /**
     * Get the user that removed the record.
     */
    public function deletedBy() {
        return $this->belongsTo(User::class, 'deleted_by');
    }
}

上述用于检测系统首次运行的中间件:

<?php

namespace App\Http\Middleware;

use App\Company;
use Closure;

class CheckSystemFirstRun {
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request $request
     * @param  \Closure                 $next
     * @return mixed
     */
    public function handle($request, Closure $next) {

        /** @var \Illuminate\Http\Response $response */
        $response = $next($request);

        // The verification must be done AFTER the response has been generated, otherwise the request's route is
        // unknown.
        if ($request->route()->getName() != 'company.create') {

            // Check if there are no active companies.
            if (Company::count() == 0) {
                return redirect(route('company.create'));
            }
        } else {

            // Check if there are active companies.
            if (Company::count() > 0) {
                return redirect(route('dashboard'));
            }
        }

        return $response;
    }
}

允许用户输入第一个公司和位置记录的CompanyController类:

<?php

namespace App\Http\Controllers;

use App\Company;
use App\Http\Requests\AddCompanyRequest;
use Illuminate\Http\Request;

class CompanyController extends Controller {


    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create() {
        return view('company.create');
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param AddCompanyRequest $request
     * @param Company           $company
     * @return \Illuminate\Http\Response
     */
    public function store(AddCompanyRequest $request, Company $company) {
        $company->create($request->all());

        // If there are no locations, create one using the same data as the received to create the company.
        if ($company->locations->count() == 0) {
            $company->locations()->create($request->all());
        }

        return redirect()->route('company.create');
    }
}

指定的Request类,其中还包含公司创建验证:

<?php

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;

class AddCompanyRequest extends FormRequest {
    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize() {
        return true;
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules() {
        return [
            'nit'     => 'required|max:255',
            'name'    => 'required|max:255',
            'email'   => 'required|email|unique:companies|max:255',
            'website' => 'url|max:255',
        ];
    }

}

当数据库是全新的并且我运行系统时,我被重定向到&#34;创建公司&#34;行动。提交时,新的公司记录已成功创建,但创建了预期的位置记录,但没有与公司记录的预期关系(company_id外键列保持为NULL)。

Company record successfully created

Location record created without the expected company_id value

我正在关注the recommendation from Laravel 5.3,所以我不确定我的代码有什么问题。

在发布此问题之前,我发现位置表中的company_id字段可能需要在迁移中定义为可为空;它不像以前那样,但当时Laravel / PHP回应了MySQL的完整性错误,因为&#34; company_id&#34;字段不能为空。我还尝试使用dd()用于在新记录上定义company_id的参数,但返回id值的函数始终返回null。另外,我试过了:

$company->locations()->create($request->all());

$location = new Location($request->all());
$company->locations()->save($location);

都没有成功。

我在Windows 10 x64,PHP 7.0.4上使用MySQL Ver 15.1 Distrib 10.1.10-MariaDB for Win32(AMD64)。

非常感谢任何帮助。感谢。

更新01

以下是执行操作时执行的查询的输出:

----------------------
Query: insert into `companies` (`nit`, `name`, `contact_name`, `address`, `phone`, `email`, `website`, `updated_at`, `created_at`) values (?, ?, ?, ?, ?, ?, ?, ?, ?)
Bindings: array (
  0 => '1113332323-9',
  1 => 'TEST COMPANY INC',
  2 => 'JOHN DOE',
  3 => '1362 36TH PL',
  4 => '8889990099',
  5 => 'test@test.com',
  6 => 'http://test.com',
  7 => '2017-01-16 00:16:25',
  8 => '2017-01-16 00:16:25',
)
Time: 4.5099999999999998

----------------------
Query: select * from `locations` where `locations`.`company_id` is null and `locations`.`company_id` is not null and `locations`.`deleted_at` is null
Bindings: array (
)
Time: 0.48999999999999999

----------------------
Query: insert into `locations` (`nit`, `name`, `contact_name`, `address`, `phone`, `email`, `website`, `company_id`, `updated_at`, `created_at`) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
Bindings: array (
  0 => '1113332323-9',
  1 => 'TEST COMPANY INC',
  2 => 'JOHN DOE',
  3 => '1362 36TH PL',
  4 => '8889990099',
  5 => 'test@test.com',
  6 => 'http://test.com',
  7 => NULL,
  8 => '2017-01-16 00:16:25',
  9 => '2017-01-16 00:16:25',
)
Time: 4.5300000000000002

1 个答案:

答案 0 :(得分:1)

您要将位置添加到Company的ORM实例而不是新创建的记录。

$company->create($request->all());

应该是

$company = $company->create($request->all());