lundi 19 février 2018

Laravel 5.1 Blade (or just plain echo) displaying classes incorrectly

I have the following code in my Laravel (5.1) view. The code in the PHP block decides which class I should apply to the first td element below.

@foreach($datesUnavailable as $dateUnavailable)
    <?php
        $unavailableDate = strtotime($dateUnavailable->unavailable_on);
        $creationDate = strtotime($dateUnavailable->created_at);
        $newformat = date('D, M d, Y',$unavailableDate);

        $createdBefore = ($unavailableDate - $creationDate)/86400;

        if($createdBefore >= 5) {
            $class = "alert alert-success";                                           
        } else if ($createdBefore < 5 && $createdBefore >= 3) {
            $class = "alert alert-warning"; 
        } else {
            $class = "alert alert-danger";
        }
    ?>
    <tr>
        <td class=></td>
        <td></td>
        <td>
            {!! Form::open([
                'method' => 'DELETE',
                'route' => ['delete-unavailability', $alias, $dateUnavailable->uc_key],
                'onsubmit' => 'return submitResult();'
            ]) !!}
                {!! Form::submit('Delete', ['class' => 'btn btn-danger btn-xs']) !!}
            {!! Form::close() !!}
        </td>
    </tr>
@endforeach

However, when the page renders, the classes on the td element show as

<td class="alert" alert-success="">Wed, Feb 28, 2018</td>

That is, I want it to show class="alert alert-success" (based on the logic) but it displays this weird string. If I do a var_dump($class), I see the value of the $class variable as "alert alert-success".

Any idea why this might be happening?



via Chebli Mohamed

jeudi 15 février 2018

How login with Facebook and Google without authentication in Laravel 5.1 using socialite version 2.1?

'facebook' => ['client_id' => 'id',
'client_secret' => 'fgdfgsdfgrtt45453',
'redirect' => 'http://example.com/callback', ],

In route.php

Route::get('/redirect', 'SocialAuthFacebookController@redirect'); Route::get('/callback', 'SocialAuthFacebookController@callback');

And I have add Services/SocialFacebookAccountService.php directory in App. SocialFacebookAccountService.php

namespace App\Services;
use App\SocialFacebookAccount;
use App\User;
use Laravel\Socialite\Contracts\User as ProviderUser;

class SocialFacebookAccountService
{
    public function createOrGetUser(ProviderUser $providerUser)
    {
        $account = SocialFacebookAccount::whereProvider('facebook')
            ->whereProviderUserId($providerUser->getId())
            ->first();
        echo 'Account info : ';

        if ($account) {
            return $account->user;
        } else {

            $account = new SocialFacebookAccount([
                'provider_user_id' => $providerUser->getId(),
                'provider' => 'facebook'
            ]);

            $user = User::whereEmail($providerUser->getEmail())->first();

            if (!$user) {

                $user = User::create([
                    'email' => $providerUser->getEmail(),
                    'name' => $providerUser->getName(),
                    'password' => md5(rand(1,10000)),
                ]);
            }

            $account->user()->associate($user);
            $account->save();

            return $user;
        }
    }
}

Please help , How to get user info in callback.



via Chebli Mohamed

mardi 13 février 2018

Laravel 5.1 - retrieving a polymorphic association with WHERE clause

I'm having trouble retrieving a polymorphic association "where related_object.column_name = x". Let me explain:

I have a number of “pages" in my database, and each page has polymorphic association to different types of “content”. for example :

pages

slug : my-page, id : 1

slug : my-other page, id: 2

music_track table

music_track : id 10, page_id : 1

video table

video : id 20, page_id: 2

image table

image : id 30, page_id: 1

content table

id: 555, contentable_id 10, contentable_type: App\music_track

id: 556, contentable_id 20, contentable_type: App\video

id: 557, contentable_id 30, contentable_type: App\image

I can return all content using Content::all( ), how do I return all content who’s page_id is ‘1’?

I can't seem to use Content::where('page_id', "=", "1"), because page_id is ambiguous, or assumed to be part of the content table. How to a query the RELATED table?



via Chebli Mohamed

Laravel TokenMismatch for one user only, possible reasons

Bugsnag has been reporting a single user keeps receiving a TokenMismatchException throughout the last few weeks on my production Laravel 5.1 environment, no other user has experienced this issue.

Stacktrace

The exception trace

Request Data (Obscured)

Request data

I considered perhaps the session was dying for this particular user, that they spent too long idle on the login page however I noticed sometimes they make 2 or 3 attempts in quick succession.

I'm preparing to contact the user but wish to understand the possibilities of why this exception keeps occurring other than a genuine attack before I do.



via Chebli Mohamed

vendredi 9 février 2018

How to create an error page to a QueryException?

I'm working on a commercial application and I've created Blade files for several kinds of HTTP errors. They are placed in /resources/views/errors and are working fine for authorization (503.blade.php), page not found (404.blade.php), and so on.

I've created files for 400, 403, 404, 500 and 503, until now. The issue is when a QueryException is thrown. In this case, "Whoops, looks like something went wrong." appears.

For example, considering that name cannot be null, when I do something like this, Laravel throws a QueryException:

User::create([
          'name' => null,
          'email' => 'some@email.com'
]);

The exception would be:

QueryException in Connection.php line 651: SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'name' cannot be null (SQL: insert into users (nome, email, updated_at, created_at) values (, some@email.com, 2018-02-09 12:10:50, 2018-02-09 12:10:50))

I don't want "Whoops, looks like something went wrong." to appear to the end user, I want to show a custom page. What kind of error file do I need to create to achieve this behavior?



via Chebli Mohamed

Installing Omnipay/payu with laravel 5.1

I am developing a website using Laravel 5.1 and i need to have a shopping cart set up, i am trying to install Omnipay for this, i have selected three gateways i will be using: Paypal, Stripe and PayU. My require under composer.json looks like this:

"require": {
    ...
    "ignited/laravel-omnipay": "2.*",        
    "omnipay/paypal": "*",
    "omnipay/stripe": "*",
    "omnipay/payu": "*"
},

And i am getting error on the payu part alone, this is the error:

The requested package omnipay/payu * is satisfiable by omnipay/payu[dev-master, 2.0.x-dev] but these conflict with your requirements or minimum-stability.

I read on another post that specifying "prefer-stable": true, and "minimum-stability": "dev" into the config part of the composer.json would fix the issue but its not working for me, any tips?



via Chebli Mohamed

dimanche 4 février 2018

Duplicate Oracle Sequence

I have a problem about using oracle sequence with yajra/laravel-oci8 library (v.5.1) in Laravel 5.1.

My scenario are:

  1. I have a column named trailer_id in table A.
  2. That column contain ID that generated using sequence named trailed_id_seq. Code:

    function postCreate(Request $request) {
        $id = $request->input('id');
    
        DB::beginTransaction();
    
        ....
    
        $running_no_seq = DB::getSequence();
        $trailer_id = $running_no_seq->nextValue('trailer_id_seq')."";
    
        for($i=0; $i < $counter; $i++) {
           // Some process, it can take a while here...
        }
    
        DB::table('A')->where('id',$id)->update([
           'trailer_id' => $trailer_id
        ]);
    
        DB::commit();
    }
    
    

    The property of trailer_id_seq sequence (captured in Navicat apps): enter image description here

  3. This function can be called by many users.

The problem is:

Why the trailer_id column can be duplicate when accessed by different user in different time?

Here some example: (created_by is user ID) enter image description here

My question: How is this possible? How can i fixed it? (I can't make trailer_id column to unique)

I've read yajra oci docs: https://yajrabox.com/docs/laravel-oci8/master/sequence , is the code DB::getSequence()->nextValue('seq_name'); ONLY return next value and not updated the sequence?

Because in oracle docs, nextval is increments the sequence and returns the next value (https://docs.oracle.com/cd/A84870_01/doc/server.816/a76989/ch26.htm#4062).

Please help me. Thank you for any answer!



via Chebli Mohamed