I want to be able to display attributes from a table anywhere in my application and have them be edited by the user. This means the data will come from a row in a database. Each column has a default value which can be changed by the user if they so choose.
The table columns may look like this:
| id | admin_id | practitioner | practitioners | student | students | and so on ...
Here is my model:
class Lang_trans_model extends CI_Model {
public function get_lang_trans($admin_id){
$this->db->where('admin_id', $admin_id);
$query = $this->db->get('lang_trans');
return $query->row();
}
Here is my MY_Controller:
class MY_Controller extends CI_Controller {
public function __construct() {
parent::__construct();
$this->load->model('key_items_model');
$this->load->model('lang_trans_model');
foreach ($this->key_items_model->get_key_items() as $result){
$this->label[$result->key] = $result->value;
}
//$admin_id = $this->session->userdata('admin_id');
$admin_id = 3;
$lang = $this->lang_trans_model->get_lang_trans($admin_id);
//not sure what should go here????
//I want to be able to use $lang->practitioner; to be able to have practitioner, or the edited attribute to show on the page
var_dump($lang);
// produces the following:
//object(stdClass)[26]
//public 'id' => string '3' (length=1)
//public 'admin_id' => string '3' (length=1)
//public 'practitioner' => string 'blah' (length=4)
//public 'practitioners' => string 'blahplural' (length=10)
How do I finish the MY_Controller to be able to use these data fields anywhere in my application?
I do not mind using object oriented syntax or array syntax. Whichever is easier. I just can not figure out how to display the fields without using a foreach statement in every view. I want to call the data with the $lang->practitioner; format in the view.
Thanks in advance.
Declare parent class properties. Than call it from child class:
class MY_Controller extends CI_Controller
{
public $lang;//parent variable declared
public function __construct()
{
parent::__construct();
$this->load->model('key_items_model');
$this->load->model('lang_trans_model');
foreach ($this->key_items_model->get_key_items() as $result) {
$this->label[$result->key] = $result->value;
}
//$admin_id = $this->session->userdata('admin_id');
$admin_id = 3;
//dedicating value to variable
$this->lang = $this->lang_trans_model->get_lang_trans($admin_id);
}
}
Child controller:
class Practitioners extends MY_Controller
{
public function __construct()
{
parent::__construct();
// no need to call model if it is called in parent controller
}
public function index()
{
var_dump($this->lang);
}
}
Related
I have table relation like this:
Is it possible to get the data of the project from the cash mutation table?
In example in cash mutation table with id '4' I wanna call agency name from projects table.
I've done it with with (eager loading in Laravel) but it returns null like:
$cash_mutations = CashMutation::where('id', 4)->with('project')->get();
This is my CashMutation Model:
class CashMutation extends Model
{
use HasFactory;
protected $guarded = ['id'];
public function category(){
return $this->belongsTo(Category::class);
}
public function balanceType(){
return $this->belongsTo(BalanceType::class);
}
public function project(){
return $this->belongsTo(Project::class, 'cash_mutation_id', 'id');
}
public function deposit(){
return $this->hasMany(Deposit::class);
}
}
and this is my Project Model:
class Project extends Model
{
use HasFactory;
protected $guarded = ['id'];
public function cashMutation(){
return $this->belongsTo(CashMutation::class);
}
public function projectFunding(){
return $this->hasMany(ProjectFunding::class);
}
public function SharingProfit(){
return $this->hasMany(SharingProfit::class);
}
}
Is it possible to do that in Laravel? If it possible anyone want to tell me about it? Thanks
Yes. It is definitely possible.
You don’t get an error message so the basic configuration should be fine.
I suspect that the data in the database is not correct. What’s the project_id for the CashMutation with the id 4? Is it maybe null?
Iam developing a website using codeigniter.I want to pass a particular data to all the controller.I searched and find it can be done by session like below.
$var = $this->session->userdata('passingdata');
But this data loses when history is cleared and when session expires.
Is there any other way to pass variable to all controllers.
You can try this
- create MY_Controller in application/core. it looks like
Class MY_Controller extends CI_Controller {
public function __construct() {
parent::__construct();
$this->load->model('HomeModel');
$this->global_data['settings']= $this->HomeModel->getSettings();
}
function display_view($view, $local_data = array()) {
$data = array_merge($this->global_data, $local_data);
return $this->load->view($view, $data);
}}
then extend this controller in your all controller
your functions in your controller like
Class Home extends MY_Controller {
public function index() {
$data['title'] = 'test';
$this->display_view('home', $data);
}
}
I have 3 models: User, Query, Offer. The relation is that user has many queries and query has many offers.
In controller I use $queries=User->query()->get(); (query() is a hasMany method in User model) and pass it to view as parameter. In view I want to display a table of queries from single user and count all offer made to each query.
Actually what I want to get, is a nested object $queries like
user:
query1:
offer1
offer2
query2:
offer3
offer4
How it should be coupled in models and how to use it in controller and view?
You could have something like:
Class User extends Model{
protected $table = 'users';
public function query(){
return $this->hasMany('App\Query');
}
}
Class Query extends Model{
protected $table = 'queries';
public function offer(){
return $this->hasMany('App\Offer');
}
public function user(){
return $this->belongsto('App\User');
}
}
Class Offer extends Model{
protected $table = 'offers';
public function query(){
return $this->belongsTo('App\Query');
}
}
Then in your controller you can have something like this:
public function showUser($id){
$user = User::with('query.offer')->find($id);
return View::make('user')->with('user', $user);
}
Then in your view you could have something like:
#foreach($user->query as $query)
{{$query->title}}
#foreach($query->offer as $offer)
{{$offer->title}}
{{$offer->amount}}
#endforeach
#endforeach
I imagine I might need to add a special annotation somewhere, but I'm crossing my fingers that PhpStorm is smart enough to resolve return types given awkward inheritance patterns.
For example, I have some code that resembles this:
<?php
class Collection extends \ArrayObject
{
public function __construct(array $items)
{
foreach ($items as $key => $value) {
if (isset(static::$requiredType) && !$item instanceof static::$requiredType)
$this->offsetSet($key, $value);
}
}
public function getFirst()
{
return $this->offsetGet(0);
}
}
class MessageCollection extends Collection
{
protected static $requiredType = 'Message';
}
class UserCollection extends Collection
{
protected static $requiredType = 'User';
}
I'd like it if when I call UserCollection::getFirst() it inferred that a User was returned, while when I call MessageCollection::getFirst() it inferred that a Message was returned. Is there some annotation I could put in somewhere to achieve this result?
My first thought was something like this:
/**
* #return Message|User|XXXX|YYYY|ZZZZ|AAAA|BBBB|CCCC|DDDD
*/
public function getFirst()
{
return $this->offsetGet(0);
}
but I imagine that would get a little ridiculous to the point of being useless as I add more collection classes.
Try this:
/**
* #method \User getFirst()
*/
class UserCollection extends Collection
{
protected static $requiredType = 'User';
}
I'm working with the following models: Game, User and Player where Player extends User. In this case Game and Player have a relationship of N:N and User extends Confide. So code stands like:
app/models/Game.php
<?php
class Game extends Eloquent {
protected $guarded = array();
protected $table = 'users';
public $timestamps = false;
public function players(){
return $this->belongsToMany('Player');
}
}
app/models/User.php
<?php
use Zizaco\Confide\ConfideUser;
class User extends ConfideUser {
protected $table = 'users';
public $timestamps = false;
protected $hidden = array('password');
//Here go original Confide functions
}
app/models/Player.php
<?php
class Player extends User{
public function games(){
return $this->belongsToMany('Game');
}
}
I've populated the pivot table called game_player. As player extends user, pivot table is like this:
game_player
id
game_id
user_id
But if I run this super simple example I have an error:
$game = Game::find($gameId);
$player = $game->players(); //Error in this line
Error
Call to undefined method Illuminate\Database\Query\Builder::players()
I'm confused because I'm not having the error about pivot table was not found so I could think relationship is created fine.
On the other hand if I var_dump games var I obtain:
var_dump($game)
object(Game)[181]
protected 'table' => string 'games' (length=5)
//more var info about DB
protected 'relations' =>
array (size=0)
empty
protected 'hidden' =>
array (size=0)
empty
protected 'visible' =>
array (size=0)
empty
protected 'guarded' =>
If relationship would be created fine... shouldn't relations var have any data?
Thanks!
Just pointing out somethings that could solve the problem:
Your Game.php has the wrong table name.
When you get players, the line should be:
$game = Game::with('players')->find($gameId);
$player = $game->players;