mshakeel

Experience

1,360

0 Best Reply Awards

  • Member Since 3 Years Ago
  • 6 Lessons Completed
  • 0 Favorites

12th January, 2016

mshakeel started a new conversation Query Builder: Union Within Closure Giving Syntax Error - Extra Parenthesis • 2 years ago

Following is query which uses union within closure:

protected function test()
{
    // Get All skill level metadata ids Query
    $skillMetaDataQuery = \DB::table('metadata_field_options')
        ->select('id')
        ->where('metadata_field_id', '=', function ($query) {
            $query->select('id')
                ->from('metadata_fields')
                ->where('field', '=', 'skill_level');
        });
    $filterCallback = function ($query) use ($skillMetaDataQuery) {
            $query->select('metadata_field_option_id')
                ->from('profile_extended')
                ->where('profile_id', 1)
                ->union($skillMetaDataQuery);
        };

    $users = User::where('users.id', '<>', 1)
        ->join('profiles as p', 'users.id', '=', 'p.user_id')
        ->join('profile_extended as pe', 'pe.profile_id', '=', 'p.id')
        ->whereIn('pe.metadata_field_option_id', $filterCallback)
        ->get();

    return $users;
}

It gives syntax error near join which is because of the parenthesis added with each union query, it is fixed when we made a slight change in framework file. Following is the change:

/src/Illuminate/Database/Query/Grammars/MySqlGrammar.php change from

if ($query->unions) {
        $sql = '('.$sql.') '.$this->compileUnions($query);
    }

to

if ($query->unions) {
        $sql = $sql.' '.$this->compileUnions($query);
    }

There is already a bug reported where I have found this solution: https://github.com/laravel/framework/issues/5457

Question: Am I doing anything wrong here or should we create or reopen an issue on github.

4th December, 2015

mshakeel left a reply on Multi Line Environment Variable • 2 years ago

@Snapey I have tried quoting the entire string, '\n' & '' at the end of each line

2nd December, 2015

mshakeel started a new conversation Multi Line Environment Variable • 2 years ago

We are using https://github.com/layerhq/layer-identity-token-php to generate identity token and have added private key in .env file like following:

LAYER_PRIVATE_KEY=-----BEGIN RSA PRIVATE KEY-----
keyxxxxxxxxxxxxxxxxxxxxxxxxx-----------
-----END RSA PRIVATE KEY-----

but it only returns first line (i.e. -----BEGIN RSA PRIVATE KEY-----) and therefore identity token can't be generated.

Please help how to store this key within .env file

Setting this key within code like following is working fine but we want to store it in .env

$privateKey                 = <<<EOF
-----BEGIN RSA PRIVATE KEY-----
keyxxxxxxxxxxxxxxxxxxxxxxxxx-----------
-----END RSA PRIVATE KEY-----
EOF;
$layerIdentityTokenProvider->setPrivateKey($privateKey);

27th November, 2015

mshakeel started a new conversation Dingo Api: Can't Add Transformers Within Response • 2 years ago

Want to add a transformed object along with other response, I have used following code:

$accessToken = Authorizer::issueAccessToken();

    $user = User::where('email', $request->get('username'))->with('profile')->first();
    if ($user) {
        $accessToken['user'] = $this->response->item($user, new UserTransformer);
    }

    return $accessToken;

Expected Response:

{
    "access_token": "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
    "token_type": "Bearer",
    "expires_in": 31536000,
    "data": {
      "id": 1,
      "email": "xxxxx",
      "profile": {
        "data": {
          "id": 1,
          "first_name": "Muhammad",
          "last_name": "Shakeel",
        }
      }
    }
}

but not getting transformed object, there must be some better way to add multiple transformed objects with response. Am I missing something?

PS: Returning only the transformed object as following works correctly and sends transformed object:

return $this->response->item($user, new UserTransformer);

12th November, 2015

mshakeel left a reply on OrderBy Resultset With Related Column • 2 years ago

Thanks alot, yes it is working fine. I was missing the records after sorting.

mshakeel left a reply on OrderBy Resultset With Related Column • 2 years ago

@pmall Thanks, It worked as expected. But I found the actual problem i.e. eager loading works with inner join but failed to load if we use leftJoin.

The following query will not eager load due to left Join

$users = User::with('profile')->select('users.*')
    ->join('profiles', 'users.id', '=', 'profiles.user_id')
    ->orderBy('profiles.first_name')
    ->get();

mshakeel left a reply on OrderBy Resultset With Related Column • 2 years ago

@bobbybouwmann @pmall , Thanks for your answers.

Joining the tables as suggested works fine with the sort but it does not attach related column in my case. Since I am using Fractal transformer to ultimately send a response from my api (dingo api used).

$users = $user->otherUsers()->paginate($this->limit);
return $this->response->paginator($users, new UserTransformer);

/**
 * Scope a query to only include popular users.
 *
 * @return \Illuminate\Database\Eloquent\Builder
 */
public function scopeOtherUsers($query)
{
    return $query->where('users.id', '<>', $this->id)
        ->with('profile')
        ->leftJoin('profiles as p', 'p.user_id', '=', 'users.id')
        ->orderBy('p.first_name', 'asc');
}

mshakeel left a reply on Is There An Easy Way To Find Out Where Are My Queries Are Being Generated From? • 2 years ago

You can also do it like following:

\DB::listen(function ($sql) {
            var_dump($sql);
        });

mshakeel started a new conversation OrderBy Resultset With Related Column • 2 years ago

I have User model which has one Profile model.

/**
 * Get User's profile
 *
 * @return App/Models/Profile Profile object for this User
 */
public function profile()
{
    return $this->hasOne('App\Models\Profile');
}

Now I wanted to get all users order by profile.first_name. I have tried few options with no luck. Following is what I wanted:

$users = User::with('profile')->orderBy('profile.first_name')->get();

It says Unknown column 'profile.first_name'

Help me correct the way we can order by related model's column so I can further use it in my scopes:

/************************ Scopes *************************/

/**
 * Scope a query to get other users sorted by profile.first_name.
 *
 * @return \Illuminate\Database\Eloquent\Builder
 */
public function scopeOtherUsers($query)
{
    return $query->where('users.id', '<>', $this->id)
        ->with('profile')->orderBy('profile.first_name');
}

2nd October, 2015

mshakeel left a reply on [L5] Disable CSRF Middleware On Certain Routes • 3 years ago

In Laravel 5.1, you can exclude URIs from CSRF protection by simply adding them to the $except property of the VerifyCsrfToken middleware:

<?php

namespace App\Http\Middleware;

use Illuminate\Foundation\Http\Middleware\VerifyCsrfToken as BaseVerifier;

class VerifyCsrfToken extends BaseVerifier
{
    /**
     * The URIs that should be excluded from CSRF verification.
     *
     * @var array
     */
    protected $except = [
        'api/*',
    ];
}

Documentation: http://laravel.com/docs/5.1/routing#csrf-protection http://stackoverflow.com/questions/26992885/laravel-5-post-whithout-csrf-checking/32902801#32902801

Edit Your Profile
Update

Want to change your profile photo? We pull from gravatar.com.