Home » Php » php – Laravel how to add a function from a view to the controller (Business Logic Layer)

php – Laravel how to add a function from a view to the controller (Business Logic Layer)

Posted by: admin February 25, 2020 Leave a comment

Questions:

I currently have this function in my view:

</tr>
  @foreach ($data as $i )
     <tr>
       <td>
         {{$i->AanvraagID}}
       </td>
       <td>
         {{$i->Status}}
       </td>
       <td>
         {{$i->StartDatum}}
       </td>
       <td>
         {{$i->EindDatum}}
       </td>
       <td>
         {{$i->Leverancier}}
       </td>
       <td>
         {{$i->Product}}
       </td>
       <td>
         {{$i->Validatie}}
       </td>
       <td>
         {{$i->TypeCertificaat}}
       </td>
       <td class="wrong">
            @php  
                $date = new DateTime($i->EindDatum);
                $now = new DateTime();

                if($date < $now)  echo  'Certificaat verlopen';

                if ($date < $now && $i->email_send == 0) {

                $user=App\User::find(1);
                $user->notify(new App\Notifications\TaksComplete);

                }
       @endphp
   </td>
</tr>

I would like to know if its possible to add this to my controller so my view only has to display the result.

This is my controller:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\DB;

class sslController extends Controller
{
    function SSL(){
        $data = DB::table('SSL')->where('userID', Auth::id())->get();
        return view('SSL',['data'=>$data]);
    }
}

this is my model:

<?php

namespace App;
use Illuminate\Notifications\Notifiable;
use Illuminate\Database\Eloquent\Model;

class sslModel extends Model
{
    use Notifiable;

    protected $table='SSL';
    protected $fillable = [
         'email_send',
    ];
}

Now the problem is the controller doesn’t know stuff like DateTime()

What the function does is if the users has something wich is past the current date it will send an email to the user. But now each time the user reloads the page it send the email again. This function is there to stop that but it doesnt work in my view:

$SSL=App\sslModel::find(1);
$SSL->email_send = 1;
$SSL->save();

This does not change the column email_send to 1 from 0 in the table SSL in my database.

Is it possible to add this code to my controller(or model)? and so that the column does update in the database?

my user table migration:

class CreateUsersTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('users', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->string('name');
            $table->string('email')->unique();
            $table->timestamp('email_verified_at')->nullable();
            $table->string('password');
            $table->rememberToken();
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('users');
    }
}

How to&Answers:

Define a relationship to your User in your SslModel:

<?php

namespace App;
use Illuminate\Notifications\Notifiable;
use Illuminate\Database\Eloquent\Model;
use App\User;

class sslModel extends Model
{
    use Notifiable;

    protected $table='SSL';
    protected $fillable = [
         'email_send',
         'userID'
    ];

    protected $hidden = ['userID'];

    public function user() {
        return $this->belongsTo(User::class, "userID", "id");
    }
}

See: laravel.com/docs/6.x/eloquent-relationships#updating-belongs-to-relationships

Write your loop in your controller

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\DB;
use Carbon\Carbon;

class sslController extends Controller
{
    public function SSL(){

        $models = \App\sslModel::where('userID', Auth::id())->get();

        foreach($models as $model) { 
             if ($model->EindDatum < Carbon::now() && $model->email_send == 0) {
                $model->update(['email_send' => 1]);
                $model->user()->first()->notify(new \App\Notifications\TaksComplete);
             }
        }


        return view('SSL',['data'=>$model]);
    }
}

The view:

</tr>
  @foreach ($data as $i )
     <tr>
       <td>
         {{$i->AanvraagID}}
       </td>
       <td>
         {{$i->Status}}
       </td>
       <td>
         {{$i->StartDatum}}
       </td>
       <td>
         {{$i->EindDatum}}
       </td>
       <td>
         {{$i->Leverancier}}
       </td>
       <td>
         {{$i->Product}}
       </td>
       <td>
         {{$i->Validatie}}
       </td>
       <td>
         {{$i->TypeCertificaat}}
       </td>
       <td class="wrong">
          @if($i->EindDatum < \Carbon::now())
              {{'Certificaat verlopen'}}
          @else
            {{'OK'}}
           @endif
       </td>
</tr>

See: laravel.com/docs/master/blade#if-statements

[EDIT 1]

Or if you want to do some background processing checkout Laravel Scheduler:
laravel.com/docs/master/scheduling

Answer:

Fixed my issue..
I just changed my controller to:

public function SSL(){

        $data = DB::table('SSL')->where('userID', Auth::id())->get();
        $models = \App\sslModel::where('userID', Auth::id())->get();

        foreach($models as $model) { 
             if ($model->EindDatum < Carbon::now() && $model->email_send == 0) {
                DB::table('SSL')
                ->where('userID', Auth::id())
                ->where('EindDatum', '<', Carbon::now())
                ->update(['email_send' => 1]);
                $model->user()->first()->notify(new \App\Notifications\TaksComplete);
             }
        }


        return view('SSL',['data'=>$data]);
    }