Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Attempt to read property on int

My relations:

Content mode =>

class Content extends Model
{
    use HasFactory;

    protected $table = "contents";

    protected $fillable = [ "body", "camapaign_id" ];

    public function campaigns(){
        return $this->belongsTo(Campaign::class, "campaign_id");
    }
}

My camapaign model =>

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

class Campaign extends Model
{
    use HasFactory;

    protected $table = "campaigns";

    protected $fillable = [ "ringba_campaign_id", "is_active" ];

    public function contents(){
        return $this->hasMany(Content::class, "content_id");
    }
}

Here are my migrations:

content table =>

 public function up()
    {
        Schema::create('contents', function (Blueprint $table) {
            $table->id();
            $table->timestamps();
            $table->string("body", 255);
            $table->foreignIdFor(\App\Models\Campaign::class)->nullable();
        });
    }

Campaign table =>

   public function up()
    {
        Schema::create('campaigns', function (Blueprint $table) {
            $table->id();
            $table->timestamps();
            $table->string("ringba_campaign_id", 255);
            $table->boolean("is_active")->default(0);
        });
    }

Here is my content controller:

 public function index(){
        $contents = Content::all()->sortBy("created_at");
        return view("Dashboard.Contents.contents", [
            "contents" => $contents
        ]);
    }

I am trying to access ringba_camapaign_id in here like this =>

 @foreach($contents as $content) 
  {{ $content->campaign_id->ringba_campaign_id }}
 @endforeach

But I am getting this error : Attempt to read property on int

>Solution :

A couple of things here, since Content BelongsTo a Campaign the method should be singular.

public function campaign(): BelongsTo
{
    return $this->belongsTo(Campaign::class, 'campaign_id');
}

Then when you do $content->campaign_id you are getting the property on the model and so is returning an int. What you want to do is return the campaign model like so $content->campaign through the relationship defined in the Content model. Now you can access the property on the campaign model, $content->campaign->ringba_campaign_id.

However it also looks like a campaign can be nullable by your migration so you would need to add protection from this so you don’t get property on null errors. So this would look like optional($content->campaign)->ringba_campaign_id, this would then return null if the Content didn’t have a Campaign.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading