When i run db seed command on terminal but unfortunatly i get error Attempt to read property client_id on null please help me how can i resolve that ? thank u
Contractor model
class Contractor extends Model
{
protected static function booted() {
static::creating(function ($model) {
$model->client_id = auth()->user()->client_id;
});
}
}
There is not any authenticated user when you run the database seeders. so you have to make the user in your factory or authenticate the fake user in your database seeder
During seeders, there's no auth()->user().
The most simple fix would be probably this:
$model->client_id = auth()->user()?->client_id;
It will work with PHP 8, from what I remember.
Or, if not, just add if (auth()->check()) before adding this line.
Related
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
I'm developing an application to help companies manage its products.
First, the company should register itself to the app with an active admin user. The new registered company is not active by default.
I want to check if the user company active before logging in.
For example:
I want to register Nike to the app . the registered user is admin#nike.com
if I want to login with "admin#nike.com", it should not be possible till Nike company become active.
Database structure is like this:
Company:
com_id
com_name
com_isActive
User:
user_id
user_name
email
password
user_isAdmin
user_com_id
Any suggestions how to fix it?
Probably you have relation in your User model. Something like
public function company()
{
return $this->belongsTo(Company::class);
}
In your LoginController (if you want it only there) or in some middleware you can check request()->user()->company()->is_active and do whatever you need. RedirectIfAuthenticated middleware is a variant if you don't want to make new middleware
1.First add column to the company table called isActive and adminEmail
2.Make a new Validation Rule called CheckCompanyStatus by this command
php artisan make:rule CheckCompanyStatus
3.Inside this rule put the following logic inside passes()
$data = \Company::where('adminEmail',$value)->first();
if($data->isActive == true) {
return true;
} else {
return false;
}
Go to LoginController and override the function validateLogin() and put this logic
protected function validateLogin(Request $request)
{
$this->validate(['email'=>new \CheckCompany, 'password'=>
'required]);
}
Make sure to add adminEmail and isActive in your company registration form as well
and use all the classes at the top
Let me know if u have any errors
In my crud controller I am trying to get the name of the person who is currently being edited.
so
http://192.168.10.10/admin/people/93/edit
In the people crud controller
public function setup() {
dd(\App\Models\People::get()->first()->name)
}
This returns the first person not the person currently being edited.
How do I return the current person (with an id of 93 in this example)
Ok, So since you use backpack look into CrudController to see how the method looks:
public function edit($id)
{
$this->crud->hasAccessOrFail('update');
$this->data['entry'] = $this->crud->getEntry($id);
$this->data['crud'] = $this->crud;
$this->data['fields'] = $this->crud->getUpdateFields($id);
$this->data['id'] = $id;
return view('crud::edit', $this->data);
}
So now you can overwrite the edit function and change whatever you want. You can even create a custom edit page if you so wish.
Setup on the other hand is usually used to add things like
$this->crud->addClause(...);
Or you can even get the entire constructor and put it in the setup method because setup call looks like this:
public function __construct()
{
// call the setup function inside this closure to also have the request there
// this way, developers can use things stored in session (auth variables, etc)
$this->middleware(function ($request, $next) {
$this->setup();
return $next($request);
});
}
So you could do something like \Auth::user()->id;
Also it's normal to work like this. If you only use pure laravel you will only have access to the current id in the routes that you set accordingly.
Rahman said about find($id) method. If you want to abort 404 exception just use method findOrFail($id). In my opinion it's better way, because find($id)->name can throw
"Trying to get property of non-object error ..."
findOrFail($id) first fetch user with specified ID. If doesn't exists just throw 404, not 500.
The best answer is:
public function edit($id)
{
return \App\Models\People::findOrFail($id);
}
Good luck.
you need person against id, try below
public function setup($id) {
dd(\App\Models\People::find($id)->name);
}
My basic aim is to extend the package class and override a method in it.
I have used Fnatte's ans as a reference : How to extend laravel 4 core?
Sentry2 is a package that i am using for authentication in larval 4.
A user can be logged in using Sentry::login($credentials)
I want to override the login method of the Sentry package and remove the check for activating the user(i have commented it in the code below)
public function login(UserInterface $user, $remember = false)
{
#prevent throwing error if not activated !
// if ( ! $user->isActivated())
// {
// $login = $user->getLogin();
// throw new UserNotActivatedException("Cannot login user [$login] as they are not activated.");
// }
$this->user = $user;
// Create an array of data to persist to the session and / or cookie
$toPersist = array($user->getId(), $user->getPersistCode());
// Set sessions
$this->session->put($toPersist);
if ($remember)
{
$this->cookie->forever($toPersist);
}
// The user model can attach any handlers
// to the "recordLogin" event.
$user->recordLogin();
}
Steps i have done till now :
1. Created a app/lib folder and added my extension class CustomSentry in it.
2. Added the app/lib folder to composer.json class map
app/lib/CustomSentry.php :
use Cartalyst\Sentry\Sentry;
use Cartalyst\Sentry\Users\UserInterface;
class CustomSentry extends Sentry{
public function login(UserInterface $user, $remember = false){
$this->user = $user;
// Create an array of data to persist to the session and / or cookie
$toPersist = array($user->getId(), $user->getPersistCode());
// Set sessions
$this->session->put($toPersist);
if ($remember)
{
$this->cookie->forever($toPersist);
}
// The user model can attach any handlers
// to the "recordLogin" event.
$user->recordLogin();
}
}
4. Created a service provider app/lib/CustomSentryServiceProvider
use Cartalyst\Sentry\SentryServiceProvider
class CustomSentryServiceProvider extends SentryServiceProvider{
//What should i put it here?
}
5. Register the service provider in app/config/app.php
'CustomSentryServiceProvider'
6. Use it as :
Sentry::login($credentials);
Well i figured out the answer to the question.
The better way to extend the class would be to use the same name. Since i am already using namespacing it would help reduce the confusion.
Assuming my apps name is MyApp i will only need to replace:
Cartalyst\Sentry
by:
MyApp\Cartalyst\Sentry
the rest of the SentryService provider can be copied as it is.
Then i can call the login method the normal way
Sentry::login($credentials)
I have doubts, because you have to copy whole package/Sentry. Did you tried to extend the Sentry class? This this the best way to do what you want to achieve.
In few project we don't need database, so how we setup cakephp on local machine without modification in database config? Right now what I done ...I created database and modified config file. But my database has no table, its just wastage of database....so please suggest better way to do this.
Thank you in advance..
With CakePHP 2.3.x I simply use an empty string as a datasource in the database configuration and it works fine.
The database configuration (app/Config/database.php) is almost empty, it looks like this:
class DATABASE_CONFIG {
public $default = array(
'datasource' => '',
);
}
You have to tell your AppModel not to use DB tables, otherwise you'll get an error: "Datasource class could not be found". It's not enough to set the $useTables in descendant models only.
class AppModel extends Model {
public $useTable = false;
}
I dindn't experienced any problems with that yet.
Cakephp actually try to connect to a database no matter that you don’t use a table
so using this
class MyModel extends AppModel {
public $useTable = false;
}
it will be just a mistake , creating application on cakephp is piece of cake. Here are some steps you have to do in order to start developing without a database.
Create fake dbo source
Create file DboFakeDboSource.php in app/Model/Datasource/Dbo/ and put the following code in it
class DboFakeDboSource extends DboSource {
function connect() {
$this->connected = true;
return $this->connected;
}
function disconnect() {
$this->connected = false;
return !$this->connected;
}
}
Set the default connection
The next step is to tell cakephp to use dbo source by default. Go and change default connection in database.php to be like this
var $default = array(
'driver' => 'FakeDboSource'
);
Fine tuning the model
The third step is to be sure that $useTable = false; is included in every model, so add it in AppModel.php
You can just leave default datasourse settings empty in database.php and for Models you use, specify that it doesn't need corresponding table in DB like following:
class MyModel extends AppModel {
public $useTable = false;
}