binggle
10 months ago

Property [outlink] does not exist on this collection instance.

Posted 10 months ago by binggle

I have two models , Outlink ( parent ) and OutlinkList ( child )

in Controller

$outlink_list = OutlinkList::where( 'id', $outlink_list_id)
        ->with('outlink')
        ->get();


dd($outlink_list->toArray());

-> it display properly !!!

array:1 [▼
  0 => array:9 [▼
    "id" => 172
    "outlink_id" => 168
    "type" => "drama"
    "title" => "SomeGood outlink"
    "content" => ""
    "href" => "lnk"
    "created_at" => "2019-11-22 11:01:59"
    "updated_at" => "2019-11-22 11:01:59"
    "outlink" => array:9 [▼
      "id" => 168
      "is_notice" => 0
      "type" => "drama"
      "title" => "Title"
      "thumb_small_path" => "/upload/1.jpg"
      "content" => null
      "href" => ""
      "created_at" => "2019-11-22 11:01:57"
      "updated_at" => "2019-11-22 11:01:57"
    ]
  ]
]

We really see that there is 'outlink' property with data.

But If I try this , it goes error

dd($outlink_list->outlink);
Exception
Property [outlink] does not exist on this collection instance.

check my models

in Outlink.php (parent)

use App\Models\OutlinkList;
class Outlink extends Model
{
    function outlink_list(){
        return $this->belongsTo( OutlinkList::class );
    }
}

in OutlinkList.php (child)

use App\Models\Outlink;
class OutlinkList extends Model
{
  function outlink(){
    return $this->belongsTo( Outlink::class);
  }
}

What is the wrong with my code ?

I really can not understand what the problem is .

Please sign in or create an account to participate in this conversation.