I have variables that are created and used on my model that I need to be able to use on my controller how is that accomplished?
Edit:
Controller: http://pastebin.com/jhAwAVa6
Model: http://pastebin.com/9xXRyYAa
It's unclear from your question, what exactly you want to do.
If it is about accessing model properties, the right way is using accessor methods:
class Model extends CI_Model{
private $name;
public function getName() {return $this->name; /*any other logic here*/}
public function setName($value) {$this->name= $value; /*any other logic here*/}
}
You can not pass the variable from a model to controller.
You can access public variables of a model through a controller.
echo $this->model_name->variable_name;
Model (my_model)
function useful_info()
{
$data = new stdClass();
$q = $this->db->get('users');
$data->users = $this->db->result();
$data->date = date('Y-m-d');
$data->info = array('whatever','more','anything');
return $data;
}
Controller
function index()
{
$info = $this->my_model->useful_info();
foreach($info->users as $user)
{
echo $user->id;
}
echo $info->date;
if($info->info[0] == 'whatever')
{
// do something
}
}
You don't have to create an object (it can be a string, T/F, array, etc), but you usually need to return something from your model and library functions. And you can access what you return by returning it to a variable $info = $this->my_model->useful_info();
Related
I'm using Laravel for a project and i want to filter a collection based on custom method written in the model:
Controller:
$models= Produs::with('categorie')
->with('poza')
->with('element_extra.extras')
->get()
->where('id_stare', 1)
->where('categorie.id_restaurant', $idRestaurant)
->groupBy('categorie.denumire');
$produse = $models->filter(function ($produs, $key) {
return $produs->isAvailable();
})->values();
Model:
class Produs extends Model
{
protected $table = "elemente";
public $timestamps = false;
public function isAvailable(){
if(...something....){
return false;
}else{
return true;
}
}
This is what i get:
BadMethodCallException
Method Illuminate\Database\Eloquent\Collection::isAvailable does not exist.
You need to add keyword scope to your method
public function scopeIsAvailable($query) {
return $query->where('active', true);
}
and after that call it like this
$models= Produs::isAvailable()...
:: double colon usage is for static functions.
try to use
public static function isAvailable(){}
I made a category tree and I need to pass one parameter to relation, I can't pass them.
public function Child()
{
return $this->hasMany(Category::class, 'parent_id', 'id');
}
but I want to use variable to pass in relation look like this.
public function Child()
{
return $this->hasMany(Category::class, 'parent_id', 'id')->where(['owner_id' => $this->ownerId]);
}
then I try to use variable and receive nothing, but if I use hardcoded value then works well. Please help
$models = App\{YourMainModel}::with(['Child' => function ($query) use ($this) {
$query->where(['owner_id' => $this->ownerId]);
}])->get();
You will need to add a constructor to your Child model (which extends the class Model).
private ownerId;
public function __construct(int ownerId)
{
parent::__construct($attributes);
$this->ownerId = $ownerId;
}
Then you can access this throughout your class.
public function child()
{
return $this->hasMany(Category::class, 'parent_id', 'id')->where('owner_id', $this->ownerId);
}
You would do this if every time you wanted to instantiate your class Child, you would have to give it an owner:
$ownerId = 5;
$child = new Child($ownerId);
Alternatively, you could pass a parameter directly to that function from wherever you call it, like:
public function childWithOwner(int $ownerId)
{
return $this->hasMany(Category::class, 'parent_id', 'id')->where('owner_id', $ownerId);
}
And you would call it: $this->childWithOwner(4);
As a tip I would encourage you to start your function names with a lowercase letter.
I want to pass data from Controller to model.But I am unable to fetch it at the model side in CI.Kindly help me out.
Here is my controller function:
function show_chronicles($chronicle_num) {
$this->load->database();
//load the model
$this->load->model('Chronicles_model');
//load the method of model
$data['h'] = $this->Chronicles_model->show_seminar();
//return the data in view
$this->load->view('chronicles', $chronicle_num);
}
And here is my model:
public function show_seminar($chronicle_num) {
echo $chronicle_num;
//$this->db->select('*');
//$this->db->where('chronicles_no',$chronicle_num);
//$query1 = $this->db->get('chronicles');
//return $query1;
}
Its because your not passing any value to your model.
CONTROLLER
function show_chronicles($chronicle_num)
{
$this->load->database();
$this->load->model('Chronicles_model');
$data['h']=$this->Chronicles_model->show_seminar($chronicle_num);
$this->load->view('chronicles', $chronicle_num);
}
and you need to return the result() of your query
MODEL
public function show_seminar($chronicle_num = NULL)
{
return $this->db
->get_where('chronicles', array('chronicles_no' => $chronicle_num))
->result();
}
Controller:
function show_chronicles($chronicle_num) {
$this->load->database();
//load the model
$this->load->model('Chronicles_model');
//load the method of model
// pass it to model
$data['chronicle_num'] = $this->Chronicles_model->show_seminar($chronicle_num);
//return the data in view
$this->load->view('chronicles', $data);
}
And here is my model:
public function show_seminar($chronicle_num) {
return $chronicle_num;
//$this->db->select('*');
//$this->db->where('chronicles_no',$chronicle_num);
//$query1 = $this->db->get('chronicles');
//return $query1;
}
You just forgot to pass $chronicle_num as a parameter when you called $this->Chronicles_model->show_seminar();.
So the right way is $this->Chronicles_model->show_seminar($chronicle_num);
You can add as many parameters as you wish to the functions. Example:
public function show_seminar($chronicle_num, $param2, $param3) {
//Login here
}
Just remember to pass the parameters every time you call the function.
Can I actually make database queries without creating a new instance of the model?
For example, in my controller I have (CONTROLLER)
Name_Model::get_all_from_another_table();
and in my name_model.php (MODEL)
public static function get_all_from_another_table() {
$this->db->query('select * from another_table');
return $x;
}
This case happens for tables with predefined values to be loaded before creating new instances of model.
The above code returns:
Fatal error: Using $this when not in object context
Try it fisrt.
Controller:
$this->load->model( 'name_model' );
$data = $this->name_model->get_all_from_another_table()->result();
print_r( $data ); //just for testing...
Model:
//Model name: name_model.php
public static function get_all_from_another_table() {
$this->db->query('select * from another_table');
return $x;
}
Controller:
// Model File and Class Name first Character Must be Uppercase
$this->load->model( 'Name_model' );
$data = $this->Name_model->get_all_from_another_table();
print_r($data); //You result is print here
Model:
//Model name: Name_model.php
public static function get_all_from_another_table() {
$query = $this->db->query('select * from another_table');
return $query->result();
}
I have function showItemList inside User class
class User extends Eloquent {
//...
protected $item = ['axe', 'sword', 'knife'];
public function showItemList() {
return $this->$item;
}
}
In my controller it's possible to use this.
$id = 1;
$user = User::find($id);
$user -> showItemList();
But how can I just call this function directly(irrelevant to $id query)?
I looks for something like below (sure now it's not working):
$list = User::showItemList();
You need to use the static protected variable and return it from static method.
class User extends Eloquent {
//...
static protected $item = ['axe', 'sword', 'knife'];
public static function showItemList() {
return self::$item;
}
}