AJth
AJth

Reputation: 53

Laravel: Pass date to a view (display date in the page)

The first view I have is called receipt.blade which should contain a date. The second view called userview.blade has a form such as below: (Its job is to get the email entered by user and show the receipt)

<form method="get" action="{{  route('test') }}">
        <label>Enter the email address:</label><input type="text" name="email">
        <button type="submit">Submit</button>
    </form

The route redirects it to a controller:

public function getUserReceipts(Request $data)
    {
        $email = $data->email;

        $donor = Donor::where('email', $email)->get();
        return view('emails.receipt')->with(compact('donor'));
    }

This functions allows the user to get their receipt (receipt.blade.php file).

I also have another view(payment.blade) where before all the actions on top, a user registers themselves with their name and email address that adds them in the database.

My question is How do I display the date (of when they submitted the form from the payment.blade) in the receipt.blade file?

I read about carbon that does get the date but i don't know how to use it and where the value should be passed. I'm guessing there should be a hidden input with date:now() or something like that in the payment.blade page. So do I need to create a column for date in the database as well. But there is already a created_at column in the database (created by default by laravel) that contains the date and the time.

Also, how would I be able to change the date to day/month/year (As i need it in that format in the receipt.blade).

Would appreciate the help.

EDIT:

    public function thankyoupage(Request $data, Mailer $mailer){

    $hashed_id= hash('sha256', time());

    $donor = Donor::create([

    'first_name'=> $data->first_name,
    'last_name' => $data->last_name,
    'email' => $data->email,
    'video_count' => $video_count,
    'amount_donated' => $data->amount_donated,
    'hashed_id'=>$hashed_id,

    $mailer
        ->to($data->input('email'))
       ->send(new \App\Mail\MyMail(($data->input('first_name')),($data->input('last_name')),
        ($data->input('amount_donated')),($hashed_id)));
return redirect()->action('PagesController@thank1');

    }

When i'm submitting the form i'm creating a donor and sending the mail. It then redirects to a page via the controller that just says 'thank you' and contains no data. However, its also sending an email which contains the receipt.blade which in turns consists the data that is just submitted to the database.Therefore, does it mean I need to do similar stuff to pass the date in the receipt? So it looks like I might need to create a column for date as well. Or is possible to do without doing so?

EDIT 2 My Donor model:

class Donor extends Model
{
    public $table ="donor";
    use Notifiable;

    protected $fillable = [
        'first_name', 'last_name', 'email','video_count', 'amount_donated', 'hashed_id'
    ];

    protected $hidden = [
        'id'
    ];


    public function updateDonor($first_name , $last_name , $email){
        $this->first_name = $first_name;
        $this->last_name = $last_name;
        $this->email = $email;

        $this->save();
    }

}

My mailer MyMail.php:

class MyMail extends Mailable
{
    use Queueable, SerializesModels;

    public $first_name, $last_name, $amount_donated, $hashed_id;
    /**
     * Create a new message instance.
     *
     * @return void
     */
    public function __construct($first_name,$last_name,$amount_donated,$hashed_id)
    {
        $this->first_name = $first_name;
        $this->last_name = $last_name;
        $this->amount_donated = $amount_donated;
        $this->hashed_id = $hashed_id;
    }

    /**
     * Build the message.
     *
     * @return $this
     */
    public function build()
    {
        return $this->from('[email protected]')
            ->view('emails.singlereceipt');
    }

Upvotes: 0

Views: 5825

Answers (2)

Sandeesh
Sandeesh

Reputation: 11906

Your donor object should contain the created_at field. Use this in the receipt like so.

Add this to your view

{{ $donor->created_at->format('d/m/Y') }}

This prints the date in DD/MM/YYYY format. You can change the format however you like, check the doc for more formatting options http://php.net/manual/en/function.date.php

Edit : You really should learn some basics about passing variables. Your second receipt gets its own set of data. So make the following changes.

Add $donor->created_at to the parameters of the mailable in your thankyoupage method

send(new \App\Mail\MyMail(($data->input('first_name')),($data->input('last_name')),
            ($data->input('amount_donated')),($hashed_id), $donor->created_at));

Add a new data variable in your MyMail class.

public $first_name, $last_name, $amount_donated, $hashed_id, $date;

public function __construct($first_name,$last_name,$amount_donated,$hashed_id,$date)
{
    $this->first_name = $first_name;
    $this->last_name = $last_name;
    $this->amount_donated = $amount_donated;
    $this->hashed_id = $hashed_id;
    $this->date = $date;
}

In your singlereceipt blade view, add this.

{{ $date->format('d/m/Y') }}

Upvotes: 1

homelessDevOps
homelessDevOps

Reputation: 20726

Please see this an example..

Lets say you have a controller Action to save the user when form was submitted:

// Save user action

$user = new \App\User();
$user->email = "[email protected]";
$user->save();
$userId = $user->id;

// redirect to next action
return redirect()->action(
    'Controller@confirm, ['id' => $userId]
);

and a other action to show your confirmation with date

// confirm action

$user = \App\User::find($id);
return view('confirm', compact('user');

With this part in your view:

// view

{{ Carbon\Carbon::parse($user->created_at)->format('d.m.Y') }} 

Upvotes: 1

Related Questions