Hi i am trying to use a model for a test but i get Class 'App\UserRole' not found this is my controller where i am calling it
<?php
namespace App\Http\Controllers;
use App\UserRole;
use Illuminate\Http\Request;
class TestController extends Controller
{
public function index()
{
$role = UserRole::get();
die(var_dump($role));
}
}
and this is my model
<?php
use \Illuminate\Database\Eloquent\Model as Eloquent;
class UserRole extends Eloquent
{
public $table = 'role';
public $primaryKey = 'id_role';
public $timestamps = false;
const ADMIN = 1;
const OPERATOR = 2;
const CUSTOMER = 3;
}
I dont know what im missing, i tried to do the same with User model and it works perfect, also my table is created and populated.
Add a namespace to your model
namespace App;
Should look like this:
<?php
namespace App;
use \Illuminate\Database\Eloquent\Model as Eloquent;
class UserRole extends Eloquent
{
public $table = 'role';
public $primaryKey = 'id_role';
public $timestamps = false;
const ADMIN = 1;
const OPERATOR = 2;
const CUSTOMER = 3;
}
You should add in your model
namespace App;
Related
I have i like query in my model but it doesn't work
Model:
<?php namespace App\Models;
use CodeIgniter\Model;
class SearchModel extends Model
{
protected $table = 'sport_tbl';
protected $primaryKey = 'id';
protected $returnType = 'array';
public function search($word)
{
$this->db->like('title', $word);
$res = $this->db->get('sport_tbl')->result_array();
return $res;
}
}
Controller:
<?php namespace App\Controllers\api;
use App\Controllers\BaseController;
use App\Models\SearchModel;
class Search extends BaseController
{
public function index()
{
$searchModel = new SearchModel();
$data['allnews'] = $searchModel->search('test');
return view('welcome_message', $data);
}
}
And this is error:
Call to undefined method CodeIgniter\Database\MySQLi\Connection::like()
You are basically using the old query builder style from Codeigniter 3 instead of Codeigniter 4.
In Codeigniter 4 your code should look like this.
<?php namespace App\Models;
use CodeIgniter\Model;
class SearchModel extends Model
{
protected $table = 'sport_tbl';
protected $primaryKey = 'id';
protected $returnType = 'array';
public function search($word)
{
$db = \Config\Database::connect();
$builder = $db->table('sport_tbl');
$builder->like('title', $word);
return $builder->get()->getResultArray();
}
}
In this case your controller should be just the same.
However there's another way of doing the same without creating a new database object and using the same one that is being automatically created for you.
<?php namespace App\Models;
use CodeIgniter\Model;
class SearchModel extends Model
{
protected $table = 'sport_tbl';
protected $primaryKey = 'id';
protected $returnType = 'array';
public function search($word)
{
$this->like('title', $word);
return $builder->get()->getResultArray();
}
}
In this case your controller should be a bit different:
<?php namespace App\Controllers\api;
use App\Controllers\BaseController;
use App\Models\SearchModel;
class Search extends BaseController
{
public function index()
{
$searchModel = new SearchModel();
$data['allnews'] = $searchModel->search('test')->getAll();
return view('welcome_message', $data);
}
}
The second version of the code is actually better because that way your can have as many functions you want in your model and then just call them in a chain statement always returning $this.
I have 3 tables and the corresponding models which are connected as shown below:
Telephones.php:
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class Telephones extends Model {
protected $table = 'telephones';
protected $primaryKey = 'telephone_id';
public function UserTelephone() {
return $this->hasOne('App\Models\Users','user_id','user_id');
}
}
Addresses.php:
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class Addresses extends Model {
protected $table = 'addresses';
protected $primaryKey = 'address_id';
public function UserAddress() {
return $this ->hasOne('App\Models\Users','address_id','address_id');
}
}
Users.php:
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class Users extends Model {
protected $table = 'users';
protected $primaryKey = 'user_id';
}
In my view I want to connect user's telephone with user's address so I do the following:
<?php $user_telephone = $telephone->UserTelephone()->first();
$address = $user_telephone->UserAddress()->first();
?>
but it fails with error:
BadMethodCallException in Builder.php line xxxx:
Call to undefined method
Illuminate\Database\Query\Builder::UserAddress()
Any ideas how I could solve it?
On your telephones and address model, I would say to use Belongs to instead of Has One.
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use App\Models\Users;
class Addresses extends Model {
protected $table = 'addresses';
protected $primaryKey = 'address_id';
public function UserAddress() {
return $this->belongsTo(Users::class);
}
}
class Telephones extends Model {
protected $table = 'telephones';
protected $primaryKey = 'telephone_id';
public function UserTelephone() {
return $this->belongsTo(Users::class);
}
}
From https://laravel.com/docs/5.4/eloquent-relationships#one-to-one
Defining The Inverse Of The Relationship
The reason your case doesn't work on user address is because you may not have the address_id field on your user model.
return $this->hasOne('App\Models\Users','id','user_id');
You are creating a relationship so therefore you need to let it know how they're connected. I would suggest read more about relationships: https://laravel.com/docs/5.4/eloquent-relationships
Try to call your model functions like this :
$user_telephone = $telephone->UserTelephone;
$address = $user_telephone->UserAddress;
this will be okay only if $telephone its a Telephone model
EDIT
Your error is in the place you declare you model relationship
change this:
return $this->hasOne('App\Models\Users','user_id','user_id');
to this
return $this->hasOne(Users::class,'user_id','user_id');
and the same with your other function
*make sure what is your models name.. most of the times is User
Telephone Model :
public function UserTelephone() {
return $this->belongsTo('App\Models\Users','user_id','user_id');
}
Address Model :
public function UserAddress() {
return $this->belongsTo('App\Models\Users','address_id','address_id');
}
Controller :
$telephone =Telephone::find(1); //get first telephone
$user_telephone = $telephone->UserTelephone; // get User
$address = $user_telephoneObject->UserAddress; //get Address Of the user
Finally I managed to solve it, the issue was that I was trying to call a model's function on the wrong object.
My goal was to get the address when user was known. To be more specific I had the following relationship: telephone->user->address. As you could see to the initial posted code, I declared 2 functions, UserTelephone and UserAddress. Then I called UserTelephone on a telephone object and UserAddress on a recipient object. This was the reason that my code failed. Had to create a new function on Users model to be able to get the address when user was know. Working code posted below:
Telephones.php:
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class Telephones extends Model {
protected $table = 'telephones';
protected $primaryKey = 'telephone_id';
public function UserTelephone() {
return $this->belongsTo('App\Models\Users','user_id','user_id');
}
}
Addresses.php:
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class Addresses extends Model {
protected $table = 'addresses';
protected $primaryKey = 'address_id';
public function UserAddress() {
return $this ->belongsTo('App\Models\Users','address_id','address_id');
}
}
Users.php:
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class Users extends Model {
protected $table = 'users';
protected $primaryKey = 'user_id';
public function Address() {
return $this->hasOne('App\Models\Addresses','address_id','address_id');
}
}
In my view:
<?php $address = $telephone->UserTelephone->Address()->first();?>
I am not sure what is going on here. I am getting returns of null despite having the info in the database.
Stimuli Model
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Stimuli extends Model
{
protected $table = 'stimuli';
public $timestamps = false;
/**
* Get the details associated with the stimulus
*/
public function info()
{
return $this->hasMany('App\StimuliInfo', 'attribute', 'value');
}
}
StimuliInfo Model
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class StimuliInfo extends Model
{
/**
* Get the stimuli associated with the detailss
*/
protected $table = 'stimuli_info';
public $timestamps = false;
public function info()
{
return $this->belongsTo(Stimuli::class);
}
}
Controller
<?php
namespace App\Http\Controllers;
use App\User;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\DB;
use App;
use Illuminate\Http\Requests;
class Labels extends Controller
{
public function index()
{
}
public function objects()
{
$stimulus = App\Stimuli::with('info')->get();
foreach ($stimulus as $stim)
{
$stimuli[] = $stim->stimulus_id;
}
return $stimuli;
shuffle($stimuli);
return view('label/objects')->with(compact('stimuli'));
}
}
Okay, so here is a test controller. For some reason, I am getting a null output for every data point. Which is odd as I can see in the DB that there is data there which is not null.
I am wondering whether I am doing something fishy in the model that is not right outputting this data.
Any help would be greatly appreciated - I am at a loss for this process
I am just starts a project in laravel and this is new environment for me. I am familiar with PHP and also models relationship. But the laravel structure is little bit complex for starting.
So the problem is I have a question and Course table, A single question may have more then one course so i created one more table which hold course id and question id.
Question Model
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class Question extends Model{
protected $primaryKey = "id";
public function courses(){
return $this->hasMany('App\Models\CourseQuestion');
}
}
Course model
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class Course extends Model {
protected $primaryKey = "id";
public function questions(){
return $this->hasOne('App\Models\CourseQuestion');
}
}
And last courseQuestion model
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class CourseQuestion extends Model {
protected $primaryKey = "id";
public function questions(){
return $this->hasOne('App\Models\Question');
}
public function courses(){
return $this->hasMany('App\Models\Course');
}
}
And my controller where i getting result -
class IndexController extends BaseController {
public function index(){
$user = Question::with('user','courses','branches')->find(100);
echo "<pre>";
print_r($user); die;
}
}
So the courses return only id of course and questions which store in third table but i want the course name respective id. Please help
Thanks.
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class Question extends Model
{
protected $primaryKey = "id";
public function courses()
{
return $this->hasMany('App\Models\CourseQuestion', 'course_question_id', 'id')
->select('id', 'name');
}
}
I have two tables
products and users
Both of this objects has images associated with it in a table
images
The schema for the images table is
id | image_id | resource_id | flag
1 | 567575 | 1 | user
2 | 423423 | 1 | product
Based on this flag i am identifying whether its a users image or whether its a products image.
If I need to eager load a users image how do it do it?
User model
<?php
namespace App\Entities;
use Illuminate\Database\Eloquent\Model;
class User extends Model implements Transformable
{
use TransformableTrait;
protected $table = 'users';
protected $primaryKey = 'users_id';
public function images()
{
return $this->hasMany('App\Entities\Image','resource_id');
}
}
Product model
<?php
namespace App\Entities;
use Illuminate\Database\Eloquent\Model;
class Product extends Model implements Transformable
{
use TransformableTrait;
protected $table = 'products';
protected $primaryKey = 'products_id';
public function images()
{
return $this->hasMany('App\Entities\Image','resource_id');
}
}
images model
<?php
namespace App\Entities;
use Illuminate\Database\Eloquent\Model;
class Image extends Model implements Transformable
{
use TransformableTrait;
protected $table = 'images';
protected $primaryKey = 'images_id';
public function products()
{
return $this->hasOne('App\Entities\Product','products_id');
}
public function users()
{
return $this->hasOne('App\Entities\User','users_id');
}
}
Is there a way I can pass a flag in the relationship function of images() so that it will fetch the record based on the flags?
Please help out.
You can try this adding a conditional inside your images() method:
<?php
namespace App\Entities;
use Illuminate\Database\Eloquent\Model;
class User extends Model implements Transformable
{
use TransformableTrait;
protected $table = 'users';
protected $primaryKey = 'users_id';
public function images($filtered=false)
{
if ($filtered) {
return $this->hasMany('App\Entities\Image','resource_id')->where('flag','user');
}
return $this->hasMany('App\Entities\Image','resource_id');
}
}
and try the same logic to your Product model
Change your Image model to:
<?php
namespace App\Entities;
use Illuminate\Database\Eloquent\Model;
class Image extends Model implements Transformable
{
use TransformableTrait;
protected $table = 'images';
protected $primaryKey = 'images_id';
public function product()
{
return $this->belongsTo('App\Entities\Product', 'products_id', 'resource_id');
}
public function user()
{
return $this->belongsTo('App\Entities\User', 'users_id', 'resource_id');
}
}
try with this way :
$user_id = 1;
$my_image= User::find($user_id)->images()->where('flag', '=', 'user')->first();