When I Want To Access my login route then it threw me and error like this
(1/1) FatalErrorException
Namespace declaration statement has to be the very first statement or after any declare call in the script
in User.php (line 3)
but the Namespace of the UserController set Default by laravel
Here is the defined user controller namespace
<?php
namespace App;
use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;
class User extends Authenticatable
{
use Notifiable;
Related
I want to use dynemodb and mysql both with lumen.
I have follow below steps,
https://github.com/aws/aws-sdk-php-laravel
from above url I have add package for aws sdk for lumen
and add my accesskey and secret key in .env file
in bootstrap/app.php
I have add $app->register(Aws\Laravel\AwsServiceProvider::class);
Now I want to use dynemodb with lumen to execute query
for execute dynemodb query same as eloquent I have used below package.
https://github.com/baopham/laravel-dynamodb
now I have write my code in model as below,
<?php
namespace App\Models;
use BaoPham\DynamoDb\Facades\DynamoDb;
use BaoPham\DynamoDb\DynamoDbModel;
class CategoryMaster extends BaoPham\DynamoDb\DynamoDbModel
{
protected $table = 'category_master';
protected $fillable = ['id', 'category_name'];
public static function listname()
{
$model = DynamoDbModel::where(['category_name' => 'blue']);
$query = $model->get();
echo"<pre>";print_r($query);die;
}
}
it gives me arror like below,
FatalErrorException in CategoryMaster.php line 8:
Class 'App\Models\BaoPham\DynamoDb\DynamoDbModel' not found
can you help me to resolve thais issue to use dynemodb
I implemented dynamodb in laravel project using baopham package.
In .env file define dynamodb credentials
DYNAMODB_CONNECTION=aws
DYNAMODB_KEY=***
DYNAMODB_SECRET=****
DYNAMODB_REGION=us-east-1
In Model file
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class User extends \BaoPham\DynamoDb\DynamoDbModel
{
protected $table = 'Users'; //table name
protected $guarded = [];
}
In controller file
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\User; //include your model file
class UserController extends Controller
{
public function index()
{
$user = User::all(); // to get all data from user table
return view('products.index')->with('user', $user);
}
}
for more referance refer https://github.com/baopham/laravel-dynamodb query section.
I found my solution,
I have followed below site step by step and I am able to connect to dynemo db with lumen and able to fire eloquent queries
https://github.com/aws/aws-sdk-php-laravel
https://github.com/baopham/laravel-dynamodb
https://github.com/laravelista/lumen-vendor-publish
I am overriding the find method and add the where condition globally thats working fine here is my code
class Order extends \common\components\ActiveRecord
\common\components\ActiveRecord
namespace common\components;
use Yii;
use yii\db\ActiveRecord as BaseActiveRecord;
class ActiveRecord extends BaseActiveRecord{
public static function find() {
return parent::find()
->where(['=',static::tableName().'.company_id',Yii::$app->user->identity->company_id])
->andWhere(['=',static::tableName().'.branch_id',Yii::$app->user->identity->branch_id]);
}
}
The find is not working when i call the model from view like this
echo \common\models\Order::find()->where(['status'=>'0'])->count();
Since you're using where() inside the find function that you're using in ActiveRecord and then where() in this statement
echo \common\models\Order::find()->where(['status'=>'0'])->count();
the second ends up overriding the first
What you need to do is to use andWhere() in all the cases where you need the original conditions that you put in ActiveRecord to work. Try thus:
echo \common\models\Order::find()->andWhere(['status'=>'0'])->count();
This should work.
And again remember, use andWhere() in all places where the original condition needs to be applied
Use andWhere() so your default condition not get overridden :
class ActiveRecord extends BaseActiveRecord {
public static function find() {
return parent::find()
->andWhere(['=',static::tableName().'.company_id',Yii::$app->user->identity->company_id])
->andWhere(['=',static::tableName().'.branch_id',Yii::$app->user->identity->branch_id]);
}
}
Try to change with onCondition()
class ActiveRecord extends BaseActiveRecord {
public static function find() {
return parent::find()
->onCondition([
['=',static::tableName().'.company_id',Yii::$app->user->identity->company_id],
['=',static::tableName().'.branch_id',Yii::$app->user->identity->branch_id]
]);
}
}
I created a service provider named AdminServiceProvider
namespace App\Providers;
use Modules\Orders\Models\Orders;
use Illuminate\Support\ServiceProvider;
use View;
class AdminServiceProvider extends ServiceProvider
{
public function boot()
{
$comments = Orders::get_new_comments();
View::share('comments', $comments);
}
public function register()
{
}
}
Registered the provider
App\Providers\AdminServiceProvider::class,
Now I try to attach it to the controller
namespace App\Http\Controllers\admin;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Providers\AdminServiceProvider;
class AdminController extends Controller
{
public $lang;
public function __construct()
{
}
public function index(){
return view('admin/dashboard');
}
}
Now I get this error message
Undefined variable: comments
This is the first time I try to use a custom service provider and don't know exactly how it works I'm sure there's something missing Hope you can help. Thanks in advance.
[UPDATE]
removed use App\Providers\AdminServiceProvider; from the controller
php artisan clear-compiled solved the problem but I want to attach it to some controllers not all controllers as the $comments are sent to all contollers in my app. So how to attach the service provider to specific controllers not all of them?
For the undefined variable run: php artisan clear-compiled will solve it
If you want to share a variable in some of your views you can create a middleware and assign it to the views you want to share the data with:
First create a middleware: php artisan make:middleware someName
Then in the handle function you add your view sharing logic:
$comments = Orders::get_new_comments();
view()->share('comments',$comments);
return $next($request);
Then register your middleware under the $routeMiddleware array and
give it an alias.
Then attach it to your routes like:
Route::group(['middleware'=> 'yourMiddlewwareName'], function(){
//your routes
});
If you have all your admin views in one directory (views\admin for example) you can use view composer in AdminServiceProvider:
public function boot()
{
view()->composer('admin.*', function($view){
$view->with('comments', Orders::get_new_comments());
});
}
It will attach comments variable to each view in your views\admin directory.
You can also attach a variable to some specific views or folders like this:
view()->composer(['admin.posts.*', 'admin.pages.index'], function($view){
$view->with('comments', Orders::get_new_comments());
});
I want to use my custom Trait stored in app directory into my controller. However I always get this message:
Trait 'app\MessageTrait' not found
My Controller:
namespace app\Http\Controllers;
use Illuminate\Http\Request;
use Auth;
use app\Http\Requests;
use app\User;
use app\MessageTrait;
class login extends Controller{
use MessageTrait;
public function index(Request $request){
return back();
}
}
My MessageTrait is contained within MessageTrait.php, located in app directory. Code looks like this:
My Trait:
trait MessageTrait{
public function success(){
return 'success';
}
public function error($message){
return 'error';
}
}
First I thought it may be a Namespace issue - however, User class could be found using same namespacing as my MessageTrait. Any ideas?
I solved this by adding namespace app to the top line of my Trait file. Everything works as expected now!
I am trying to login a user with laravel 5.
Here is my controller
public function postLogin(LoginRequest $request){
$remember = ($request->has('remember'))? true : false;
$auth = Auth::attempt([
'email'=>$request->email,
'password'=>$request->password,
'active'=>true
],$remember);
if($auth){
return redirect()->intended('/home');
}
else{
return redirect()->route('login')->with('fail','user not identified');
}
}
When i enter wrong credentials, everything works fine, but when i enter the right one, i got this error message:
ErrorException in EloquentUserProvider.php line 110:
Argument 1 passed to Illuminate\Auth\EloquentUserProvider::validateCredentials() must be an instance of Illuminate\Contracts\Auth\Authenticatable, instance of App\Models\User given, called in C:\xampp\htdocs\Projects\Pedagogia\Admin.pedagogia\vendor\laravel\framework\src\Illuminate\Auth\Guard.php on line 390 and defined
I don't see where i did wrong
Argument 1 passed
to Illuminate\Auth\EloquentUserProvider::validateCredentials() must be
an instance of Illuminate\Contracts\Auth\Authenticatable, instance of
App\Models\User given.
The validateCredentials() method of the Illuminate\Auth\EloquentUserProvider class expects an instance of Illuminate\Contracts\Auth\Authenticable, but you are passing it an instance of App\Models\User. To put it simply, your user model needs to implement the Illuminate\Contracts\Auth\Authenticable interface to work with Laravels authentication scaffolding.
Your App\Models\User model should look like this:
use Illuminate\Contracts\Auth\Authenticatable;
use Illuminate\Auth\Authenticable as AuthenticableTrait;
class User extends \Eloquent implements Authenticatable
{
}
#Gaetan you are getting this not found error, because you are using the
use Illuminate\Contracts\Auth\Authenticable instead using use Illuminate\Contracts\Auth\Authenticatable;
you user model should be like that:
use Illuminate\Database\Eloquent\Model;
use Illuminate\Contracts\Auth\Authenticatable;
use Illuminate\Auth\Authenticatable as AuthenticableTrait;
class User extends Model implements Authenticatable
{
// your code here
}
change Authenticable with Authenticatable.