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?
Related
I'm trying to save a document on the database in mongo usin a 'jenssegers/laravel-mongodb' model
this is the model
<?php
namespace App;
use Jenssegers\Mongodb\Eloquent\Model;
class Candidate extends Model
{
public function User(){
return $this->belongsTo(User::class);
}
}
and the controller
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Candidate;
class CandidateController extends Controller
{
public function __construct(){
//$this->middleware('jwt');
}
public function create(Request $request){
//var_dump($request->all());
$candidate = new Candidate();
$candidate->name($request['name']);
$candidate->source($request['source']);
$candidate->save();
}
}
I get this error
BadMethodCallException: Call to undefined method App\Candidate::name() in file /home/myuser/myproject/vendor/laravel/framework/src/Illuminate/Support/Traits/ForwardsCalls.php on line 50
The problem is clear but how I have to add a method to add the attributes on the model
Looking at docs it should be used same as in Eloquent so instead of
$candidate = new Candidate();
$candidate->name($request['name']);
$candidate->source($request['source']);
$candidate->save();
you can use:
$candidate = new Candidate();
$candidate->name = $request->input('name');
$candidate->source = $request->input('source');
$candidate->save();
I am trying to simply save a model to the database via a controller without setting the fields directly but instead having them set in the constructor of said object.
Here is the class handling the logic. If the commented out line
//$todoItem->item = $todo; is uncommented, it works fine and saves whatever is entered in $todo in the database. However, I would like to set that value in the constructor of the task object and not need to manually specify it. I found this question: Laravel with Eloquent doesn't save model properties on database suggesting I could set the property as protected and have it work, but that still does not.
NewTodo.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class NewTodo extends Controller
{
public function saveTodoItem(Request $request)
{
$todo = $request->input('content');
$todoItem = new \App\Task($todo);
//$todoItem->item = $todo;
$todoItem->save();
return view('testview', ['name' => $todoItem->getItem()]);
}
}
Task.php
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Task extends Model
{
protected $item = null;
function __construct($newItem)
{
$this->item = $newItem;
}
function getItem()
{
return $this->item;
}
}
Summary, database entry being stored as blank unless $todo->item is manually set when from what I can $todo->item is being set in the constructor.
You don't need to specify these in the constructor. You can use Mass Assignment
Model:
class Task extends Model
{
protected $fillable = ['item'];
}
Controller:
class NewTodo extends Controller
{
public function saveTodoItem(Request $request)
{
$todo = $request->input('content');
$todoItem = new \App\Task::create(['item' => $todo]);
return view('testview', ['name' => $todoItem->getItem()]);
}
}
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);
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.
I am trying to add a hasMany relation from my Project class to my Version class (Projects have many Versions).
I have these:
My Project class -
namespace App;
use Version;
use Jenssegers\Mongodb\Model as Model;
class Project extends Model
{
protected $fillable = ['title'];
public function versions()
{
return $this->hasMany('Version');
}
}
My Version class -
<?php
namespace App;
use Jenssegers\Mongodb\Model as Model;
class Version extends Model
{
protected $fillable = ['nickname'];
public function project()
{
return $this->belongsTo('Project');
}
}
And in a route -
public function createVersion(Request $request)
{
$projectID = $request->input('projectID');
$version = $request->input('version');
$project = Project::where('_id', $projectID)->first();
$version = new Version();
$version->projectID = $projectID;
$version->nickname = $version['nickname'];
$project->versions->save($version);
return $version;
}
I am getting the error - Class 'Version' not found - and it points to Model.php.
Am I missing adding a "use" somewhere, or am I missing something else entirely?
You have defined the class Version under the App namespace.
For that reason you must use
public function project()
{
return $this->belongsTo('App\Project');
}
in your Project class.