Resolve a PHPCS error in a Laravel migration file - laravel

In all my laravel migrations I want to use PSR2, but I getting a warning.
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
class CreatePlayersTable extends Migration {
// migration
}
The error phpcs warning is Each class must be in a namespace of at least one level (a top-level vendor name)
How can I fix this warning?

Related

Schema File not run in tests

When I run tests I call migrate:fresh. The schema file is not running. I'm using mysql database for testing. migrate:fresh runs well in other environments. please anyone has an idea?
This is the command I'm using:
Artisan::call('migrate:fresh')
This probably happens because your test database doesn't have the same name as your dumped database, so laravel can't find the migration.
A work around could be manually specify the dump path to the RefreshDatabase trait like that:
namespace Tests;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Foundation\Testing\TestCase as BaseTestCase;
abstract class TestCase extends BaseTestCase
{
use CreatesApplication, RefreshDatabase;
protected function migrateFreshUsing()
{
return ['--schema-path' => 'database/schema/mysql-schema.dump'];
}
}

Facing problem while trying to insert data in database, LARAVEL

I am getting this error "Class 'App\Models\Student' not found".
Please check my codes:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\Student;
class StudentController extends Controller
{
public function data(){
$stud = new Student;
$stud->name = 'Mona Lisa';
$stud->rollnumber = '001';
$stud->save();
}
}
Your error is most likely in the namespace declaration of the Student model.
check in your class file App\Models\Student.php that the namespace is correct.
namespace App\Models;
then try to run in the console:
composer dump-autoload
Change this line:
use App\Models\Student;
to:
use App\Student;
Your error suggests that class is not loaded into the controller. I am guessing from the issue that you have created a separate directory for models if you have then you need to update your composer file to include new class paths for model classes. locate autoload in you composer.json file and add an entery in classmap array as shown below
"autoload": {
"classmap": [
"database",
"app/models"
]
}
then on command line navigate to your project directory and run the following command:
composer dump-autoload
and if you didn't create new models directory then just run
composer dump-autoload
Make sure your namespace of student model
use App\Models\Student If your model is in App\Models directory
use App\Student if your model is in app directory
Finally run composer dump-autoload
check your namespace of student model
use App\Models\Student If your model is in use App\Models directory
use App\Student if your model is in app directory
then go to command line and run composer dump-autoload

Undefined class OneSignal

I use Laravel 5.2 and want to use berkayk/laravel-onesignal package, and I installed this package step by step according to Guide on github.
But when I want to use this package I get "Undefined class OneSignal", also I run this artisan command in the terminal:
php artisan config:clear
php artisan config:cache
I try this code in controller
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use OneSignal;
use App\Http\Requests;
class SignalController extends Controller
{
public function index()
{
OneSignal::sendNotificationToAll("Some Message");
}
}
I get this error
You should use full namespace for the facade:
\OneSignal::sendNotificationToAll("Some Message");
Or add this to the top of your class:
use OneSignal;
You should write use OneSignal top of the class underneath the namespace.
Hope this work for you!

Laravel failed to open stream

UPDATE: I managed to resolve the issue by completely removing the vendor directory, and re-running the composer install command. I still don't know what was the actual cause of this beahviour, but at least it's working now.
I've been tasked to troubleshoot and improve an application written in Laravel.
Since I almost always worked in Codeigniter, my knowledge of Laravel boils down to "I know what it is and where to download it from" :)
So, the application fails with the the following error:
PHP Warning: Uncaught ErrorException: require(\Repos\PermissionRepo.php): failed to open stream: No such file or directory in \wwwroot\app\Repos\PermissionRepo.php:5
the PermissionRepo file looks like this:
2: namespace Wckphma\Repos;
3:
4: use Illuminate\Support\Facades\Auth;
5: use Wckphma\User;
the seemingly missing Wckphma\User declaration file does exist, and the beginning of the file looks like this:
namespace Wckphma;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Wckphma\Repos\GroupRepo;
use Wckphma\Repos\PermissionRepo;
class User extends Authenticatable
{
****
It cannot be the file permissions, as in pure desperation I gave all the user read/write permissions to all files throughout.
Any ideas, what this might be?

How to specify a path in routing Laravel?

I have located contoller in directory dashboard, how to specify correct path in routing to this controller?
I have class by path:
Class App\Http\Controllers\Dashboard\PlaceController
But I get error that this class : does not exist
Laravel uses PSR-4 namespaces, so you need to make sure controller is in correct namespace:
namespace App\Http\Controllers\Dashboard;
use App\Http\Controllers\Controller;
class PlaceController extends....
If namespace is correct, try to run composer dumpauto command.
Here are a example :
Laravel 5.2 :
create a directory on >> App >> Http >> Controllers >> Dashboard
create a file >>App>>Http>>Controllers>>Dashboard>> PlaceController.php
PlaceController.php
<?php
namespace App\Http\Controllers\Dashboard;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Http\Requests;
class PlaceController extends Controller
{
// write your functions
}
command line
on command line : composer dump-autoload

Resources