mercredi 1 avril 2020

Send verification e-mail, when it is changed by user later in laravel

I am developing a system with users, which must have their e-mail addresses verified. In registration that is OK by almost default laravel function (with used vue auth). However, I can give users an opinion, to change their e-mail address any time.

So I have stored users in my DB, and they have "email" column and "email_verified_at" column. When user is registrated, the verification e-mail is sent, and "email_verified_at" is default set to NULL. This is OK.

When user change his e-mail inside my app, as logged user, I have two problems:

1 How can I set NULL value again to the "email_verified_at"? It is datetime, so when user has his email verified, the date and time is stored there. When he change the e-mail address, I need to set this value again to NULL, how can I do that?

2 How can I force Laravel to send new verification e-mail with verification link? Is here any method or something like this?

I even tried using boot method in my user module, but it's not working either (not giving at least an error)

protected static function boot()
    {
        parent::boot();

        static::updating(function (User $setting) {
            if (in_array('email', $setting->getChanges())) {
                $setting->email_verified_at = null;
                $setting->sendEmailVerificationNotification();
            }
        });
    }

And following is my update function in the profile controller,

 public function update(Request $request, User $setting)
        {

            $changedAttributes = array_diff($request->all(), $setting->getAttributes());

            $validationRules = array_intersect_key([
               'name'      => ['required', 'alpha','min:2', 'max:255'],
               'last_name' => ['required', 'alpha','min:5', 'max:255'],
               'mobile'    => ['required', 'numeric','min:9','regex:/\+(9[976]\d|8[987530]\d|6[987]\d|5[90]\d|42\d|3[875]\d|
               2[98654321]\d|9[8543210]|8[6421]|6[6543210]|5[87654321]|
               4[987654310]|3[9643210]|2[70]|7|1)\d{1,14}$/'],
               'email'     => ['required', 'string', 'email', 'max:255', 'unique:users,email,'.$setting->id.''],
               'propic' => ['required','image','mimes:jpeg,png,jpg,gif,svg','max:2048'],
           ], $changedAttributes);

           if($request->hasFile('propic'))
           {
                $this->validate($request, [
                    'name' => ['required', 'alpha','min:2', 'max:255'],
                    'last_name' => ['required', 'alpha','min:5', 'max:255'],
                    'mobile' => ['required', 'numeric','min:9','regex:/\+(9[976]\d|8[987530]\d|6[987]\d|5[90]\d|42\d|3[875]\d|2[98654321]\d|9[8543210]|8[6421]|6[6543210]|5[87654321]|4[987654310]|3[9643210]|3[70]|7|1)\d{1,14}$/'],
                    'email' => ['required', 'string', 'email', 'max:255', 'unique:users,email,'.$setting->id.''],
                    'propic' => ['required','image','mimes:jpeg,png,jpg,gif,svg','max:2048'],
                ],$request->all());

                $imageName = time().'.'.$request->propic->extension();  
                $request->propic->move(public_path('propics'), $imageName);
                $setting->propic=$imageName;
                $setting->name=$request->input('name');
                $setting->last_name=$request->input('last_name');
                $setting->mobile=$request->input('mobile');
                $setting->email=$request->input('email');
                $setting->update();
                return Redirect::back()->with('success',__('sentence.User updated successfully'));  
           }

           $this->validate($request, $validationRules);

           $setting->update($changedAttributes);

           return Redirect::back()->with('success',__('sentence.User updated successfully'));  

        }


via Chebli Mohamed

Laravel Chunk Upload skipping one chunk

I am using laravel-chuck-upload in combination with dropzone.js. Everything works fine, the chunks are uploading and when they are uploaded the final file will be saved in S3. The problem is that 1 chunk is always missing. Sometimes .8.part is missing, sometimes .7.part is missing (they remain in the chunks directory after uploading). When I upload a video that has a total size of 9.7MB the file in S3 is 8.7MB. That is 1mb missing, the same size as the missing part. All the chunks are 1MB in size.

What could be the problem and how can I fix this?



via Chebli Mohamed

How to get the last record using Eloquent scope?

I have a Posts table structured like this:

enter image description here

Now I'm trying to get the latest post which is obviously the record with id = 2.

I have this in my theme page posts.htm:

[builderList postLatest]
modelClass = "Me\Articles\Models\Posts"
scope = "scopeLatest"
scopeValue = ""
displayColumn = "title"
noRecordsMessage = "No records found"
detailsPage = "-"
detailsUrlParameter = "slug"
pageNumber = ""

in my model Posts I have:

public function scopeLatest($query)
{
  return $query->orderBy('created_at','desc')->first();
}

But this one is returning both records. I also tried using latest()

return $query->latest();

And this one gives me the error:

Maximum function nesting level of '1000' reached, aborting!

Even tried passing parameter like latest('created_at') but got the same error.

Tried dumping dd($query->orderBy('created_at','desc')->first()->toSql()) and got select * from posts where posts.deleted_at is null

  1. Why first() is not limiting to 1 record?
  2. Why latest() is throwing me Maximum error? Is this version specific bug? It seems I cant find documentation related to it. I have Laravel 5.5.48.

I'm not sure now what can I use. Maybe I'm doing something wrong here. I just need to get the latest post.



via Chebli Mohamed

How to stop laravel error(at-rule or selector expected)

Error on public/app/app.css

While i was just working on other stuff i found the error on this page.



via Chebli Mohamed

why my database does not update ? (LARAVEL)

my database does not update while when i do

 dd(request()->has('validated'));

this is what my web.php looks like:

Route::patch('prof/theme/{id}/validated', 'Theme\ThemeController@valide')->name('prof.theme.validated');

this is what my ThemeController.php looks like:

public function valide(Theme $theme) {
        $theme->update([
            'validated' => request()->has('validated')
        ]);
       return back();

    }

this is what my show.blade.php looks like:

<form action="" method="POST">
                        @csrf
                        @method('PATCH')
                             @if ($theme->validated)
                                <button type="submit" class="btn btn-danger text-center" style="width: 350px">
                                            INVALIDER LE THEME
                                </button>
                             @else
                                 <button type="submit" class="btn btn-success text-center" name="validated" id="validated" style="width: 350px">
                                                VALIDER LE THEME
                                 </button>
                             @endif
                         </form> 

this code does not show me any error but it does not produce the expected action. my variable "validated" is a boolean



via Chebli Mohamed

SQLSTATE[42S01]: Base table or view already exists: 1050 Table 'activity_log' already exists

In Connection.php line 664:

SQLSTATE[42S01]: Base table or view already exists: 1050 Table 'activity_log' already exists (SQL: create table activity_log (id int
unsigned not null auto_increment primary key, log_name varchar(255) null, description text not null, subject_id int null, subject_
type
varchar(255) null, causer_id int null, causer_type varchar(255) null, properties text null, created_at timestamp null, upd
ated_at
timestamp null) default character set utf8 collate utf8_unicode_ci)

In PDOStatement.php line 123:

SQLSTATE[42S01]: Base table or view already exists: 1050 Table 'activity_log' already exists

In PDOStatement.php line 121:

SQLSTATE[42S01]: Base table or view already exists: 1050 Table 'activity_log' already exists



via Chebli Mohamed

Getting # query parameters in laravel

I am developing a CRM for one of my client and he needs VoIP integration into his CRM. I've almost achieved what he wanted but there is a small issue left. The API I am using for VoIP integration sends me access_token to my redirect_uri but the issue is it send me that access token in # instead of ? can any one help me to get this # parameter from the uri as I've searched the internet a lot but couldn't find the answer any help would be great. Here is the url I am getting after authorization.

http://localhost:8000/handleresponse#access_token={Authorization_Token}&token_type=Bearer&expires_in=86400&principal={PRINCIPAL}&loa=2

I can get any parameter from the URI but this #access_token is what I need but I can't get that.

I've tried using $request->access_token but it gives me null. The other logic I've thinking was to get the complete url and convert it into an array and then get the access token but when I get the URL it returns only URL

http://localhost:8000/handleresponse

Anyone knows how to handle this thing.



via Chebli Mohamed