PHP code example of goldnead / statamic-brand-context

1. Go to this page and download the library: Download goldnead/statamic-brand-context 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/ */

    

goldnead / statamic-brand-context example snippets


// config/brand-context.php
'multi_brand' => true,

use Goldnead\BrandContext\Concerns\HasBrand;

class Contact extends Model
{
    use HasBrand; // 

BrandContext::runFor('acme', function () {
    Contact::create([...]); // stamped with the acme brand, isolated from others
});

use Goldnead\BrandContext\Facades\BrandMembers;

BrandMembers::usersOf();            // Statamic users of the current brand
BrandMembers::usersOf('acme');      // …of a named brand
BrandMembers::

$assignees = BrandMembers::usersOf()
    ->filter(fn ($user) => $user->can('view leadhub'))
    ->map(fn ($user) => ['value' => (string) $user->id(), 'label' => $user->email()]);

use Goldnead\BrandContext\Http\Middleware\SetBrandFromRouteValue;

Route::get('/confirm/{token}', ConfirmController::class)
    ->middleware(SetBrandFromRouteValue::class.':'.Subscription::class.',token,token');

BrandContext::runFor($brand, fn () => SendCampaignJob::dispatch($handle));
// …the worker picks it up, and $brand is current inside handle().

$brand->update(['settings' => ['mail' => [
    'from_address' => '[email protected]',
    'from_name'    => 'chorgesucht.de',        // defaults to the brand name
    'mailer'       => 'scaleway_chorgesucht',  // a mailer from config/mail.php
    'locale'       => 'de',                    // the language its mail is written in
]]]);

app(BrandMailer::class)->send($brandId, $to, $toName, $mailable);
app(BrandMailer::class)->sendRaw($brandId, $html, $text, fn ($message, $identity) => …);
app(BrandMailer::class)->maySend($brandId);   // ask before you stamp anything

if (empty($this->from)) {
    $this->from(config('mail.from.address'), config('mail.from.name'));
}

$this->app->bind(
    \Goldnead\BrandContext\Contracts\SenderIdentityResolver::class,
    MyOwnResolver::class,   // resolve(?int $brandId): SenderIdentity
);
bash
php artisan vendor:publish --tag=brand-context-config
bash
php artisan vendor:publish --tag=brand-context-cp --force