skip to Main Content

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 =>

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(AppModelsCampaign::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

3

Answers


  1. Chosen as BEST ANSWER

    Actually I wrote bad relation code inside content model:

    Before I wrote:

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

    Answer will be :

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

    I wrote campaigns instead of campaign.


  2. 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.

    Login or Signup to reply.
  3. try this.

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

    for more information check this link

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search