Laravel关系在PHP Artisan Tinker中返回我为null

时间:2019-12-02 18:48:15

标签: php laravel

我在项目中使用Laravel 5.8。我正在测试Laravel关系,但是在手工艺匠中,当我请求它提供给我一个null模型中与{{1 }}模型,而地址属性在relation模型中。

模型文件:App \ relation

RelationCreatewth

模型文件:App \ relationCreatewth

RelationCreatewth

以下是迁移文件:

迁移文件:2019_12_02_081558_create_relations_table

 <?php

     namespace App;
      use App\RelationCreatewth;
      use Illuminate\Database\Eloquent\Model;

     class relation extends Model
         {
             protected $guarded=[];
             public function Relation()
                {
                   return $this->hasOne('RelationCreatewth','user_id','id');
                 }
       } 

迁移文件:2019_12_02_083259_create_relation_createwths_table

        <?php

    namespace App;

    use Illuminate\Database\Eloquent\Model;
    use App\Relation;
    class relationCreatewth extends Model
    {
        protected $guarded=[];
        public function relWith()
        {
            return $this->belongsTo('App\Relation');
        }
    }

<?php use Illuminate\Support\Facades\Schema; use Illuminate\Database\Schema\Blueprint; use Illuminate\Database\Migrations\Migration; class CreateRelationsTable extends Migration { /** * Run the migrations. * * @return void */ public function up() { Schema::create('relations', function (Blueprint $table) { $table->bigIncrements('id'); $table->string('name'); $table->integer('Roll no'); $table->integer('mobile'); $table->timestamps(); }); } /** * Reverse the migrations. * * @return void */ public function down() { Schema::dropIfExists('relations'); } } 中:

       <?php

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

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

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

0 个答案:

没有答案