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=AppUser::find(1); $user->notify(new AppNotificationsTaksComplete); } @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 AppHttpControllers; use IlluminateHttpRequest; use IlluminateSupportFacadesAuth; use IlluminateSupportFacadesDB; 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 IlluminateNotificationsNotifiable; use IlluminateDatabaseEloquentModel; 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=AppsslModel::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'); } }
Advertisement
Answer
Fixed my issue.. I just changed my controller to:
public function SSL(){ $data = DB::table('SSL')->where('userID', Auth::id())->get(); $models = AppsslModel::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 AppNotificationsTaksComplete); } } return view('SSL',['data'=>$data]); }