lundi 27 février 2017

Auth Issue when upgrading from Laravel 5.2 to Laravel 5.3

I have following the official guide to upgrade from laravel 5.2 to laravel 5.3: http://ift.tt/29vUJ0Q

Because I needed some customizations to the default authentication I have copied the login function to Http\Controllers\Auth\AuthController.php.

Now, when I updated, the `AuthController.php' was divided into several other files.

I have copied the login function to Http\Controllers\Auth\LoginController.php

Now, I am getting the following error when trying to login:

BadMethodCallException in Controller.php line 82:

Method [getCredentials] does not exist.

The login functions below (Might not matter):

public function login(Request $request)
{
$this->validate($request, [
    'email' => 'required|email', 
    'password' => 'required',
]);

$credentials = $this->getCredentials($request);

// This section is the only change
if (Auth::validate($credentials)) {
    $user = Auth::getLastAttempted();
    if ($user->active) {
        Auth::login($user, $request->has('remember'));

        ActivityLog::add("User has successfully logged in.", $user->id);

        return redirect()->intended($this->redirectPath());
    } else {
        return redirect($this->loginPath) // Change this to redirect elsewhere
            ->withInput($request->only('email', 'remember'))
            ->withErrors([
                'active' => 'This account has been suspended.'
            ]);
    }
}

return redirect($this->loginPath)
    ->withInput($request->only('email', 'remember'))
    ->withErrors([
        'email' => $this->getFailedLoginMessage(),
    ]);

}

How do I fix this?



via Chebli Mohamed

Laravel 5.3 how to write custom casted attribute

I have Trait for custom cast types and added currency cast type but it works only when accessing attribute:

trait ModelCastsTrait {

protected function castAttribute($key, $value)
{
    if (is_null($value)) {
        return $value;
    }

    switch ($this->getCastType($key)) {
        case 'int':
        case 'integer':
            return (int) $value;
        case 'real':
        case 'float':
        case 'double':
            return (float) $value;
        case 'string':
            return (string) $value;
        case 'bool':
        case 'boolean':
            return (bool) $value;
        case 'object':
            return $this->fromJson($value, true);
        case 'array':
        case 'json':
            return $this->fromJson($value);
        case 'collection':
            return new BaseCollection($this->fromJson($value));
        case 'date':
        case 'datetime':
            return $this->asDateTime($value);
        case 'timestamp':
            return $this->asTimeStamp($value);
        case 'currency':
            return new CurrencyCast($value, $this);
        default:
            return $value;
    }
}

}

How make custom attribute casting work in reverse, convert value to castable type?



via Chebli Mohamed

Internal server error in laravel 5.3

I am trying to validate a form data if it exist in database. I'm using jquery validate plugin.But I am getting the internal server error 500. Here is my validation.js file

$(document).ready(function () {
    $("#test-form").validate({
        rules: {
            projectname:{
                required:true,
                minlength:5,
                remote:{
                    url: '/checkProject',
                    type: "post"
                }
            }
        },
        messages:{
            projectname:{
                required: "Please enter at least 5 character project name!",
                remote:"This project already exist. Please enter a diffrent name!"
            }
        }
    });

});

here is my web.php

Route::post('/checkProject','ProjectController@checkProject');

and controller

public function checkProject(Request $request){
        $RequestedProject = "Project Y";
        $RegisteredProject = Project::all();
        $flag = "true";
        foreach ($RegisteredProject as $item){
           if( $item['name'] == $RequestedProject){
               $flag = "false";
               break;
           }
        }
        echo $flag;
    }

what is the problem



via Chebli Mohamed

Run a raw SQL statement to rename an index in a Laravel migration

I want to run the following raw SQL statement in a Laravel migration:

RENAME INDEX offer_venue_id_fk_idx TO offers_venue_id_index

How can I do this?



via Chebli Mohamed

Rename an index in Laravel 5

I want to rename an index in Laravel 5.

In a previous migration a column was created for table named a like so:

$table->unsignedInteger('foo')->index('blah');

I want to rename the index so that it uses the default Laravel notation.

i.e. I want to rename the blah index to a_blah.

I know how to rename a normal column, like so:

$table->renameColumn('from', 'to');

But the documentation does not mention how to rename indexes.

How can I do this?



via Chebli Mohamed

Which is the best choice between Querry builder && Eager loading in laravel 5.4?

I have 3 table: users, posts and comments. I have 2 way for selecting data.

$posts = User::select('id')
            ->with(['post' => function( $query ){
                $query->with(['comments']);
            }])->find($userId);

and

$posts = DB::table('posts')
        ->leftjoin('comments', 'posts.id', '=', 'comments.post_id')
        ->where('user_id', $userId)
        ->get();

I see method 2 is faster than method 1 in some records. I don't know what happen when I have 1000 or 10000 records. Can you tell me which is better? Thanks for any help.



via Chebli Mohamed

Uncaught Referrence error

I am using laravel 5.3. I am trying to validate form data with jquery validate plugin. After data validation i want to fire an ajax call. Here is my code

<!doctype html>
<html>
<head>
    <meta name="_token" content="{!! csrf_token() !!}"/>
    <script src="http://ift.tt/1dLCJcb"></script>
    <script src="http://ift.tt/1NKbGQl"></script>
    <link href="http://ift.tt/1jAc5cP" rel="stylesheet" type="text/css" />
    <script src="http://ift.tt/1jAc4pg"></script>
    <script src="/js/bootstrap-tagging.js"></script>
    <link type="text/css" href="/css/bootstrap-tagging.css" rel="stylesheet">
    <script src="/js/tapered.bundle.js"></script>
    <script src="http://ift.tt/2iFPHiv"></script>
    <script src="/js/validation.js"></script>
    <script>
        $(document).ready(function() {
            function ajaxCall() {
                console.log("event occured");
                $.ajax({
                    type: "POST",
                    url: '/storeproject',
                    data: { _token: "" },
                    success: function( msg ) {
                        console.log(msg);
                    }
                });
            }
        });
    </script>
</head>
<body>
<form role="form" method="POST" action="javascript:ajaxCall()" id="test-form">
    {!! csrf_field() !!}
    <label>Project Name</label>
    <input class="form-control" id="projectName" placeholder="Name" name="projectname" type="text" autofocus="">
    <input type="submit"  id="submit" name="submit" class="btn btn-primary" value="Add">
</form>
</body>
</html>

When i hit the add button gives me the error

VM681:1 Uncaught ReferenceError: ajaxCall is not defined at :1:1

but if I avoid the document ready function it works fine. What is the problem?



via Chebli Mohamed