• 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

pivot table

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

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

Rojak Pak Lah Taman Gelora

Demong Cafe Kampung Raja Besut

Makan malam bersama keluarga di Awangan Palace

Kuih Keria Viral di Kuantan???

Patin Tempoyak Frozen Resepi Temerloh

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