dimanche 1 mai 2016

How to change database in laravel5

I am just starting to learn laravel. I had given a database name (eg: mydatabase1) while beginning. Now want to change the db(eg: mydatabase2). I have updated the new database name database.php and .env files. Still when i run a query in the model $users = DB::table('tbl_users')->limit(1)->get();, it gives me error like this SQLSTATE[42S02]: Base table or view not found: 1146 Table 'mydatabase1.tbl_users' doesn't exist (SQL: select * from 'tbl_users' limit 1), means its still considering first configuration. I wonder where i have to update to reflect the db name update. Please help



via Chebli Mohamed

Laravel 5.1 Net_SFTP has a deprecated constructor

I'm using Laravel 5.1 with the package laravelcollective/remote to run an SSH command on a remote server:

\SSH::into('myserver')->run($commands);

But I'm getting the following error message:

Methods with the same name as their class will not be constructors in a future version of PHP; Net_SFTP has a deprecated constructor

I can see that the error is coming from phpseclib package, and on newer versions they have fixed that. (They changed the function name to __construct), but I can't update phpseclib package because I don't include it on my composer.json file, laravelcollective/remote does this.

How can I solve this?



via Chebli Mohamed

Git Laravel - what should I do if I change files in the vendor directory?

I've changed the authenticatesUser trait located in the vendor folder.

Them changes are not tracked so when pulling the project, the login features won't work.

Should i just track the whole of vendor?

or create a new file outside of vendor to handle the authenticatesUser trait?



via Chebli Mohamed

How create a API tool in Laravel?

I need to create a API management tool. In this tool a user can specify the path, the source whether it is a MySQL or SQLite or other database system query. The result of the source can be JSON or XML. And user also can set the authentication method, whether using TOKEN or password. All these information will be stored in database.

I am thinking of using ServiceProvider class of Laravel, as I read we need to register the service and bind it. My question is if the path is user generated (it can be any path so that it is difficult to define it on the route), how can I handle the dynamic path in Laravel for different service, output and also parameter ?



via Chebli Mohamed

Consuming API from website

I'm developing a website and REST API which are a part of the same project folder.

I've developed a REST api as below

URL: example.com/login
Type: POST
Request Body:
{
    "credentials": {
        "username":"dd",
        "password":"dd"
    },
    "appInfo": {
        "version": "1.0"
    }
}

In the login method (UserController) I'm fetching the request body as below

json_decode(Request::instance()->getContent());

Now I'm developing a webpage and want to invoke this API from there. I'm using the below code (HomeController)

public function acceptlogin(Request $request) {
    $data = array(
        'credentials'=> array(
            'username'=>$request->email,
            'password'=>$request->pasword
        ),
        'appInfo'=> array(
            'version'=> "1.0"
        )
    );
    $request1 = Request::create('example.com/login', 'POST', $data);
    $response = Route::dispatch($request1);
}

I'm able to invoke the login api from the above code but the below code in the login method returns empty.

json_decode(Request::instance()->getContent());

Can you please suggest on how to invoke a local API (within the same project) in Laravel



via Chebli Mohamed

Laravel 5.1 & Socialite - How to connect Facebook/Twitter details to currently authenticated user

I've implemented Socialite fine and works for new users or users that have the same Facebook email address as the email address in my apps database.

However, some users will have a different Facebook email account to the account they use in my app. I would like to allow users to connect their facebook profile to their app account when they are logged in.

Below is my current code that handles the socialite user creation/update:

public function handleProviderCallback($provider)
    {
        $user = Socialite::driver($provider)->user();

        $user_query = User::where('email', $user->email)
            ->orWhere('oauth_facebook_id', $user->id)
            ->orWhere('oauth_twitter_id', $user->id)
            ->get();

        if($user_query->count() > 0) {

            $the_user = $user_query->first();

            if($provider === 'facebook') {
                $the_user->oauth_facebook_id = $user->id;
            }

            if($provider === 'twitter') {
                $the_user->oauth_twitter_id = $user->id;
            }

            $the_user->avatar = $user->avatar;

            $the_user->save();

            \Auth::login($the_user, true);

            return redirect('/dashboard');
        }

        $new_user = User::create([
            'name' => $user->name,
            'email' => $user->email,
            'oauth_facebook_id' => $provider === 'facebook' ? $user->id : NULL,
            'oauth_twitter_id' => $provider === 'twitter' ? $user->id : NULL,
            'avatar' => $user->avatar,
            'confirmed' => true,
            'gender' => $user->user['gender'],
        ]);

        \Auth::login($new_user, true);

        flash()->success('Success!', 'Your account has been created using your '.ucfirst($provider).' details!');
        return redirect('/dashboard');
    }

What would be the best way to link the returned details with the current authenticated user?

Thanks in advance :)



via Chebli Mohamed

samedi 30 avril 2016

How to append to result in Laravel 5.1

I try to return my search result into array to show in blade but get error :

$persons = persons::where('name','like',"%$search_term%")->lists('id');

foreach($persons as $person)
{
       $trials = trial::with('samples')->with('persons')->where('persons_id', '=', $person)->get();
}

FatalErrorException in 0bfe77047992e2dce86ae561e266494c line 37: Call to undefined method Illuminate\Database\Eloquent\Collection::appends()

I try to this with + array, but get errors



via Chebli Mohamed