Laravel after composer update model method call undefined - php

I am working on a project with Laravel 4.2 and I created some models and controllers and called model function from controller, the problem is after composer update command it displays this error: Call to undefined method Department::getAllParent() but before composer update it works fine. You think what is the problem with this issue? thanks in advance
Model code:
class Department extends Eloquent{
/**
* The database table used by the model.
*
* #var string
*/
protected $table = 'department';
public static function getAll()
{
$table = DB::table('department');
$object = $table->get();
return $object;
}
public static function getAllParent()
{
$table = DB::table('department');
$table->where('parent',0);
$object = $table->get();
return $object;
}
}
And Controller code:
class DepartmentController extends BaseController
{
/*
Getting all records from department
#param: none
#Accessiblity: public
#return: Object
*/
public function getAllDepartment()
{
//get data from model
$deps = Department::getAllParent();
$depAll = Department::getAll();
//load view for users list
return View::make("department.dep_list")->with('deps',$deps)->with('all',$depAll);
}
}

Don't think this is related to your issues but this might be a better way to handle these queries. you are using Eloquent and setting the table parameter. why not use Eloquent's build in power?
class Department extends Eloquent{
/**
* The database table used by the model.
*
* #var string
*/
protected $table = 'department';
public static function getAll()
{
return Department::get();
}
public static function getAllParent()
{
return Department::where('parent', 0)->get();
}
}
I think you might also be able to use $this->get(); but I can't test right now.

Related

How to join two custom table in Magento 2.3

I have two tables as below
pscustom_catalog_product_entity
pscustom_catalog_product_entity_media
I have created collection for table pscustom_catalog_product_entity using resource model and block.
Now I can access this table data using collection.
app/code/MyModule/Productsinfo/Model/Product.php
<?php
namespace MyModule\Productsinfo\Model;
use Magento\Framework\Model\AbstractModel;
use MyModule\Productsinfo\Model\ResourceModel\Product as ResourceModel;
class Product extends AbstractModel
{
protected function _construct()
{
$this->_init(ResourceModel::class);
}
}
app/code/MyModule/Productsinfo/Model/ResourceModel/Product.php
<?php
namespace MyModule\Productsinfo\Model\ResourceModel;
use Magento\Framework\Model\ResourceModel\Db\AbstractDb;
class Product extends AbstractDb
{
protected function _construct()
{
$this->_init('pscustom_catalog_product_entity', 'entity_id');
}
}
app/code/MyModule/Productsinfo/Model/ResourceModel/Product/Collection.php
<?php
namespace MyModule\Productsinfo\Model\ResourceModel\Product;
use Magento\Framework\Model\ResourceModel\Db\Collection\AbstractCollection;
use MyModule\Productsinfo\Model\Product as Model;
use MyModule\Productsinfo\Model\ResourceModel\Product as ResourceModel;
class Collection extends AbstractCollection
{
protected function _construct()
{
$this->_init(Model::class, ResourceModel::class);
}
}
app/code/MyModule/Productsinfo/Block/Product.php
<?php
namespace MyModule\Productsinfo\Block;
use Magento\Framework\View\Element\Template;
use MyModule\Productsinfo\Model\ResourceModel\Product\Collection;
class Product extends Template
{
/**
* #var Collection
*/
private $collection;
/**
* Hello constructor.
* #param Template\Context $context
* #param Collection $collection
* #param array $data
*/
public function __construct(
Template\Context $context,
Collection $collection,
array $data = []
)
{
parent::__construct($context, $data);
$this->collection = $collection;
}
public function getAllProducts() {
return $this->collection;
}
I can get product collection using getAllProducts() method.
Now I want to join table pscustom_catalog_product_entity collection to table pscustom_catalog_product_entity_media. I have tried many solution but its not working.
Any help would be appreciated.
Thanks.
I think you can join tables in following way:
$this->getSelect()->join(
['secondTable'=>$this->getTable('pscustom_catalog_product_entity')],
'main_table.id = secondTable.entity_id','*');
$this->getSelect()->join(
['thirdTable'=>$this->getTable('pscustom_catalog_product_entity_media')],
'main_table.id = thirdTable.entity_id', '*');
}
This is the simplest way of joining two tables in collection

How to use one model for multiple tables in laravel 5.6

I am looking for solutions, but can't really understand. I'm new in Laravel and I want a simple instruction on how to use one model for multiple tables like CodeIgniter as follows:
Controller myController:
public function shipBuilding()
{
$data = $this->input->post();
$response = $this->MyModel->shipbuildingSave($data);
}
public function contact()
{
$data = $this->input->post();
$response = $this->MyModel->contactSave($data);
}
Model MyModel:
public function shipbuildingSave($data){
$this->db->insert('tbl_shipbuilding', $data);
return $this->db->insert_id();
}
public function contactSave($data){
$this->db->insert('tbl_contact', $data);
return $this->db->insert_id();
}
This is not how models work in Laravel. each model should be a representation of one single table.
You could, however, change the table name on booting the model up:
class Flight extends Model
{
/**
* The table associated with the model.
*
* #var string
*/
protected $table = 'example';
/**
* The "booting" method of the model.
*
* #return void
*/
protected static function boot()
{
parent::boot();
static::addGlobalScope(new AgeScope);
// Set the $this->table depending on some logic.
}
}
But again, this is probably not recommended for your case.

PHPDoc different return type for extended classes

I have created my own DB - Model structure which is similar to Laravel. I have been facing with 2 problems.
I have a Model class which all of my models extend it. For example, my User class extends Model. I want to return that get() method return type of class which is extended.
Is this possible?
Class Model extends DB {
/**
* #return AnyClassThatExtended
*/
function get()
{
}
}
Class User extends Model {
function test() {
$user->get(); // I want it to return User type of object
}
}
You should use
private static $instance;
/**
* return static
*/
public function get() {
if (is_null(self::$instance)) {
self::$instance = new static();
}
return self::$instance;
}
because you are returning current class that you are at (if I understand correctly)
It's possible that PHPStorm does not recognize it

Finding related model in laravel based on parent

I want to search for like which is made by a given user on a given post.
I have one Post model as: Post
class Post extends \Eloquent
{
protected $fillable = [];
public function likes()
{
return $this->morphMany('Like', 'likeable');
}
}
and one Like model as:
class Like extends \Eloquent
{
protected $fillable = [];
/**
* #return \Illuminate\Database\Eloquent\Relations\MorphTo
*/
public function likeable()
{
return $this->morphTo();
}
/**
* #return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
public function owner()
{
return $this->belongsTo('User', 'owner_id');
}
}
and default User model provided in laravel.
now I have queried Post model to find one post using post_id like
$post=Post::find(10);
and User model like
$user=User::find(1);
now I want to find Like made by this User(1) on Post(10) Is there any function available in laravel for this.
I know that I can directly query Like using raw where function as
\Like::whereLikeableId('10')->whereLikeableType('Post')->whereUserId('1')->get();
but this looks ugly and I want to know laravel way of doing it.
All you need is:
$like = $post->likes()->where('user_id', 1)->first();

how to Inject a Sentry 2 User Model into a controller - laravel 4

How do you inject a Sentry 2 User model into a laravel 4 controller using IoC?
for example i would like the following
class myController extends BaseController {
/**
* creates a list of MyModel Models
*
* #return View
*/
public function getIndex( User $user )
{
// fetch models
$models = MyModel::all();
// Show the page
return View::make('my-views.the-view', compact('models', 'user'));
}
}
This is how I like to do it:
class myController extends BaseController {
$protected $user
/**
* creates a list of MyModel Models
*
* #return View
*/
function __construct(User $user){
parent::__construct();
$this->user = $user;
}
public function getIndex()
{
// fetch models
$models = MyModel::all();
// Show the page
return View::make('my-views.the-view', compact('models', 'user'));
}
}
You can also do it in the method, but... well, give this a good read, too: http://fabien.potencier.org/article/11/what-is-dependency-injection

Categories