Argument 1 passed to Illuminate\Foundation\Testing\TestCase::actingAs() must implement interface Illuminate\Contracts\Auth\Authenticatable - laravel

Argument 1 passed to Illuminate\Foundation\Testing\TestCase::actingAs() must implement interface Illuminate\Contracts\Auth\Authenticatable, instance of Illuminate\Database\Eloquent\Collection given
I have also tried some of the following methods, but none of them worked:
Argument 1 passed to Illuminate\Foundation\Testing\TestCase::actingAs()
Problem with testing method with actingAs
I hope there is another way I can solve this problem.
This is my ClipartTest.php file:
<?php
namespace Tests\Feature;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Foundation\Testing\WithFaker;
use Tests\TestCase;
class ClipartTest extends TestCase
{
use RefreshDatabase;
/**
* A basic feature test example.
*
* #return void
*/
public function test_can_get_clipart()
{
$this->withoutExceptionHandling();
$user = User::factory(3)->create();
$this->actingAs($user, 'api');
$this->getJson('/api/cliparts')
->assertStatus(201)
;
}
This is my User.php file
<?php
namespace App\Models;
use App\Http\Traits\Uuid;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Laravel\Sanctum\HasApiTokens;
class User extends Authenticatable
{
use Authenticatable, HasFactory, Notifiable, HasApiTokens;
use Uuid;
/**
* The attributes that are mass assignable.
*
* #var array
*/
protected $fillable = [
'name',
'email',
'password',
];
This is my Clipart.php file:
<?php
namespace App\Models;
use App\Http\Traits\Uuid;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Clipart extends Model
{
use HasFactory;
use Uuid;
/**
* The attributes that are mass assignable.
*
* #var array
*/
protected $fillable = [
'user_id',
'name',
'image_id',
];

You are creating 3 users with the factory and therefore the $user will actually return a collection of 3 users.
Instead of User::factory(3)->create() do User::factory()->create() so that it would return a single user model.

Related

Laravel 8 Sanctum: Is it required to extend from Authenticatable?

On the one hand, I have an Eloquent model User that extends Illuminate\Database\Eloquent\Model. On the other hand, I have SanctumUser extends Authenticatable (https://laravel.com/docs/8.x/sanctum#issuing-api-tokens).
What I would like to do is, User extends Model, SanctumUser, but multiple inheritance is not possible in PHP 7.x.
I know that some traits are used in SanctumUser according to the documentation I've linked above. These traits are: use HasApiTokens, HasFactory, Notifiable;. Do you know if they are sufficient if I remove extends Authenticatable and replace it with extends Model (User would extend SanctumUser)?
Authenticable as alias for Illuminate\Foundation\Auth\User class has traits which has methods for authentication and authorization
<?php
namespace Illuminate\Foundation\Auth;
use Illuminate\Auth\Authenticatable;
use Illuminate\Auth\MustVerifyEmail;
use Illuminate\Auth\Passwords\CanResetPassword;
use Illuminate\Contracts\Auth\Access\Authorizable as AuthorizableContract;
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
use Illuminate\Contracts\Auth\CanResetPassword as CanResetPasswordContract;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Foundation\Auth\Access\Authorizable;
class User extends Model implements
AuthenticatableContract,
AuthorizableContract,
CanResetPasswordContract
{
use Authenticatable, Authorizable, CanResetPassword, MustVerifyEmail;
}
Even the default User model class which is available with any new standard Laravel installation has it extending the Authenticable class [use Illuminate\Foundation\Auth\User as Authenticatable;]
Default User Model class in a standard Laravel application has two more traits - Notifiable and HasFactory (since Laravel 8.x)
<?php
namespace App\Models;
use Illuminate\Notifications\Notifiable;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Foundation\Auth\User as Authenticatable;
class User extends Authenticatable
{
use HasFactory;
use Notifiable;
/**
* The attributes that are mass assignable.
*
* #var array
*/
protected $fillable = [
'name',
'email',
'password',
];
/**
* The attributes that should be hidden for arrays.
*
* #var array
*/
protected $hidden = [
'password',
'remember_token',
'two_factor_recovery_codes',
'two_factor_secret',
];
}
With Sanctum you may add trait HasApiToken to the User model class.
In order to easily integrate Laravel Sanctum or Jetstream or Laravel UI or Breeze for authentication and authorization to your app, its better to make User class extend Authenticable - plug and play

Class 'App\Scope\__name_Scope' not found in lumen (Laravel micro-framework)

I want to add a global scope to my project. and use it in some models.
so I create this code: (in app/scopes folder)
<?php
namespace App\Scopes;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Scope;
class GameStoreScope implements Scope
{
/**
* Apply the scope to a given Eloquent query builder.
*
* #param Builder $builder
* #param Model $model
* #return void
*/
public function apply(Builder $builder, Model $model)
{
//$storeId = \request()->header('Store');
//dd("asdad");
$builder->where('game_store_id', '=', 1);
}
}
And use it to my model like this:
<?php
namespace App;
use App\Scopes\GameStoreScope;
use Illuminate\Database\Eloquent\Model;
class Player extends Model
{
protected $guarded = [];
protected static function boot()
{
parent::boot();
static::addGlobalScope(new GameStoreScope);
}
but after run my project. always get this FatalError : Class 'App\Scopes\GameStoreScope' not found
Fix namespace ref link https://laravel.com/docs/8.x/eloquent#query-scopes
namespace App\Scopes;
use App\Scopes\GameStoreScope;
use Illuminate\Database\Eloquent\Model;
then run composer dump-autoload

Error when using Spate Media Library: "BadMethodCallException Call to undefined method Illuminate\Foundation\Auth\User::addMediaFromRequest()"

This is my User Model
namespace App;
use Illuminate\Database\Eloquent\Model;
use Spatie\MediaLibrary\HasMedia\HasMedia;
use Spatie\MediaLibrary\HasMedia\HasMediaTrait;
use Spatie\MediaLibrary\File;
use Illuminate\Notifications\Notifiable;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Foundation\Auth\User as Authenticatable;
class User extends Authenticatable implements HasMedia
{
use HasMediaTrait;
use Notifiable;
protected $fillable = [
'name', 'email', 'password',
];
protected $hidden = [
'password', 'remember_token',
];
protected $casts = [
'email_verified_at' => 'datetime',
];
public function role(){
return $this->belongsToMany('App/Role');
}
}
This is my UserController
namespace App\Http\Controllers;
use Illuminate\Foundation\Auth\User;
use Illuminate\Http\Request;
class UserController extends Controller
{
public function store(Request $request)
{
$user=new User;
$user->name=($request['name']);
$user->email=($request['email']);
$password=bcrypt($request['password']);
$user->password=$password;
$user_photo=$request['photo'];
$user->addMediaFromRequest('photo')->toMediaCollection('images');
$user->save();
return redirect('/admin');
}
}
I want to use Spatie Media Library and upload a photo for each user but I get this error related to Spatie Library:
"Call to undefined method
Illuminate\Foundation\Auth\User::addMediaFromRequest()".
I have read some related posts but I don't understand how to fix this.
Thank you.
Version 8 of the library has this documentation. Are you using the correct trait?
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Spatie\MediaLibrary\HasMedia;
use Spatie\MediaLibrary\InteractsWithMedia;
class YourModel extends Model implements HasMedia
{
use InteractsWithMedia;
}
Edited:
On another note, using HasMediaTrait is for version 7 so I assume you're using version 7. I think it's because you're importing the wrong User class. The User class used to implement HasMedia lies in the App\User namespace. But you're importing the User class from the Illuminate\Foundation\Auth\User namespace. So change it to use App\User; and you should be fine.
namespace App\Http\Controllers;
use App\User;
use Illuminate\Http\Request;
class UserController extends Controller
{
public function store(Request $request)
{
$user=new User;
$user->name=($request['name']);
$user->email=($request['email']);
$password=bcrypt($request['password']);
$user->password=$password;
$user_photo=$request['photo'];
$user->addMediaFromRequest('photo')->toMediaCollection('images');
$user->save();
return redirect('/admin');
}
}
https://docs.spatie.be/laravel-medialibrary/v8/basic-usage/preparing-your-model/

Laravel 5.8 "Class 'App\Exports\Auth' not found"

I am trying to download records related to authenticated user using maatwebsite/excel and here is my export:
<?php
namespace App\Exports;
use App\User;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Maatwebsite\Excel\Concerns\FromCollection;
class UsersExport implements FromCollection
{
/**
* #return \Illuminate\Support\Collection
*/
public function collection()
{
return Auth::user()->getrecs();
}
}
but when I try to visit the route related to this export I get:
"Class 'App\Exports\Auth' not found"
how to solve this?
You forgot to use Auth.
<?php
namespace App\Exports;
use Auth;
use App\User;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Maatwebsite\Excel\Concerns\FromCollection;
class UsersExport implements FromCollection
{
/**
* #return \Illuminate\Support\Collection
*/
public function collection()
{
return Auth::user()->getrecs();
}
}
FYI, Now Laravel has released version 7.

laravel QueryException table not found

I am not getting it that why i am facing it.
I created a migration name 2018_01_12_035551_create_inquiry_master_table
In my route file assign a route Route::get('lists-inquiry','InquiryController#listinquiry')->name('lists-inquiry');
Created a controller name InquiryController
Controller code:
<?php
namespace App\Http\Controllers\Admin;
use App\Http\Controllers\Controller as BaseController;
use Carbon\Carbon;
use Illuminate\Http\Request;
use App\InquiryMaster;
use Illuminate\Support\Facades\Input;
class InquiryController extends BaseController
{
public function listinquiry() {
$inquiry = InquiryMaster::all();
// dd($inquiry) Here i am getting error
return view('admin.list_inquiry')->with('listinquiry', $inquiry);
}
}
Created a model InquiryMaster
My model look like:
<?php
namespace App;
use Illuminate\Http\Request;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Input;
class InquiryMaster extends Model
{
/**
* The attributes that are mass assignable.
*
* #var array
*/
protected $fillable = [
'created_date_time',
'user_id',
'stitch_video_path',
'completion_status'
];
/**
* Indicates if the model should be timestamped.
*
* #var bool
*/
}
When i dd($inquiry) in controller it returns error
SQLSTATE[42S02]: Base table or view not found: 1146 Table 'bigball_travel.inquiry_masters' doesn't exist (SQL: select * from inquiry_masters)
It shows table inquiry_masters
and i created inquiry_master
add protected $table = 'inquiry_master'; to InquiryMaster model
so class would look like this.
<?php
namespace App;
use Illuminate\Http\Request;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Input;
class InquiryMaster extends Model
{
protected $table = 'inquiry_master';
protected $fillable = [
'created_date_time',
'user_id',
'stitch_video_path',
'completion_status'
];
}

Resources