Laravel PHPUnit Call to a member function connection() on null - laravel

I am learning testing on Laravel and I am unable to continue testing Eloquent by this error:
Error: Call to a member function connection() on null
This is my code:
public function test_users_can_follow_each_other()
{
$this->userOne = User::find(1);
$this->userTwo = User::find(2);
$this->userOne->followedBy($this->userTwo->id);
$this->assertTrue($this->userTwo->isFollowedBy($this->userOne->id));
}
I am also using the trait use DatabaseTransactions;
The database is already created with 2 records on users table. Do I need to config anything else, thanks!

replace
PHPUnit\Framework\TestCase
with
Tests\TestCase

Related

How to call job from traits in laravel?

I want to use a job in a trait . Lumen give me following error:
Call to undefined method App\Consumers\Journal::dispatch()
this is my code
use App\Jobs\CreateFact;
trait Ledger
{
private function save() {
$this->dispatch(new CreateFact($entries));
}
}
Please help to resolve with issue. I am new in lumen
You can dispatch the job in the trait using:
CreateFact::dispatch($entries);

osiset / laravel-shopify Error Call to a member function api() on null

I've installed the plugin and I followed the instructions for installation.
Now, I received the following error when I call the api method:
Error
Call to undefined method stdClass::api()
My user model:
use Osiset\ShopifyApp\Contracts\ShopModel as IShopModel;
use Osiset\ShopifyApp\Traits\ShopModel;
class User extends Authenticatable implements IShopModel
{
use Notifiable, ShopModel;
...
My controller:
public function themeUpdate(ShopvoteShops $shopvoteShop, $request = null)
{
$user = DB::table('users')->first();
$shopvoteShop = ShopvoteShops::where('url', $user->name)->first();
$shop = Auth::user();
$response = $user->api()->rest('GET', '/admin/script_tags.json');
Basically, the instances User and Auth everytime are empty and I can't call for example $shop->api(), because Error Call to a member function api() on null.
I will be glad to hear how to fix this issue.
Thank you!
You don't need to fetch the user from the database. osiset/laravel-shopify does automatically add shop- and api-informations to the laravel-authentification Auth::user() after the user has successfully authenticated himself in the shop.
Take a look into the docs: https://github.com/osiset/laravel-shopify/wiki/Usage
Otherwise, instead
$user->api()->rest('GET', '/admin/script_tags.json');
try
$shop->api()->rest('GET', '/admin/api/2021-04/script_tags.json');
2021-04 stands for the API-Version.
More Informations you will find in the shopify dev-documentaion: https://shopify.dev/docs/admin-api/rest/reference/online-store/scripttag

Laravel Eloquent model events on created user

I'm trying to automatically create a profile for a user when a user is created.
I'm using the created event and overriding the boot() method. But I when call the create() method on user->profile->create(), it says create was called on null. I checked and profile is null in this.
Here's the code:
static::created(function ($user) {
// it returns profile as null, thus create() can't be used on null.
$user->profile->create(['title' => $user->username,]);
});
Can anyone help me understand this? It's working in my tutor's code, and he is using Laravel 5.8 but I have version 7.1.
$user->profile returns the related model if any exists. You have to do $user->profile() which returns a query builder to query the relation. Try to do it like so:
$user->profile()->create(['title' => $user->username,]);

function connection() on null when use database

i'm new on lumen , i start a lumen project with some table. but when I want get some table use this code
Course::all();
lumen said Call to a member function connection() on null
FatalErrorException in Model.php line 3245:
Call to a member function connection() on null
what's wrong ?
in lumen by default Eloquent is off , you should enable that.
for that you should write this code on your app.php in bootstrap folder
$app->withEloquent()

Error using save( ) in Laravel command

I am setting up my first Laravel command, but having an issue saving the the record.
My model:
Public static function contractorDecline(){
return DB::table('job_interviews')
->where('job_interviews.status', '=', 'awaiting contractor response')
->get();
}
I set up a command to change the status after 24 hours with no action:
public function fire()
{
//
$tooLate = Carbon::Now()->subHours(24);
$interviews = JobInterview::contractorDecline();
//
foreach($interviews as $interview){
if ($interview->response_received_on <= $tooLate){
$interview->status = 'contractor declined interview';
$interview->save();
}
}
return('finished');
}
When I run the command, I am getting the error:
"Call to undefined method stdClass::save()"
What do I need to add to the controller to be able to use save() in the command?
You are using Fluent which returns a simple object and calling Eloquent save method on it. If you want to update the row you should use Eloquent instead. Assuming you have a JobInterview class(Model) for the job_interviews table, you can code:
JobInterview::whereStatus('awaiting contractor response')->get();

Resources