Ad

Laravel How To Add A Function From A View To The Controller (Business Logic Layer)

- 1 answer

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 which 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 doesn't 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');
    }
}
Ad

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]);
    }

Ad
source: stackoverflow.com
Ad