代码之家  ›  专栏  ›  技术社区  ›  okunade yusuf

我的一对多关系不起作用,当我尝试在视图中调用属性“senders_name”时,它会引发错误“”尝试获取属性“”

  •  0
  • okunade yusuf  · 技术社区  · 4 年前

    我希望能够将我的相关表的值传递给我的观点,我正在使用一对多关系。此外,我正在尝试在不使用外键的情况下进行连接,我已经被告知将其用于mysql数据库是无效的,它可能会带来错误或什么都不做。 我实际上测试了它,我指的是外键,它不起作用。

    后置模型:

    namespace App\Models;
    
    use Illuminate\Database\Eloquent\Factories\HasFactory;
    use Illuminate\Database\Eloquent\Model;
    
    class Post extends Model
    {
       //Post model
        protected $table = 'posts';
    
        public function staff()
        {
            return $this->belongsTo('App\Models\Staff');
        }
    }
    

    员工模式:

    namespace App\Models;
    
    use Illuminate\Database\Eloquent\Factories\HasFactory;
    use Illuminate\Database\Eloquent\Model;
    
    class Staff extends Model
    {
        protected $table = 'staff';
    
        public function posts()
        {
            return $this->hasMany('App\Models\Post');
        }
    }
    

    创造 posts 表迁移:

    <?php
    
    use Illuminate\Database\Migrations\Migration;
    use Illuminate\Database\Schema\Blueprint;
    use Illuminate\Support\Facades\Schema;
    
    class CreatePostsTable extends Migration
    {
        /**
         * Run the migrations.
         *
         * @return void
         */
        public function up()
        {   //my post table
            Schema::create('posts', function (Blueprint $table) {
                $table->increments('id');
                $table->string('title');
                $table->string('name');
                $table->string('phone_number');
                $table->mediumText('fault_explained');
                $table->string('repair_type');
                $table->string('repair_part');
                $table->integer('repair_total');
                $table->mediumtext('justify');
                $table->string('vendor_name');
                $table->string('vendor_number');
                $table->string('status_option');
                $table->integer('status_value');
                $table->string('vehicle_name');
                $table->string('vehicle_number');
                $table->integer('staff_id')->nullable()->unsigned();
                $table->timestamps();
            });
        }
    
        /**
         * Reverse the migrations.
         *
         * @return void
         */
        public function down()
        {
            Schema::dropIfExists('posts');
        }
    }
    

    创造 staff 表迁移:

     <?php
    
    use Illuminate\Database\Migrations\Migration;
    use Illuminate\Database\Schema\Blueprint;
    use Illuminate\Support\Facades\Schema;
    
    class CreateStaffTable extends Migration
    {
        /**
         * Run the migrations.
         *
         * @return void
         */
        public function up()
        {   //Staff table
            Schema::create('staff', function (Blueprint $table) {
                $table->increments('id');  
                $table->string('senders_name');
                $table->string('senders_number');
                $table->timestamps();
            });
        }
    
        /**
         * Reverse the migrations.
         *
         * @return void
         */
        public function down()
        {
            Schema::dropIfExists('staff');
        }
    }
    

    视图:

    <div class="content-wrapper">
        <main class="st_viewport">
            <div class="border bg-warning">
                <div style="margin-left:14px; margin-top:5px;">
                    <span class="h2 wrapper " > <em><b>SUBJECT:</b> {{ $posts->title }}</span></em>
                </div>
                <div class="" style="margin-left:14px; margin-bottom:5px;">
                    <em><b>Sent on</b> {{ $posts->created_at }}</em> {{ $posts->staff->senders_name}}
                </div>
    
    0 回复  |  直到 4 年前
        1
  •  0
  •   Endeavour    4 年前

    Post 你应该使用的模型-

    public function staff()
        {
            return $this->belongsTo(Staff::class);
        }
    
    

    Staff 你应该使用的模型-

    public function posts()
        {
            return $this->hasMany(Post::class);
        }
    

    在CreatePostsTable迁移中,您需要再添加一行-

    $table->foreign('staff_id')->references('id')->on('staff');
    
        2
  •  -1
  •   Jeremy Caney Abloin    4 年前

    您必须更换 App\Models\Staff 具有 Staff::class 在您的代码中。

    ...
    return $this->belongsTo(Staff::class);
    ...