I've recently installed this package and configured everything with guide but some how it's not working!
By it's not working I mean it's not adding anything to database. I really don't know what is wrong with my configs but I've checked everything with guide 3 times and everything is correct but... I don't know
config/audit.php:
<?php
return [
'enabled' => env('AUDITING_ENABLED', true),
'implementation' => OwenIt\Auditing\Models\Audit::class,
'user' => [
'morph_prefix' => 'user',
'guards' => [
'web',
'api',
],
],
'resolver' => [
'user' => OwenIt\Auditing\Resolvers\UserResolver::class,
'ip_address' => OwenIt\Auditing\Resolvers\IpAddressResolver::class,
'user_agent' => OwenIt\Auditing\Resolvers\UserAgentResolver::class,
'url' => OwenIt\Auditing\Resolvers\UrlResolver::class,
],
'events' => [
'created',
'updated',
'deleted',
'restored',
'gold_mailed' => 'goldMailed',
'invited' => 'clientInvited',
],
'strict' => false,
'timestamps' => false,
'threshold' => 0,
'driver' => 'session',
'drivers' => [
'eloquent' => [
'table' => 'audits',
'connection' => null,
],
],
'console' => true,
];
My model that I want to audit:
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use OwenIt\Auditing\Contracts\Auditable;
use App\Models\Expansion;
use App\Models\Audit;
class Setting extends Model implements Auditable
{
protected $table = 'settings';
use \OwenIt\Auditing\Auditable;
protected $fillable = [
'expansion_id', 'season', 'advertiser_app', 'pvp_app', 'raid_app', 'version'
];
protected $auditInclude = [
'expansion_id', 'season', 'advertiser_app', 'pvp_app', 'raid_app', 'version'
];
public function Expansion()
{
return $this->hasOne(Expansion::class, 'id', 'expansion_id');
}
}
web.php:
Route::post('/setting' , 'Admin\SuperAdminController#saveSetting')->middleware('superadmin')->name('admin_save_setting');
Controller:
public function saveSetting(Request $request)
{
$sql = Setting::where('id', 1)->update([
'expansion_id' => $request['expansion_id'],
'season' => $request['season'],
'advertiser_app' => $request['advertiser_app'],
'pvp_app' => $request['pvp_app'],
'raid_app' => $request['raid_app'],
'version' => $request['version']
]);
if ($sql) {
toastr()->success('Settings successfully updated.');
return redirect()->back();
}
toastr()->error('Something went wrong!');
return redirect()->back();
}
I don't know what infos do you need but I think this is enough
I think my problem is with "driver" in config file , I don't know if that's correct or not
[UPDATED]
Based on the controller code you showed, it didn't work because your code is being called using Builder style, and the package only works when it is called using Eloquent style.
Documentation link
So, maybe you need to change your code to:
$setting = Setting::where('id', 1)->firstOrFail();
$setting->update([
'expansion_id' => $request['expansion_id'],
'season' => $request['season'],
'advertiser_app' => $request['advertiser_app'],
'pvp_app' => $request['pvp_app'],
'raid_app' => $request['raid_app'],
'version' => $request['version']
]);
now I have another problem -_-
this is my controller:
$sql = Raid::findOrFail($request['id']);
$sql = $sql->update($request->all());
I have a array in my table , after update value will be like this:
"{\"Plate\":0,\"Cloth\":0,\"Mail\":0,\"Leather\":0}"
but it should be:
{"Plate":"0","Cloth":"0","Mail":"0","Leather":"0"}
so I will get an error
before this , I was updating like this and it was ok:
$sql = Raid::where('id', $request['id'])->update($request->all());
and this is my mode (traders and class_traders is fields that I have problem with):
use SoftDeletes;
use \OwenIt\Auditing\Auditable;
protected $table = 'raid';
protected $dates = ['date_and_time','deleted_at'];
protected $fillable = [
'admin_id', '....
];
protected $casts = [
'bosses' => 'array',
'traders' => 'array',
'class_traders' => 'array',
'boosters' => 'array',
];
Related
I have all my rules in the Validation config file, like the documentation suggest:
https://codeigniter4.github.io/userguide/libraries/validation.html#saving-sets-of-validation-rules-to-the-config-file
For example:
public $userCreate = [
'first_name' => [
'label' => 'First Name',
'rules' => 'required|string|max_length[60]'
],
'last_name' => [
'label' => 'Last Name',
'rules' => 'required|string|max_length[60]',
],
'email' => [
'label' => 'Auth.email',
'rules' => 'required|max_length[254]|valid_email|is_unique[users.email]',
],
];
In my controllers I can access my validation groups like this:
$validation = \Config\Services::validation();
$rules = $validation->getRuleGroup('userCreate');
As my app gets bigger, I need more and more validation rules, so the question is, is there a way to organize them in separate files and not to have all of them in a single config file? Something like the custom rules, which are loaded in the config file and stored separately.
Steps
Create a custom directory for storing your validation rules. I.e app/Validation.
Create a class under that directory for your 'User' rules. I.e: app/Validation/UserRules.php
<?php
namespace App\Validation;
class UserRules
{
public function create()
{
return [
'first_name' => [
'label' => 'First Name',
'rules' => 'required|string|max_length[60]'
],
'last_name' => [
'label' => 'Last Name',
'rules' => 'required|string|max_length[60]',
],
'email' => [
'label' => 'Auth.email',
'rules' => 'required|max_length[254]|valid_email|is_unique[users.email]',
],
];
}
public function update()
{
return [
// Add 'User' update rules here.
];
}
}
In the \Config\Validation config file, set the relevant 'User' validation rules in the constructor. I.e:
<?php
namespace Config;
use CodeIgniter\Config\BaseConfig;
// ...
class Validation extends BaseConfig
{
// --------------------------------------------------------------------
// Setup
// --------------------------------------------------------------------
public $userCreate = [];
public $userUpdate = [];
public function __construct()
{
$this->userCreate = ($userRules = new \App\Validation\UserRules())->create();
$this->userUpdate = $userRules->update();
}
// ...
}
In your Controllers, you may access validation groups as usual.
Thanks to #steven7mwesigwa I came up with a solution that suits me the most.
First I created separate classes inside the App/Validation folder. For example these 2 classes:
App\Validation\Auth.php
<?php
namespace App\Validation;
class Auth {
public $login = [
'email' => [
'label' => 'E-mail',
'rules' => 'required|max_length[254]|valid_email',
],
'password' => [
'label' => 'Password',
'rules' => 'required',
],
'remember' => [
'label' => 'Remember me',
'rules' => 'if_exist|permit_empty|integer',
]
];
}
App\Validation\User.php
<?php
namespace App\Validation;
class User {
public $userCreate = [
'first_name' => [
'label' => 'First Name',
'rules' => 'required|string|max_length[60]',
],
'last_name' => [
'label' => 'Last Name',
'rules' => 'required|string|max_length[60]',
],
'email' => [
'label' => 'E-mail',
'rules' => 'required|max_length[254]|valid_email|is_unique[users.email]',
],
];
}
The next step is to add a construct method to the existing validation config file:
App\Config\Validation.php
<?php
namespace Config;
use CodeIgniter\Config\BaseConfig;
class Validation extends BaseConfig {
...
// --------------------------------------------------------------------
// Rules
// --------------------------------------------------------------------
public function __construct() {
$ruleGroups = [
new \App\Validation\Auth(),
new \App\Validation\User(),
];
foreach ($ruleGroups as $ruleGroupClass) {
foreach ((array) $ruleGroupClass as $groupName => $rules) {
$this->{$groupName} = $rules;
}
}
}
}
I have three tables.
1-customer
2-seller
3-affilate
I am fresher for LARAVEL, now i am studying on LARAVEL 8, so i wanted to make login with token api from these tables. Then how can i make please please please help me i am not able to find any proper solutions.
I have added in config/auth.php
'guards' => [
'admin' => [
'driver' => 'session',
'provider' => 'admin',
],
'affilate' => [
'driver' => 'session',
'provider' =>'affilate',
],
'customer' => [
'driver' => 'session',
'provider' => 'customer',
],
'seller' => [
'driver' => 'session',
'provider' => 'seller',
],
],
'providers' => [
'admin' => [
'driver' => 'eloquent',
'model' => App\Models\Admin::class,
],
'customer' => [
'driver' => 'eloquent','model'=>App\Models\Customer::class,
],
'seller' => [
'driver' => 'eloquent',
'model' =>App\Models\Seller::class,
],
'affilate' => [
'driver' => 'eloquent',
'model' =>App\Models\Affilate::class,
],
],
Here Is my First Models For Customer
<?php
namespace App\Models;
use Laravel\Sanctum\HasApiTokens;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Notifications\Notifiable;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Foundation\Auth\User as Authenticatable;
class Customer extends Authenticatable
{
use HasFactory, HasApiTokens, Notifiable;
protected $table = 'customer';
protected $primaryKey = 'id';
protected $fillable = [
'name', 'email', 'mobile', 'countryCode','email_verified_at',
'email_verified','passWord','confirmPassword'
];
protected $hidden = [
'passWord', 'remember_token',
];
}
Controller For Customer
<?php
namespace App\Http\Controllers\API;
use Carbon\Carbon;
use App\Models\Customer;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Validator;
class CustomerController extends Controller
{
public function customerLogin(Request $request)
{
if (Auth::guard('customer')->attempt($credentials))
{
$registerCustomer = Auth::Customer();
$token = $registerCustomer->createToken( $registerCustomer->name)->accessToken;
$success['success'] = true;
$success['message'] = "Success! you are logged in successfully";
$success['token'] = $token->plainTextToken;
$success['tokenExpiryTime'] = 2592000000; //converted 30days minutes in miliseconds
$success['customerName'] = $registerCustomer->name;
return response()->json(['success' => $success ], $this->successStatus);
}else {
return response()->json(['error'=>'Unauthorised'], 401);
}
}
public function registerCustomer(Request $request)
{
$validator = Validator::make($request->all(), [
'name' => 'required|min:3|max:55',
'email' => 'required|email|unique:customers',
'mobile' => 'required|digits:10|unique:customers',
'countryCode' => 'required|digits:6|unique:customers',
'passWord' => 'required|alpha_num|min:8',
'confirmPassword' => 'required|same:passWord|alpha_num|min:8',
]);
if($validator->fails())
{
return response()->json(['error'=>'Unprocessable Entity','validationErrors' => $validator->errors()], 422);
}
$customerData = array(
'name' => $request->name,
'email' => $request->email,
'mobile' => $request->mobile,
'countryCode' => $request->countryCode,
'passWord' => Hash::make($request->passWord),
'confirmPassword' => Hash::make($request->confirmPassword),
'email_verified' => '0',
'email_verified_at' => Carbon::now(),
);
$saveCustomerData = Customer::create($customerData);
return $this->customerLogin($request);
}
}
And Here Is My Routes
<?php
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\Api\CustomerController;
Route::post('/customer/register', [CustomerController::class,
'registerCustomer']);
Route::post('/customer/login', [CustomerController::class,
'customerLogin']);
I wanted To Know Where is my mistake its Showing ErrorErrorException: Undefined variable $credentials in file
Thanks Please Please Please Please Please Please Help Me
try this manual method as you told attempt() is not working in your case
public function customerLogin(Request $request)
{
$customer = Customer::where('email', $request->email)->first();
if (!$customer) {
return response()->json(['error' => 'email not found'], 400);
}
if (Hash::check($request->email, $customer->password)) {
auth()->login($customer);
$registerCustomer = Auth::user();
$token = $registerCustomer->createToken($registerCustomer->name)->accessToken;
$success['success'] = true;
$success['message'] = "Success! you are logged in successfully";
$success['token'] = $token->plainTextToken;
$success['tokenExpiryTime'] = 2592000000; //converted 30days minutes in miliseconds
$success['customerName'] = $registerCustomer->name;
return response()->json(['success' => $success], $this->successStatus);
} else {
return response()->json(['error' => 'Unauthorised'], 401);
}
}
So i changed config/auth.php to change default auth table from user to accounts like this :
'defaults' => [
'guard' => 'web',
'passwords' => 'accounts',
],
'guards' => [
'web' => [
'driver' => 'session',
'provider' => 'accounts',
],
'api' => [
'driver' => 'token',
'provider' => 'accounts',
'hash' => false,
],
],
'providers' => [
'accounts' => [
'driver' => 'eloquent',
'model' => App\Akun::class,
],
],
i have changed App\Akun Model configuration same as App\User Model :
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
class Akun extends Authenticatable
{
use HasFactory;
protected $table = 'accounts';
protected $fillable = [
'nip',
'password',
];
protected $hidden = [
'password',
'remember_token',
];
protected $casts = [
'email_verified_at' => 'datetime',
];
}
And My Login system is working too. It can return back if the NIP or Password wrong and give error message too. Here my login code:
function checklogin(Request $request){
$this->validate($request,[
'nip' => 'required',
'password' => 'required'
]);
$akun_data = array(
'nip' => $request->get('nip'),
'password' => $request->get('password')
);
if(Auth::attempt($akun_data)){
return redirect('login/successlogin');
}else{
return back()->with('pesan','NIP atau Password salah');
}
}
it was working and return correctly to login/successlogin and run the code in there. But when i tried to add middleware('auth') to the route of login/succeslogin , it'll always return back to login page even when i gave the correct nip and password
Route::get('/login/successlogin',[LoginController::class, 'successlogin'])->middleware('auth');
You can just add a new guard that will be for your accounts table and call it 'accounts' and then in your routes i.e. you can provide route grouping by guard i.e.
Route::middleware('auth:accounts')->group(function () { ...other protected routes... });
I have solved this, My way to change default auth table is correct. But I forgot to add id column there. just add this code in your New Default Auth Table Migration
$table->id();
I am not able to add field which is not available in my database
I have tried adding
$this->crud->addFields([
[
'name' => 'coupon_type',
'label' => 'Coupon For',
'type' => 'select_from_array',
'options' => [
// Options
],
'allows_null' => true,
'default' => 1,
'attributes' => [
'id' => 'coupon_type'
]
]
]);
I want to add fields in my create page.
You can do this by defining accessors for your "virtual" attributes
public function getIsAdminAttribute()
{
return $this->attributes['admin'] == 'yes';
}
and defining the appends on your model
protected $appends = ['is_admin'];
Find everything in the docs here:
https://laravel.com/docs/5.8/eloquent-serialization#appending-values-to-json
I see from here : https://github.com/andersao/laravel-validator
For example the code like this :
use \Prettus\Validator\LaravelValidator;
class PostValidator extends LaravelValidator {
protected $rules = [
ValidatorInterface::RULE_CREATE => [
'title' => 'required',
'text' => 'min:3',
'author'=> 'required'
],
ValidatorInterface::RULE_UPDATE => [
'title' => 'required'
]
];
}
I want to add condition if user is member(if(Auth::id())) then the field author not required
So the validator to be like this :
ValidatorInterface::RULE_CREATE => [
'title' => 'required',
'text' => 'min:3'
],
The author required if the user is guest(no login)
Whether it can be done?
the Laravel documentation has a section on custom validators
Since you cannot execute instructions in a class properties, you can try to override the create method of your Repository instance, in order to modify the $rules parameter before the create actually takes place.
So in your Repository class, override the method create:
public function create(array $attributes)
{
$oldRule = $this->rules[ValidatorInterface::RULE_CREATE]['author'];
if(Auth::guest()){ // or use the preferred check
unset($this->rules[ValidatorInterface::RULE_CREATE]['author']);
}
$this->makeValidator();
$res = parent::create($attributes);
$this->rules[ValidatorInterface::RULE_CREATE]['author'] = $oldRule;
return $res;
}
EDIT
Another method could be specifying custom validation logic in your PostValidator as follows:
use \Prettus\Validator\LaravelValidator;
class PostValidator extends LaravelValidator {
const RULE_CREATE_FOR_MEMBER = 'RULE_CREATE_FOR_MEMBER';
protected $rules = [
ValidatorInterface::RULE_CREATE => [
'title' => 'required',
'text' => 'min:3',
'author'=> 'required'
],
self::RULE_CREATE_FOR_MEMBER => [
'title' => 'required',
'text' => 'min:3'
],
ValidatorInterface::RULE_UPDATE => [
'title' => 'required'
]
];
public function passes($action = null)
{
if($action == ValidatorInterface::RULE_CREATE && \Auth::id()) {
$action = self::RULE_CREATE_FOR_MEMBER;
}
return parent::passes($action);
}
}
But again, you need to override the standard behavior, it's up to you to decide which is the simplest solution for your needs.