vendredi 10 décembre 2021

Laravel SFTP Connection Using Passphrase

       This is the stand way that I have been using to place a file in SFTP 

Storage::disk()->put($fileName, file_get_contents($storagePath . $fileName));

But I am facing a situation where for connection first I need pass the key-phrase and post that only SFTP will askfor user_id and password,

How can Is set this up for Jobs in Laravel, Using Laravel 5.1 (due to some issues cant upgrade but that's not the concern as of now)

My Configuration in Filesystem

'new_sftp' => [
        'driver'     => 'sftp',
        'host'       => 'homestead',
        'port'       => 22,
        'username'   => 'myuser',
        'password'   => 'mypass',
        'root'       => /myroot/folder,
        'privateKey' => '/home/myroot/myfile.pem',
        'password'   => 'mypassprase',
        'timeout'    => '300',
        'visibility' => 'public',
        'permPublic' => 0766,
    ],


via Chebli Mohamed

jeudi 9 décembre 2021

Route is working when referred but when I go to that provided link it will not find the controller function in laravel

I have added a custom function to save pictures and added the route as well and when I reference the route it does work but when visiting it gives error that the function can not be found with reflectionexception error ReflectionException

Function () does not exist

Controller ----------------------

/**
*    show the form for uploading profile picture 
*    
*   @return \Illuminate\Http\Response 
*/
public function changePicture()
{

    return view('usersinformation.profilepicture');
}

route web.php ---------------------------

Route::get('usersinformation/changePicture',[usersinformationController::class, 'changePicture'])->name('usersinformation.changePicture');

Route::post('usersinformation/savePicture', 'usersinformationController@savePicture');



via Chebli Mohamed

Route issue in Laravel with several controllers and different route with same name

I have added below routes in web.php but it's not working.

Route::post('show', [
'as' => 'usersinformation.show',
'uses' => 'usersinformationController@show'


 ]);



 Route::post('store', [
    'as' => 'usersinformation.store',
    'uses' => 'usersinformationController@store'
  ]);



 Route::get('store',[usersController::class, 'store'])->name('usersinformation.store');
    Route::post('/store', 'usersController@store');
    Route::post('store',[usersController::class, 'store'])->name('users.store');
    Route::get('/index', 'usersController@index');

my controller is as below and I am using Ajax to send data but the error I receive is Method not allowed exception.

public function store(Request $request)
{
    //
    $fname = $request -> fname;
    $lname = $request -> lname;
    $pnumber = $request -> pnumber; 

    
}

Ajax Code ----------------

data = {
    _token: $('input#usersinformation-token').val(),
    'fname': $('input#first_name').val(), 
    'lname': $('input#last_name').val(),
    'pnumber': $('input#phonenumber').val()

};
$.post(url, data, function(data, status){
    alert('working' + data + "    " + status );
    $('div#load-content').html(data);
} );


via Chebli Mohamed

mercredi 8 décembre 2021

Primary Key is different from the default id that laravel searches for

I have added a custom primary key within the table and now the Laravel is not even finding the result at all. giving error that the page could not be found.

NotFoundHttpException

No query results for model [App\usersinformation].

Controller --------------------

public function show(usersinformation $usersinformation)
{
    //
  //  $users =  $user = usersinformation::where('user-id','=',$usersinformation) -> first();
    
        return view('usersinformation.show');

}

Model -------------------

class usersinformation extends Model
{
    //

    public $table = "usersinformation";
    public $incrementing = false;
    
    protected $fillable = [

        'fname','lname', 'user-picture', 'phone-number', 'user-id', 'place-id'

    ];
    protected $primaryKey = 'user-info-id';

    public function users(){
        $this -> belongsTo('App\users');
    }

route ---- web.php

Route::post('/show','App\Http\Controllers\usersinformationController@show');

Still facing the same issue, if I comment the primary key, it will give error that the id field is not found within the table when I add the custom primary key in the model, it will give error page not found.



via Chebli Mohamed

How to join Multiple databases from different hosts in Laravel

Is there any way to use join query with Multiple databases from different servers?

my database.php is

'mysql' => [
            'driver' => 'mysql',
            'host' => env('DB_HOST', '127.0.0.1'),
            'port' => env('DB_PORT', '3306'),
            'database' => env('DB_DATABASE', 'db1'),
            'username' => env('DB_USERNAME', 'root'),
            'password' => env('DB_PASSWORD', ''),
            ...
        ],

        'mysql2' => [
            'driver' => 'mysql',
            'host' => env('DB_HOST_2', '192.233.****.*'),
            'port' => env('DB_PORT_2', '3306'),
            'database' => env('DB_DATABASE_2', 'db2'),
            'username' => env('DB_USERNAME_2', 'root'),
            'password' => env('DB_PASSWORD_2', ''),
         ...
        ]

and i need to implement it in join query.

My controller function is

public function function1(){
$db1 = DB::connection('mysql2');
$result = TABLE1::join($db1 . '.' . 'table2', 'table2.id','=','table1.table2_id');

return Datatables::of($result);

}


via Chebli Mohamed

mardi 7 décembre 2021

How to set the primary key itself as a foreign key in laravel?

This sounds nooby but I wanted to know if I can make the primary key work as a foreign key in Laravel, and I'am new to Laravel.

So, I have two migrations 'User' and 'Student' as Shown below: User :

 Schema::create('users', function (Blueprint $table) {
            $table->string('uniqueId', 30)->primary();
            $table->text('password');
            $table->string('userType');
            $table->timestamps();
        });

and Student :

Schema::create('students', function (Blueprint $table) {
            $table->string('uniqueId', 30)->primary();
            $table->text('name');
            $table->text('fName');
            $table->text('mName');
            $table->text('addr');
            $table->string('image');
            $table->integer('class');
            $table->integer('roll');
            $table->string('year');
            $table->timestamps();
        });

So, all I wanted was that the primary key in Student (uniqueId) also work as a foreign key that references the 'uniqueId' column from the User table.

Thanks in advance.



via Chebli Mohamed

Laravel 5.2 New schedule entries not running on production

I have three command AAA, BBB and CCC.

CCC is the latest command that I have entered in this schedule, strange scenario is that AAA and BBB are still running as they were, but not CCC.

This laravel schedular runs all okay on local, but on production it is not showing any effect for CCC. Even doing php artisan ccc on production terminal works, but scheduler is showing no effect, no error for CCC.

Any idea what's causing it ? Is there any sort of cache or similar thing that is causing it ?

<?php

namespace App\Console;

use Illuminate\Console\Scheduling\Schedule;
use Illuminate\Foundation\Console\Kernel as ConsoleKernel;

class Kernel extends ConsoleKernel
{
    /**
     * The Artisan commands provided by your application.
     *
     * @var array
     */
    protected $commands = [
        \App\Console\Commands\AAA::class,
        \App\Console\Commands\BBB::class,
        \App\Console\Commands\CCC::class,
    ];

    /**
     * Define the application's command schedule.
     *
     * @param  \Illuminate\Console\Scheduling\Schedule  $schedule
     * @return void
     */
    protected function schedule(Schedule $schedule)
    {
        $schedule->command('aaa')
                 ->hourly();

        $schedule->command('bbb')
                 ->everyTenMinutes();

        $schedule->command('ccc')
                ->everyTenMinutes();

    }

}


via Chebli Mohamed