اذهب إلى المحتوى
  • 0

خطأ في علاقة Eloquent Laravel 8

Mahmoud Alrashidi

السؤال

أتلقى الخطأ التالي في مشروعي الذي أستعمل فيه الإصدار الثامن:

Attempt to read property "created_by" on null

هذا ما يوجد في ملف العرض:

<h6><strong>Created By:</strong> {{ $purchase->user->created_by }}</h6>
<h6><strong>Updated By:</strong> {{ $purchase->user->updated_by }}</h6>

حيث النموذج User:

<?php

namespace App\Models;

use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;

class User extends Authenticatable
{
    use HasFactory, Notifiable;

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'role_id',
        'name',
        'username',
        'email',
        'password',
    ];

    /**
     * The attributes that should be hidden for arrays.
     *
     * @var array
     */
    protected $hidden = [
        'password',
        'remember_token',
    ];

    /**
     * The attributes that should be cast to native types.
     *
     * @var array
     */
    protected $casts = [
        'email_verified_at' => 'datetime',
    ];

    public function role(){
      return $this->belongsTo('App\Models\Role');
    }

    public function scopeAuthors($query){
      return $query->where('role_id',2);
    }

    public function products(){
      return $this->hasMany('App\Models\Product');
    }

    public function purchases(){
      return $this->hasMany('App\Models\Purchase');
    }

    public function invoices(){
      return $this->hasMany('App\Models\Invoice');
    }
}

و النموذج Purchase:

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class Purchase extends Model
{
    use HasFactory;

    public function user(){
      return $this->belongsTo('App\Models\User');
    }

    public function supplier(){
      return $this->belongsTo('App\Models\Supplier');
    }

    public function category(){
      return $this->belongsTo('App\Models\Category');
    }

    public function unit(){
      return $this->belongsTo('App\Models\Unit');
    }

    public function product(){
      return $this->belongsTo('App\Models\Product');
    }
}

ملف التهجير الخاص بالمستخدمين:

public function up()
    {
        Schema::create('users', function (Blueprint $table) {
            $table->id();
            $table->integer('role_id')->default(3);
            $table->string('name');
            $table->string('username')->unique();
            $table->string('email')->unique();
            $table->timestamp('email_verified_at')->nullable();
            $table->string('password');
            $table->string('image')->default('default.png');
            $table->rememberToken();
            $table->timestamps();
        });
    }

ملف التهجير الخاص بالمشتريات:

public function up()
    {
        Schema::create('purchases', function (Blueprint $table) {
            $table->id();
            $table->string('purchase_no');
            $table->date('date');
            $table->double('buying_qty');
            $table->double('unit_price');
            $table->double('buying_price');
            $table->boolean('status')->default('0');
            $table->integer('supplier_id');
            $table->integer('category_id');
            $table->integer('product_id');
            $table->integer('created_by');
            $table->integer('updated_by')->nullable();
            $table->text('description')->nullable();
            $table->timestamps();
        });
    }

ماهو حل المشكلة.

رابط هذا التعليق
شارك على الشبكات الإجتماعية

Recommended Posts

  • 0

السبب في هذه المشكلة أنك خالفت العُرف في تعريفك للمفاتيح الثانوية فلا يوجد مفتاح ثانوي بالإسم user_id في جدول المشتريات و بما أنك خالفت العُرف يجب أن تكون صريح بخصوص المفتاح الثانوي عند إنشائك لعلاقة user في النموذج Purshase و بما أن هناك حقلين created_by و updated_by فهناك حالات يكون فيها المُنشئ ليس هو المُعدل و بالتالي يوجد علاقتين بالشكل التالي:

public function creator(){
	return $this->belongsTo('App\Models\User', 'created_by');
}

public function updater(){
	return $this->belongsTo('App\Models\User', 'updated_by');
}

و في صفحة العرض تقوم بالتالي:

<h6><strong>Created By:</strong> {{ $purchase->creator->name }}</h6>
<h6><strong>Updated By:</strong> {{ $purchase->updater->name ??  'لم يتم التعديل' }}</h6>
رابط هذا التعليق
شارك على الشبكات الإجتماعية

انضم إلى النقاش

يمكنك أن تنشر الآن وتسجل لاحقًا. إذا كان لديك حساب، فسجل الدخول الآن لتنشر باسم حسابك.

زائر
أجب على هذا السؤال...

×   لقد أضفت محتوى بخط أو تنسيق مختلف.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   جرى استعادة المحتوى السابق..   امسح المحرر

×   You cannot paste images directly. Upload or insert images from URL.

  • إعلانات

  • تابعنا على



×
×
  • أضف...