mardi 28 janvier 2020

Display Laravel Notification (MailMessage) with markdown after sent

I'm saving every email I send to an entity into the database by creating a function storeEmail and make an insert of MailMessage class into EmailMessage model. Everything works fine, and the main goal is to display the message exactly as it was, when the recipient received it and retrieve all the messages I sent as a User, to a page. To be much easier to retrieve a render of each specific Message in foreach loop, I think is better to fetch it from the Model.

This is my Notification class:

class SimpleEmail extends Notification
{
    use Queueable;

    private $link;
    private $user;

    /**
     * Create a new notification instance.
     *
     * @return void
     */
    public function __construct($link)
    {
        $this->link = $link;
        $this->user = Auth::user();
    }

    /**
     * Get the notification's delivery channels.
     *
     * @param  mixed  $notifiable
     * @return array
     */
    public function via($notifiable)
    {
        return ['mail'];
    }

    /**
     * Get the mail representation of the notification.
     *
     * @param  mixed  $notifiable
     * @return \Illuminate\Notifications\Messages\MailMessage
     */
    public function toMail($notifiable)
    {   
        $mail = (new MailMessage)
            ->from($this->user->email, $this->user->name)
            ->subject('My Dummy Subject')
            ->greeting('To: '.$notifiable->email)
            ->action('Action Button', url($this->link))
            ->line('Thank you for reading my message')
            ->salutation('Friendly, '.$this->user->name);

        $this->storeEmail($mail,$notifiable);
        return $mail;
    }

    public function storeEmail($mail,$notifiable){
        $email = new EmailMessage;
        $email->sender_type = 'App\User';
        $email->sender_id = $this->user->id;
        $email->mail = $mail;
        $email->save();
        $notifiable->email_messages()->save($email);
    }
}

Note:

  1. I'm using Illuminate\Notifications\Messages\MailMessage
  2. My class extends Illuminate\Notifications\Notification
  3. I'm saving (new MailMessage) in the $email->mail = $mail;

I tried to dd($email->mail); and I get this:

 ^ array:20 [▼
  "view" => null
  "viewData" => []
  "markdown" => "notifications::email"
  "theme" => null
  "from" => array:2 [▶]
  "replyTo" => []
  "cc" => []
  "bcc" => []
  "attachments" => []
  "rawAttachments" => []
  "priority" => null
  "callbacks" => []
  "level" => "info"
  "subject" => "My Dummy Subject"
  "greeting" => "To: Dohn John"
  "salutation" => "Friendly, Nikolas Diakosavvas"
  "introLines" => array:2 [▶]
  "outroLines" => array:1 [▶]
  "actionText" => "Action Button"
  "actionUrl" => "http://my-example-url.com ▶"

How can I display the Mail Notification, as it was when I sent it ? What is the optimal solution for that ? Thanks, in advance

EDITED

Managed to render MailMessage using this code works :

$email = EmailMessage::first();
return (new \App\Notifications\SimpleEmail('my-link', $email->recipient->assignto))->toMail($email->recipient);

But this is not exactly what I wanted, because every time I need to find:

  1. Which Notification class used on every email so I can render it.
  2. Variables for each Notification class.


via Chebli Mohamed

Show name of fields in message in Input Array Validation in Laravel

I have array of dropdown with the same name what i'm trying to do is to display the actual name of the field when the error is displayed but now it is displaying like

attributes.Size Attributes Fields are required

where size is the title that i'm passing dynamically

but i want actual name which i am passing as a title to an array.

Code:

$validator = Validator::make($request->all(), [
 'attributes.*' => 'required',
 ],[
 'attributes.*.required' => ':attribute Attributes Fields are required.',
 ]);

 {!! Form::select('attributes['.$attr->title.']') !!}


via Chebli Mohamed

How to combine two sql table in laravel 5.8

I am trying to compare two tables to check is user data already exits in another table. I show some posts they talk about whereRaw but details is not enough i was try to run query but i am not getting which i want.

UPDATED I have 2 tables one is for challenges between two people

Challenge Table  id,user_one, user_two, data_one, data_two,winner_id

And my second table is

vote Table id, user_id, battle_id, voted_user

I want to check is user already votes the table or not, If yes skip that challenge table and show remain table data to user.

$challenges = DB::table('challenges')->whereRaw([
            ['challenges.user_one', '!=', $uid],
            ['challenges.id', '!=', 'vote.id'],
        ])->orWhereRaw([
            ['challenges.user_one', '!=', $uid],
            ['challenges.id', '!=', 'vote.id'],
        ])->get();


via Chebli Mohamed

lundi 27 janvier 2020

Insert / update implode comma separated values in laravel

$value4_1=implode(',',$dff_value4).',';
$name5_1=implode(',',$dff_name5).',';
$value5_1=implode(',',$dff_value5).',';
$number1=implode(',',$sr_number).',';

how to write an update query in Laravel controller to update these values in a table on a $number1 basis, named master data, table fields are id, value4, name5, value5_1, number.

while

 print_r($dff_value5_1);

it showing like below

Browsing,All,All,Browsing,313131,754745645,All,Everywhere,9846098460,Everywhere,Particular



via Chebli Mohamed

Wrapping Vue entry point around blade produces a template warning

I have an existing Laravel app that I'm slowly replacing the sections of it into Vue.

Templates should only be responsible for mapping the state to the UI. Avoid placing tags with side-effects in your templates, such as <script>, as they will not be parsed.

Existing Laravel code in the Blade file with <div id="app"></div> wrapped around it for Vue entry point:

<div id="app>
  <div class="wrapper">
     <header>
        // content
     </header>
     @yield('content')
  </div>
</div>

The 'content' yielded is different blade files, depending on the tab clicked in the nav. I am assuming the warning is because the blade files yielded have JS at the bottom of them (eg: <script type="text/javascript"> code here</script> ).

I'm not sure if this warning is meaning that some code is not being run in the blade or not.



via Chebli Mohamed

Correct way to set up this Laravel relationship?

I'm after a bit of logic advice. I am creating a system where users login and register their participation at an activity. They can participate at an activity many times. What is the best way to do this? I want to ensure I can use eloquent with this rather than creating my own functions.

I am imagining...

Users:

id

Activitys:

id
name

Participations:

id
user_id
activity_id
time_at_activity

I want to later be able to do such things as: $user->participations->where('activity_id', 3) for example.

What is the best way to set this up? I had in mind..

User: hasMany->Participations

Activity: belongsTo->Participation

Participation: hasMany->Activitys & belongsTo->User

Does this look correct?



via Chebli Mohamed

Xero oAuth 2 authorisation in laravel

I have a laravel PHP project application. The server creates an invoice on Xero and sends the user an email etc...

I have been using oAuth 1 (Private App - PHP SDK) with no issues, but now need to switch to oAuth 2. As there is no front end-user on the server, can this still be accomplished?

All the documentation I read, seems to need a manual login to grant authorization and get an access token?

Thanks in advance :-)



via Chebli Mohamed