PHP code example of masterix21 / laravel-contacts

1. Go to this page and download the library: Download masterix21/laravel-contacts library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

masterix21 / laravel-contacts example snippets


// config/contacts.php
return [
    'models' => [
        'contact' => \App\Models\Contact::class,
    ],
];

use Illuminate\Database\Eloquent\Model;
use LucaLongo\LaravelContacts\Models\Concerns\HasContacts;

class User extends Model
{
    use HasContacts;
}

$user->contacts()->create([
    'label' => 'Office',
    'email' => '[email protected]',
    'phone' => '+39 02 1234567',
    'website' => 'https://example.com',
]);

$user->contacts()->create([
    'label' => 'Personal',
    'mobile' => '+39 333 1234567',
    'meta' => [
        'preferred_channel' => 'whatsapp',
        'timezone' => 'Europe/Rome',
    ],
]);

$user->contacts;   // every contact
$user->emails;     // only contacts with a non-null email
$user->phones;     // only contacts with a non-null phone
$user->mobiles;    // only contacts with a non-null mobile
$user->websites;   // only contacts with a non-null website

$primaryEmail = $user->emails()->where('label', 'Office')->first()?->email;

$contact = $user->contacts()->first();

$contact->meta['preferred_channel'] = 'email';
$contact->save();
bash
php artisan vendor:publish --tag="laravel-contacts-migrations"
php artisan migrate
bash
php artisan vendor:publish --tag="laravel-contacts-config"