Debug School

rakesh kumar
rakesh kumar

Posted on • Updated on

How to send email in multiple client in laravel

How to send email in multiple client
Difference between pluck and toArray()
Difference between toArray() and tojson
Difference between pluck,toArray(),json_encode() and json_decode()
Way to take for loop from laravel collection like pluck
How to display json data in key value pair format

How to send email in multiple client

Step 1:get the collection of records

$pay_users = addcart::where('admin_id', $param3)->get();
Enter fullscreen mode Exit fullscreen mode

step2: store the array of 'influencer_admin_id' values from the records in the $pay_users collection.

$influencer_adminids = $pay_users->pluck('influencer_admin_id')->toArray();
  $influencer_email = $pay_users->pluck('influencer_email')->toArray();
Enter fullscreen mode Exit fullscreen mode

output

$influencer_email  = ['abc@gmail.com', 'roshan.cotocus@gmail.com'];
Enter fullscreen mode Exit fullscreen mode

or

[2023-10-31 05:24:30] local.INFO: array (
  0 => 'abc@gmail.com',
  1 => 'roshan.cotocus@gmail.com',
)  
Enter fullscreen mode Exit fullscreen mode

step4: iterates over an array of influencer email addresses
iterates over an array of influencer email addresses
declare associative array and put request data
pass this array inside modal with sender address

 foreach ($influencer_email as $email) {
        $influencernamedata = addcart::where('influencer_email', $email)->first();
        log::info($influencernamedata);
        log::info($email);
        $influencername=$influencernamedata->slug;
        log::info($influencername);
        $InfluencerMailsend = array(
            'name' =>$influencername,
            'email' => $email,
            'token' => $validToken,
        );
        log::info($InfluencerMailsend);
    Mail::to($InfluencerMailsend['email'])->send(new InfluencerMail($InfluencerMailsend, $validToken));
        log::info("done");

       }
Enter fullscreen mode Exit fullscreen mode

Note: Different way to declare associative array

$InfluencerMailsend = array(
    'name' => $influencername,
    'email' => $email,
    'token' => $validToken,
);
Enter fullscreen mode Exit fullscreen mode
$InfluencerMailsend = [];
  $InfluencerMailsend = [
        'name' => $influencername,
        'email' => $email,
        'token' => $validToken,
    ];
Enter fullscreen mode Exit fullscreen mode
$InfluencerMailsend = [];
  $InfluencerMailsend['name'] = $influencername;
$InfluencerMailsend['email'] = $email;
$InfluencerMailsend['token'] = $validToken;
Enter fullscreen mode Exit fullscreen mode
class MailSender {
    protected $InfluencerMailsend;

    public function sendInfluencerMail($influencername, $email, $validToken) {
        $this->InfluencerMailsend = [
            'name' => $influencername,
            'email' => $email,
            'token' => $validToken,
        ];

        // Rest of the code...
    }
}
Enter fullscreen mode Exit fullscreen mode

step5: in modal pass array

import use App\Mail\InfluencerMail;
Enter fullscreen mode Exit fullscreen mode
 public function __construct($InfluencerMailsend,$validToken)
    {
        $this->InfluencerMailsend = $InfluencerMailsend;
        $this->validToken = $validToken;
    }

    /**
     * Build the message.
     *
     * @return $this
     */
    public function build()  
    {
        return $this
        ->from('contact@wizbrand.com')
        ->subject('Welcome to WizBrand.com | Verification Email for sign-up!')
        ->view('emails.influencermail')
        ->with('data',$this->InfluencerMailsend,$this->validToken);
    }
Enter fullscreen mode Exit fullscreen mode

Difference between pluck and toArray()

$influencer_adminids = $pay_users->pluck('influencer_admin_id')->toArray();
Enter fullscreen mode Exit fullscreen mode

Output:

[101, 102, 103]
Enter fullscreen mode Exit fullscreen mode

Here, pluck('influencer_admin_id') extracts the 'influencer_admin_id' values from the collection, and toArray() converts the resulting collection to a plain PHP array.

$influencer_adminids = $pay_users->pluck('influencer_admin_id');
Enter fullscreen mode Exit fullscreen mode

Output (if you directly print or use the collection):

Illuminate\Support\Collection Object (
    [items:protected] => Array (
        [0] => 101
        [1] => 102
        [2] => 103
    )
)
Enter fullscreen mode Exit fullscreen mode

Here, pluck('influencer_admin_id') returns a Laravel collection with the 'influencer_admin_id' values.

In summary, the difference is in the data type of the output:

With ->toArray(), you get a plain PHP array.
Without ->toArray(), you get a Laravel collection.
Enter fullscreen mode Exit fullscreen mode

Difference between pluck,toArray(),json_encode() and json_decode()

Let's compare the outputs of $influencer_adminids = $pay_users->pluck('influencer_admin_id')->toArray() and $influencer_adminids = $pay_users->pluck('influencer_admin_id') in combination with json_encode and json_decode. We'll use the same example as before:

$pay_users = collect([
    ['id' => 1, 'influencer_admin_id' => 101],
    ['id' => 2, 'influencer_admin_id' => 102],
    ['id' => 3, 'influencer_admin_id' => 103],
]);
$influencer_adminids = $pay_users->pluck('influencer_admin_id')->toArray();
Enter fullscreen mode Exit fullscreen mode

Output:

[101, 102, 103]
Enter fullscreen mode Exit fullscreen mode

Now, let's use json_encode on the resulting array:

$json_encoded = json_encode($influencer_adminids);
Enter fullscreen mode Exit fullscreen mode

Output of $json_encoded:

[101,102,103]
Enter fullscreen mode Exit fullscreen mode

Now, if you want to decode it back into an array:

$decoded_array = json_decode($json_encoded, true);
Enter fullscreen mode Exit fullscreen mode

Output of $decoded_array:

[101, 102, 103]
$influencer_adminids = $pay_users->pluck('influencer_admin_id');
Enter fullscreen mode Exit fullscreen mode

Output (if you directly print or use the collection):

Illuminate\Support\Collection Object (
    [items:protected] => Array (
        [0] => 101
        [1] => 102
        [2] => 103
    )
)
Enter fullscreen mode Exit fullscreen mode

Now, if you want to convert the collection to an array and then use json_encode:

$json_encoded = json_encode($influencer_adminids->toArray());
Enter fullscreen mode Exit fullscreen mode

Output of $json_encoded:

[101,102,103]
Enter fullscreen mode Exit fullscreen mode

To decode it back into an array:

$decoded_array = json_decode($json_encoded, true);
Enter fullscreen mode Exit fullscreen mode

Output of $decoded_array:

[101, 102, 103]
Enter fullscreen mode Exit fullscreen mode

In summary, both approaches will result in the same JSON representation when using json_encode. The difference lies in the data type before encoding (toArray() converts the collection to a plain PHP array). When decoding with json_decode, you get an array in both cases. Using json_encode and json_decode can be useful when you need to serialize and deserialize data for storage or transportation purposes.

Way to take for loop from laravel collection like pluck

If $influencer_adminids is a Laravel collection resulting from $pay_users->pluck('influencer_admin_id'), you can use a foreach loop to iterate over its elements. Here's an example:

$pay_users = collect([
    ['id' => 1, 'influencer_admin_id' => 101],
    ['id' => 2, 'influencer_admin_id' => 102],
    ['id' => 3, 'influencer_admin_id' => 103],
]);
Enter fullscreen mode Exit fullscreen mode
$influencer_adminids = $pay_users->pluck('influencer_admin_id');
Enter fullscreen mode Exit fullscreen mode
foreach ($influencer_adminids as $adminId) {
    // Your logic for each $adminId goes here
    echo $adminId . PHP_EOL;
}
Enter fullscreen mode Exit fullscreen mode

In this example, the foreach loop iterates over each element in the $influencer_adminids collection, and the variable $adminId represents the current element in each iteration. You can replace the echo statement with the logic you want to apply for each influencer admin ID.

If you want to use the index along with the value in the loop, you can use the foreach loop with the key and value syntax:

foreach ($influencer_adminids as $index => $adminId) {
    // Your logic for each $adminId and its $index goes here
    echo "Index: $index, Admin ID: $adminId" . PHP_EOL;
}
Enter fullscreen mode Exit fullscreen mode

This way, $index will represent the array key (index), and $adminId will represent the value in each iteration. Adjust your loop logic based on what you need to do with each influencer admin ID.

Difference between toArray() and tojson

In the context of Laravel collections, the toArray and toJSON methods serve different purposes.

Let's consider your example:

$influencer_email_array = $pay_users->pluck('influencer_email')->toArray();
$influencer_email_json = $pay_users->pluck('influencer_email')->toJSON();
Enter fullscreen mode Exit fullscreen mode

toArray() Method:

Purpose: The toArray method is used to convert a collection into a plain PHP array.
Example:

$influencer_email_array = $pay_users->pluck('influencer_email')->toArray();
Enter fullscreen mode Exit fullscreen mode

Output:

['john@example.com', 'jane@example.com']
Enter fullscreen mode Exit fullscreen mode

toJSON() Method:

Purpose: The toJSON method is used to convert a collection into a JSON representation (a JSON-encoded string).
Example:

$influencer_email_json = $pay_users->pluck('influencer_email')->toJSON();
Enter fullscreen mode Exit fullscreen mode

Output:

["john@example.com","jane@example.com"]
Enter fullscreen mode Exit fullscreen mode

This string represents a JSON array containing the influencer email values extracted from the original $pay_users collection. Each email is a separate string element in the array.

If you want to use this JSON-encoded string later, you can decode it back to a PHP array using json_decode:

$influencer_email_array = json_decode($influencer_email);
Enter fullscreen mode Exit fullscreen mode

Summary:

toArray() produces a plain PHP array.
toJSON() produces a JSON-encoded string.
Enter fullscreen mode Exit fullscreen mode

In your example, if you use toArray, you get a PHP array, and if you use toJSON, you get a JSON-encoded string. Choose the method that best suits your needs based on whether you want a PHP array or a JSON string representation of your data.

How to display json data in key value pair format

<h1>{{ $user->name }}</h1>

<p>Email: {{ $user->email }}</p>

@if ($user->json_data)
    <h2>JSON Data:</h2>
    <ul>
        @foreach(json_decode($user->json_data, true) as $key => $value)
            <li>{{ $key }}: {{ $value }}</li>
        @endforeach
    </ul>
@endif
Enter fullscreen mode Exit fullscreen mode

output

<h1>User's Name</h1>
<p>Email: user@example.com</p>

<h2>JSON Data:</h2>
<ul>
    <li>key1: value1</li>
    <li>key2: value2</li>
    <!-- ... more key-value pairs if present in the JSON data ... -->
</ul>
Enter fullscreen mode Exit fullscreen mode

other way

<table>
    <tr>
        <th>Name</th>
        <td>{{ $user->name }}</td>
    </tr>
    <tr>
        <th>Email</th>
        <td>{{ $user->email }}</td>
    </tr>
    @if ($user->json_data)
        <tr>
            <th colspan="2">JSON Data</th>
        </tr>
        @foreach(json_decode($user->json_data, true) as $key => $value)
            <tr>
                <td>{{ $key }}</td>
                <td>{{ $value }}</td>
            </tr>
        @endforeach
    @endif
</table>
Enter fullscreen mode Exit fullscreen mode

Output

Image description

Top comments (0)