简体   繁体   中英

How to update column in a database with a function in a view using Laravel

I am currently working on a project where if a date is in the past. The user should get an email notifying them there current 'Contract' finished. Now I already build the function where if the date is in the past the users gets the email but the problem is each time the users reloads the page it sends the email again. Now I added a column in my table named email_send what a default value of 0 but it doesn't seem to update from 0 to 1 when the function sends the email?

This is my sslController.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())
        ->join('users', 'users.id', '=', 'SSL.userID')->get();
        return view('SSL',['data'=>$data]);
    }
}

Note that the join is so that the loged in user only sees his own data in the table


This is the sslModel.php:

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class sslModel extends Model
{
    protected $table='SSL';
    protected $fillable = [
         'email_send',
    ];
}

and this is part of my ssl.blade.php:

</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)->first();
                $user->notify(new App\Notifications\TaksComplete);
                $user->update(['email_send' => 1]);
                }
              @endphp
           </td>
     </tr>

I also tried this:

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

$user=App\User::find(1);
$user->notify(new App\Notifications\TaksComplete);                                                            $SSL=App\sslModel::find(1);
$SSL->email_send=1;
$SSL->update();
}

if you are using find() function then don't use first()

$user=App\User::find(1);
$user->email_send=1;
$user->update();

if App\\User is not working then use App\\Models\\User;

$user=User::find(1);
$user->email_send=1;
$user->update();

For update sslModel

$sslModel=App\sslModel::where('userID', Auth::id())->first();
$sslModel->email_send=1;
$sslModel->update();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM