Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/242.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php App\Tasks::first()->;项目;在artisan tinker中,无论发生什么情况,都会返回null:(_Php_Laravel_Eloquent_Eloquent Relationship - Fatal编程技术网

Php App\Tasks::first()->;项目;在artisan tinker中,无论发生什么情况,都会返回null:(

Php App\Tasks::first()->;项目;在artisan tinker中,无论发生什么情况,都会返回null:(,php,laravel,eloquent,eloquent-relationship,Php,Laravel,Eloquent,Eloquent Relationship,tinker中的以下代码返回空值,但它应该返回第一个任务链接到的项目 App\Task::first()->projects; 已经尝试在迁移中重命名方法名、列名,尝试退出tinker并重新登录 项目迁移 public function up() { Schema::create('projects', function (Blueprint $table) { $table->bigIncrements('id');

tinker中的以下代码返回空值,但它应该返回第一个任务链接到的项目

App\Task::first()->projects;
已经尝试在迁移中重命名方法名、列名,尝试退出tinker并重新登录

项目迁移

public function up()
    {
        Schema::create('projects', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->text('title');
            $table->string('description');
            $table->timestamps();
        });
    }

public function up()
    {
        Schema::create('tasks', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->unsignedInteger('project_id');
            $table->string('description');
            $table->boolean('completed')->default(false);
            $table->timestamps();
        });
    }
任务迁移

public function up()
    {
        Schema::create('projects', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->text('title');
            $table->string('description');
            $table->timestamps();
        });
    }

public function up()
    {
        Schema::create('tasks', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->unsignedInteger('project_id');
            $table->string('description');
            $table->boolean('completed')->default(false);
            $table->timestamps();
        });
    }
Project.php

use App\Task;

class Project extends Model
{
    protected $fillable = ['title','description'];

    public function tasks(){
      return $this->hasMany(Task::class);
    }
}

Task.php

use App\Project;

class Task extends Model
{
  protected $fillable = [
    'completed'
  ];
  public function projects(){
    return $this->belongsTo(Project::class);
  }
}
如果有人能检查一下这段代码,告诉我我在哪里犯了任何常规的愚蠢的错误(因为我是新来的路由模型绑定),那将是非常有帮助的!

  • 任务属于项目,因此将项目重命名为project,因为它是单数。如果保留项目,请提供列名作为第二个参数:
  • 您的列类型不同,对于项目id,您使用大整数;对于引用,您使用整数,因此:
应该是这样的:

$table->unsignedBigInteger('project_id');

// also good to make the relationship on the Database level:

$table->foreign('project_id')->references('id')->on('projects')->onDelete('cascade');