I have a widget like this :
class ShowNewsWidget extends Widget{
public function init(){
parent::init();
}
public function run(){
$news = NewsModel::findAll(['not in ','id',$array]);
/*
HERE SHOW THE NEWS
*/
}
}
I call this widget many times in the same page (one in layout, one in the view, one in the controller, etc...) , and I need to not show a single news 2 times on the same page . So I think to use a "not in" condition , but how can I make the widgets comunicate each other? Using a singleton?
Thx
MS
In Widget class:
class ShowNewsWidget extends Widget{
public $exclude = []; // excluded news ids
public function run(){
$news = NewsModel::find()
->andFilterWhere(['not in ','id', $this->exclude])
->all();
}
}
In View:
<?php echo ShowNewsWidget::widget([
'exclude' => [1, 3]
]); ?>
Related
I am working on a query but can't find the solution to this one.
The structure i have at the moment is:
A project has 1 machine, but a machine can belong to multiple projects.
A machine can have multiple issues.
Now i want to get all projects with the issues created after the project got created.
The query that i have at the moment, but doesn't work:
$allProjects = Project::with(['machine',
'machine.issues' => function ($query) {
$query->where('created_at', '>=', 'project.created_at');
}
])
->orderBy('id')
->get();
I am not getting the issues at all by using this query, when i switch the operator to: < then i get all the issues even the ones after project.created_at
You can have a pivot do most of the work for you. I tried reproducing your problem statement as below.
<?php
class Project extends Model
{
use HasFactory;
public function machine()
{
return $this->hasOneThrough(Machine::class, MachineProject::class, 'machine_id', 'id', 'id', 'project_id');
}
public function issues()
{
return $this->hasManyThrough(Issue::class, Machine::class, 'id', 'machine_id')->whereDate('issues.created_at', '<=', $this->created_at);
}
}
class Machine extends Model
{
use HasFactory;
public $with = ['issues'];
public function projects()
{
return $this->belongsToMany(Project::class);
}
public function issues()
{
return $this->hasMany(Issue::class);
}
}
class Issue extends Model
{
use HasFactory;
public function machine()
{
return $this->belongsTo(Machine::class);
}
}
class MachineProject extends Pivot
{
//
}
To retrieve the Project with issues, you just do
$allProjects = Project::with(['machine' => function ($machine) {
$machine->with('issues');
}])->get();
I'm trying to directly edit my Kodi MySQL DB and build a tool to fix some scraping errors that have always been not quite right.
the relationship of tables is
tvshow -> episode -> file
So I'm able to so far (after creating models for these tables)
List all shows
view specific show and list all episodes
show file entry for that episode.
What I'm trying to do is list all files that belong to the show, via a dropdown. ( as I need to change them )
So I need to use the TVShow model to get all epiodoes that have an idShow entry.. AND then get all the files based on the episode table. I know that Eloquent can handle this typically, but I've only ever used my own table structure, never one like this.. so I think my issue is over riding key names etc...
the key column names are:
idShow
idEpisode
idFile
(Where that SAME name is used as the primary key on the parent table AND the mapping ID in the child table)
Anyhoo, here's my models..
Any ideas?
TVShow
<?php
namespace App\Models;
use Eloquent as Model;
class TvShow extends Model
{
public $table = 'tvshow';
protected $primaryKey = 'idShow';
public function episodes()
{
return $this->hasMany(Episode::class, 'idShow');
}
}
Episode
<?php
namespace App\Models;
use Eloquent as Model;
class Episode extends Model
{
public $table = 'episode';
protected $primaryKey = 'idEpisode';
public function tvshow()
{
return $this->belongsTo(TvShow::class, 'idShow', 'idShow');
}
public function season()
{
return $this->hasOne(Season::class, 'idShow', 'idShow')->where('season', '>=', 0)->where('idSeason', $this->idSeason);
}
public function file()
{
return $this->hasOne(File::class, 'idFile', 'idFile');
}
public function files()
{
return $this->hasMany(File::class, 'idFile', 'idShow');
}
}
File
<?php
namespace App\Models;
use Eloquent as Model;
class File extends Model
{
public $table = 'files';
protected $primaryKey = 'idFile';
public function episode()
{
return $this->belongsTo(Episode::class, 'idFile', 'idFile');
}
}
I'm calling this in my Controller...
TvShow::with('episodes.files')->get();
I'm getting ALL shows as a list..?
A hasManyThrough relationship will work. Add this to your TvShow model.
public function files()
{
return $this->hasManyThrough(Episode::class, File::class);
}
See https://laravel.com/docs/7.x/eloquent-relationships#has-many-through
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 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);
}
}
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;