Back

Laravel 5.7 Eloquent relation problem


UncleGreg posted 3 weeks ago

Im trying to develop app times 5-6 or more.

Now i've an issue in tinker

App\Adres::all()->device
Exception with message 'Property [device] does not exist on this collection instance.'

when i type:

App\Device::first()->adres;
=> App\Adres {#2914
     id: 1,
     order_id: 1,
     ulica: "Dupowata",
     nr_budynku: "23",
     created_at: null,
     updated_at: null,
   }

result is correct. My Adres model:

namespace App;
use Illuminate\Database\Eloquent\Model;
class Adres extends Model
{
    protected $guarded = [];
    protected $touches = ['order','device'];
    public function order()
    {
        return $this->belongsTo(Order::class);
    }
    public function device()
    {
        return $this->hasMany(Device::class);
    }
}

My Device model:

namespace App;
use Illuminate\Database\Eloquent\Model;
class Device extends Model
{
    protected $guarded = [];
    public function adres()
    {
        return $this->belongsTo(Adres::class);
    }
}
UncleGreg replied 3 weeks ago

Ok i serach laravel-news and find solution:

$devices = App\Device::with('adres')->get();
Kosha Shah replied 2 weeks ago

Thanks for sharing this solution. PHP frameworks have several issues which we need to solve.


Sign in to participate in this thread!



We'd like to thank these amazing companies for supporting us