So in Profile table I created new field named 'rayon_id' which is FK of my new table Rayon's 'id'
both model had the relationship coded.
In Rayon.php
/**
* #return \yii\db\ActiveQuery
*/
public function getProfiles()
{
return $this->hasMany(Profile::className(), ['rayon_id' => 'id']);
}
and In Profile.php (Overridden model)
namespace app\models;
use dektrium\user\models\Profile as BaseProfile;
class Profile extends BaseProfile
{
public function getRayon()
{
return $this->hasOne($this->module->modelMap['Rayon'], ['id' => 'rayon_id']);
}
And here my overridden controller AdminController.php
namespace app\controllers\user;
use dektrium\user\controllers\AdminController as BaseAdminController;
use Yii;
use yii\helpers\Url;
use backend\models\Rayon;
class AdminController extends BaseAdminController
{
public function actionUpdateProfile($id)
{
Url::remember('', 'actions-redirect');
$user = $this->findModel($id);
$profile = $user->profile;
$rayon = $profile->rayon;
I got an error while added $rayon = $profile->rayon;
Why am I getting the undefined index?
Found the answer for fixing the error.
I use Rayon model, and edit the getRayon function
namespace app\models;
use backend\models\Rayon;
use dektrium\user\models\Profile as BaseProfile;
class Profile extends BaseProfile
{
public function getRayon()
{
return $this->hasOne(Rayon::className(), ['id' => 'rayon_id']);
//return $this->hasOne($this->module->modelMap['Rayon'], ['id' => 'rayon_id']);
}
Thank you.
Related
I’m kind of new to Laravel and the whole API architecture, so my question may seem dumb at first.
My basic setup:
Laravel 8;
PHP 8;
routes\api.php
Route::post('/categories/',[ApiCategoriesInsertController::class, 'insertCategories'], function($insertCategoriesResults) {
return response()->json($insertCategoriesResults);
})->name('api.categories.insert');
\app\Http\Controllers\ApiCategoriesInsertController.php (created with php artisan make:controller)
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Route;
// Custom models.
use App\Models\CategoriesInsert;
class ApiCategoriesInsertController extends Controller
{
private mixed $ciAPI;
public function __construct(Request $req)
{
}
public function insertCategories(Request $req): array
{
$this->ciAPI = new CategoriesInsert(['testing'=>'debug']);
return [‘status’ => ‘OK’];
}
}
\app\Models\CategoriesInsert.php (created with php artisan make:model)
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class CategoriesInsert extends Model
{
use HasFactory;
public function __construct(array $objParameters)
{
}
}
When I make a post to http://localhost:8000/api/categories, Laravel logs the following error:
local.ERROR: Too few arguments to function App\Models\CategoriesInsert::__construct(), 0 passed in … Too few arguments to function App\\Models\\CategoriesInsert::__construct(), 0 passed in …
Anyone knows what’s wrong or missing in my architecture?
Thanks!
Make your model's constructor compatible with parent.
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class CategoriesInsert extends Model
{
use HasFactory;
public function __construct(array $attributes = [])
{
parent::__construct($attributes);
}
}
Also, notice, that when you call new CategoriesInsert(['testing'=>'debug']), you do not save the data in your database. Use:
$insert = new CategoriesInsert(['testing'=>'debug']);
$insert->save();
Or:
CategoriesInsert::create(['testing'=>'debug']);
you don't need to pass data to model
change your code to bellow code:
public function insertCategories(Request $req): array
{
CategoriesInsert::create(['testing' => 'debug']);
return [‘status’ => ‘OK’];
}
key of passed array is your filed name in database, and value stored data
also you should define fillable parameter in model
protected $fillable = [
'title',
'slug',
'priority',
];
I'm new to CodeIgniter 4 but I have previously used Laravel - so I do know what I'm doing for the most part. I'm trying to build a simple CRUD app on CI4 and in my UserController, it keeps saying that App\Models\User is not found. And as a result - anything I get from the model class, is NULL.
Here's my Model & Controller code:
<?php
namespace App\Models;
use CodeIgniter\Model;
class User extends Model
{
protected $table = 'users';
protected $primaryKey = 'id';
protected $allowedFields = ['name', 'email'];
public function getUsers()
{
return $this->findAll();
}
}
UserController.php:
<?php
namespace App\Controllers;
use App\Models\User;
class UserController extends BaseController
{
public function index()
{
$model = model(User::class);
$data['users'] = $model->getUsers();
return view('user_view', $data);
}
public function create()
{
return view('add_user');
}
public function store()
{
$model = model(User::class);
$data = [
'name' => $this->request->getVar('name'),
'email' => $this->request->getVar('email')
];
$model->insert($data);
return $this->response->redirect(site_url('users-list'));
}
}
The error is in the index() method. If I use the $model = model() approach - I get "getUsers() is NULL" error. If I use the $model = new User(); approach - I simply get "Model not Found". And as you can see - I already have included the App\Models\User in the header.
What am I missing?
I used laravel Auditor in a model and it works very well as following:
use Illuminate\Database\Eloquent\Model;
use OwenIt\Auditing\Contracts\Auditable;
class Contracts extends Model implements Auditable
{
use \OwenIt\Auditing\Auditable;
protected $fillable=['condatereceived'];
public function user()
{
return $this->belongsTo(User::class);
}
}
But I want to used it in the controller as :
public function updatecomplated(Request $request, $id,Contracts $contract ,Auditor $auditor)
{
Contracts::where('id', $id)
->update(['complated' => 50, 'conuploadby' => Auth::id(),'constatus' =>'Need To Active' ]);
if ($audit = $auditor->execute($contract)) {
$auditor->prune($contract);
}
return redirect()->back();
}
The code in controller give me error:
Call to undefined method OwenIt\Auditing\Facades\Auditor::execute()
any ideas to use auditor in the controller, please.
try this package its easy with good documentation
simply add this to your table
$table->auditable();
and this to your model
namespace App;
use Yajra\Auditable\AuditableTrait;
class User extends Model
{
use AuditableTrait;
}
thats it now simply get your auditor by calling
$user->creator // for who create
and
$user->updater //for who update data
for more information click here for check trait
Hope this helps
I have the following method in my controller:
public function store(Request $request){
$data = session()->get('dmca');
// return $data;
$notice = Notice::open($date);
$notice->useTemplate($request->input('template'));
$notice->save();
return Notice::first();
}
When the controller runs i get the following error:
Now i do have the following modal class:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Notice extends Model {
protected $fillable = [
'provider_id',
'infringing_title',
'infringing_link',
'original_link',
'original_description',
'template',
'content_removed'
];
public static function open(array $attributes) {
return new static($attributes);
}
public function useTemplate($template) {
$this->template = $template;
}
}
Now why am i getting this error in laravel class not found , even though i have this class defined ??
Just use use keyword at the top of your controller class.
<?php
namespace App\Http\Controllers;
use App\Notice; // <------------------ use here
use App\Http\Controllers\Controller;
class UserController extends Controller
{
public function store(Request $request)
{
$data = session()->get('dmca');
// return $data;
$notice = Notice::open($date);
$notice->useTemplate($request->input('template'));
$notice->save();
return Notice::first();
}
}
The use keyword helps php to recognize the class Notice within a certain namespace
Hope this helps!
Add this line to the top of a controller:
use App\Notice;
Or use full namespace:
$notice = \App\Notice::open($date);
I get the following error while trying to retrieve a array list which we use to load into the view to create a select list.
Error:
Call to undefined method Illuminate\Database\Query\Builder::albums
In our controller we are using following:
$albums = \Auth::user()->albums->lists('name', 'id');
And in model Albums.php we are using:
<?php namespace App\Models;
use Illuminate\Database\Eloquent\Model;
/**
* App\Models\Albums
*
*/
class Albums extends Model
{
protected $table = 'albums';
}
In in our main file:
public function albums()
{
return $this->hasMany('App\Models\Albums', 'name', 'id');
}
The problem is \Auth::user() object does not have albums function
The albums() function must be created in User model class
After searching around, i have solved it by adding the following to the User() class.
public function albums()
{
return $this->belongsTo('App\Media');
}