• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

GhazaliTajuddin.com

Another Kuantan Blogger

  • Home
  • Blog
    • Kuantan
    • Foods
    • Technology
    • Health
  • Stock Photography
    • Senarai Microstock Agency
    • Membuka akaun contributor Shutterstock.com
    • Tips untuk 10 keping gambar pertama Shutterstock.com
    • Mengapa Shutterstock.com reject gambar / submission
    • Model Release
    • Bagaimana withdraw earning daripada Fotolia
    • Bagaimana untuk mengisi keyword kepada imej dengan menggunakan Adobe Photoshop

relationship

BadMethodCallException Method Illuminate\Database\Eloquent\Collection::roles does not exist.

July 27, 2023 by ghazalitajuddin Leave a Comment

ERROR

BadMethodCallException Method Illuminate\Database\Eloquent\Collection::roles does not exist.
BadMethodCallException Method Illuminate\Database\Eloquent\Collection::permissions does not exist.

The hint here is ‘Collection‘…

If somehow you got this error while you have check all code is ok, relationship is ok, then try to check either you use first() or get() method.

Supposedly you use first() instead of get() method as get() method will return a collection() while first() just return a record.

Tinker

$r = App\Models\Role::where('slug','super-admin')->first()
= App\Models\Role {#7268
    id: 1,
    name: "Super Admin",
    slug: "super-admin",
    created_at: "2023-07-27 01:11:24",
    updated_at: "2023-07-27 01:11:24",
  }

 $u = App\Models\User::where('name','Ghazali Tajuddin')->first()
= App\Models\User {#7278
    id: 1,
    name: "Ghazali Tajuddin",
    email: "ghazali.tajuddin@gmail.com",
    email_verified_at: "2023-07-27 01:11:24",
    #password: "",
    #remember_token: null,
    created_at: "2023-07-27 01:11:24",
    updated_at: null,
  }

Filed Under: Technology Tagged With: error, Framework, laravel, relationship

Laravel Many To Many Relationship

July 22, 2023 by ghazalitajuddin Leave a Comment

Property Class

class Property extends Model
{
    protected $table = 'properties';
    use HasFactory;

    /**
     * Get the Street that owns the Property
     *
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
     */
    public function street(): BelongsTo
    {
        return $this->belongsTo(Street::class);
    }

    /**
     * Get the Neighbourhood that owns the Property
     *
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
     */
    public function neighbourhood(): BelongsTo
    {
        return $this->belongsTo(Neighbourhood::class);
    }
}

Resident Class

class Resident extends Model
{
    use HasFactory;
    protected $guarded = [];
    
    /**
     * The Property that belong to the Resident
     *
     * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
     */
    public function properties(): BelongsToMany
    {
        return $this->belongsToMany(Property::class);
    }
}

Pivot class, PropertyResident class.

class PropertyResident extends Model
{

    use HasFactory;
    protected $fillable = ['property_id', 'resident_id'];

}

Use tinker to test

php artisan tinker 

//To find resident id
$r=App\Models\Resident::find(1)

//To get all resident properties
$r->properties

//To count all resident properties
$r->properties->count()

//To find resident id(1) neighbourhood trough properties (many to many relationship)
$r->properties->find(5)->neighbourhood->name

//To get all resident id(1) property "property_id"
$r->properties()->pluck('property_id')

//To get distinct(*) neighbourhood
$r->properties()->distinct()->pluck('neighbourhood_id')

//To get all neighbourhood id group by neighbourhood id
$r->properties()->groupBy('neighbourhood_id')->pluck('neighbourhood_id')

Filed Under: Kuantan, Kuantan Web Developer, Technology Tagged With: laravel, many to many, many to many relationship, pivot table, relationship, tinker

Kelebihan Laravel Relationship Eloquent

November 5, 2016 by ghazalitajuddin Leave a Comment

Antara sebab aku nak belajar sangat menggunakan framework ni adalah disebabkan oleh ia menyediakan teknik query atau capaian data yang memudahkan sesetengah kerja yang remeh temeh:

Cth

Table

Filem(id, title, content, status_id )
Status(id, nama)

Dalam coding biasa, bila nak papar maklumat filem dan juga status, kita terpaksa buat  query seperti berikut:

select title, content, status
from filem
join status
   on filem.status_id = status.id

Mudah sahaja, tetapi, dalam laravel kita boleh skip, dan menjadikan coding kita lebih kemas. Cuma kena setup dulu relationship between model (belongsTo(), hasMany(), hasOne(), dll).

Lihat sample dibawah.

Model class Filem

public function getStatus()
{
return $this->belongsTo('App\Status', 'status_id');
}
Model class Status

public function Filem()
{
return $this->hasMany('App\Status', 'status_id');
}

Dalam view hanya masukkan kod berikut untuk dapatkan status

{{$parcel->getDistributor->nama}}

 

Filed Under: Technology Tagged With: array, eloquent, laravel, model, object, relationship

Primary Sidebar

“Solat. Sabar. Syukur. Senyum. Sedekah.”

For Collaboration, Events & Review, kindly contact me at +6016[-]9212092 or click Whatsapp button on this page.

Sponsor

Recent Posts

BadMethodCallException Method Illuminate\Database\Eloquent\Collection::roles does not exist.

User Roles And Permissions Without Package Laravel 10

Laravel Many To Many Relationship

Makan malam bersama keluarga di Awangan Palace

Sarapan pagi di Warung Gulai Kawah

Recent Comments

  • helmi on Personal Tips Berhenti Merokok
  • ghazalitajuddin on Personal Tips Berhenti Merokok
  • helmi on Personal Tips Berhenti Merokok
  • ghazalitajuddin on Nasi Lemak Kukus Restoran Zaman. Otai masih berbisa.
  • ghazalitajuddin on Air tangki radiator Proton Exora cepat kering? Cuba tukar penutup radiator!
  • Mal on Nasi Lemak Kukus Restoran Zaman. Otai masih berbisa.
  • Firdaus on Air tangki radiator Proton Exora cepat kering? Cuba tukar penutup radiator!

My Link

  • Takaful Insurance Web

JJCM

Minuman Diet

Day Trip Kuantan – Kuala Gandah – Temerloh – Kuantan

Cendol Terbaik Di Kuantan

Kopi Vietnam di Kuantan Pickers

Makan malam bersama keluarga di Awangan Palace

Tags

bebas rokok berhenti merokok breakfast Controller Framework Gezzeg Photography & Design health jalan-jalan cari makan jalan-jalan cari makan kuantan jjcm jjcm kuantan Jurufoto Kuantan Kuantan Kuantan Photographer kuantan programmer kuantan web developer kuantan webmaster laravel merokok merbahayakan kesihatan model MVC nikmat rokok OOP Pahang Pahangtourism pahang tourism Photo Manipulation PHP rajalanun retired smoking revisit pahang 2018 shutterstock stop smoking stop smoking tips stop smoking withdrawal symptom tips tips berhenti merokok View visit malaysia 2020 visit pahang visitpahang white wordpress yii Yii Framework

Recent Posts

  • BadMethodCallException Method Illuminate\Database\Eloquent\Collection::roles does not exist.
  • User Roles And Permissions Without Package Laravel 10
  • Laravel Many To Many Relationship
  • Makan malam bersama keluarga di Awangan Palace
  • Sarapan pagi di Warung Gulai Kawah

Copyright © 2025 — Ghazali Tajuddin • All rights reserved. •