Call to undefined method Vendormodel::read() - codeigniter

I have created a controller named as export for making export to excel function but this error is coming.
Not getting anything. New to CI.
$this->load->model('vendormodel');
$vendor = new vendormodel();
$vendor->table ='vendor';
$data['vendor'] = $vendor->read();

Make sure that in the models/Vendormodel.php there is a public method read();. For example,
class Vendormodel extends CI_Model {
public $table = NULL;
public function read()
{
...
}
}
And it is better to write like this:
$vendor = new Vendormodel();
And even better:
$this->load->model('vendormodel');
$this->vendormodel->table = 'vendor';
$data['vendor'] = $this->vendormodel->read();

Related

How to change image directory in spatie media library?

I'm new to this package since I usually use the image intervention. So the problem is whenever I save an image, it is saving in storage directory instead of in public directory. I tried to review the documentation and did some research and didn't see any answer. Please see my code below for saving.
$user->addMediaFromRequest('avatar')->toMediaCollection('avatars');
Package:
https://spatie.be/docs/laravel-medialibrary/v7/introduction
you can simply define the filesystem config with $user->addMediaFromRequest('avatar')->toMediaCollection('avatars', 'disk');
here is how im using the package(simple usage), if you need it later
on my model:
use Spatie\MediaLibrary\HasMedia\HasMedia;
use Spatie\MediaLibrary\HasMedia\HasMediaTrait;
use Spatie\MediaLibrary\Models\Media;
class User extends Model implements HasMedia
{
use HasMediaTrait;
public function registerMediaCollections()
{
$this
->addMediaCollection('avatar')
->singleFile()
->useDisk('public');
->useFallbackUrl(asset('/images/default-user.png'))
->useFallbackPath(public_path('/images/default-user.png'));
}
public function registerMediaConversions(Media $media = null)
{
$this->addMediaConversion('avatar-thumb')->width(120)->height(120);
}
...
to get the file-url im using model accessor, added this function in my model:
public function getAvatarAttribute()
{
$file = $this->getMedia('avatar')->first();
if($file) {
$file->url = $file->getUrl();
$file->thumbnail = $file->getUrl('avatar-thumb'));
$file->alternate = $file->getCustomProperty('alternate') ?: $file->file_name;
}else{
$file = new \StdClass;
$file->url = $this->getFallbackMediaUrl('avatar');
$file->thumbnail = public_path('/images/default-user-thumb.png');
$file->alternate = 'avatar.png';
}
return $file;
}
also using custom path, in config\medialibrary.php set this array 'path_generator' => Path\To\Your\Class::class,
then the class :
use Spatie\MediaLibrary\Models\Media;
use Spatie\MediaLibrary\PathGenerator\PathGenerator as PathGenerators;
class PathGenerator implements PathGenerators
{
public function getPath(Media $media): string
{
return $this->getBasePath($media).'/';
}
public function getPathForConversions(Media $media): string
{
return $this->getBasePath($media).'/conversions/';
}
public function getPathForResponsiveImages(Media $media): string
{
return $this->getBasePath($media).'/responsive-images/';
}
protected function getBasePath(Media $media): string
{
//here im using trait to generate default path, e.g: path/mimes/avatar/media->id
//its up to you to define folder structure, just make sure each folder
//for conversions has unique name, or else it will be deleted
$base_folder = $this->get_base_folder($media->mime_type);
return "{$base_folder}/{$media->collection_name}/{$media->getKey()}";
}
}

Laravel one-to-many save Carbon error

I have a class called SubjectData:
class SubjectData extends Model
{
protected $table = 'subject_datas';
protected $fillable = ['firstname','lastname','birthdate','birthcity','months'];
protected $dates = ['birthdate'];
public function setBirthdateAttribute($date)
{
// So we can add the time, not just he php date
$this->attributes['birthdate'] = Carbon::createFromFormat('d/m/Y', $date);
}
public function anamnesis() {
return $this->belongsTo('App\Anamnesis');
}
}
And I have a class called Anamnesis:
class Anamnesis extends Model
{
public function meetingTest() {
return $this->belongsTo('App\MeetingTest');
}
public function subject() {
return $this->belongsTo('App\Subject','subject_id','id');
}
public function subjectData() {
return $this->hasOne('App\SubjectData');
}
public function scholarHistory() {
return $this->hasOne('App\ScholarHistory');
}
public function familyHistory() {
return $this->hasOne('App\FamilyHistory');
}
public function psicodiagnosis() {
return $this->hasOne('App\Psicodiagnosis');
}
}
The store function of the SubjectController class works like this:
public function store(CreateSubjectRequest $request)
{
$input = $request->all();
// Let's generate the anamnesis of the subject
$anamnesis = Anamnesis::create();
$anamnesis->save();
$newSubjectData = $this->saveSubjectData($input);
$anamnesis->subjectData()->save($newSubjectData);
......
......
}
where the function called is:
public function saveSubjectData($input)
{
$subjectData['firstname'] = $input['firstname'];
$subjectData['lastname'] = $input['lastname'];
$subjectData['birthcity'] = $input['birthcity'];
$subjectData['birthdate'] = $input['birthdate'];
return SubjectData::create($subjectData);
}
The problem is with the "birthdate" property.
If i check the value of $newSubjectData (dd($newSubjectdata)) after the call $this->saveSubjectData($input) the value of the birthdate is exactly the one i set on the calendar in the frontside (and also in the db the value is correct)
If I put the dd($anamnesis->subjectData) after the call $anamnesis->subjectData()->save($newSubjectData) the result is the "today" date and also in the DB the value is not the one I set but the date of today.
I can't find the error
P.S. The calendar is inside a Vue template
I think the problem is that, the date must be an instance of Carbon or is properly formatted according to your database table. Try the following inside your saveSubjectData() method
$subjectData['birthdate'] = Carbon\Carbon::parse($input['birthdate']
I write down the answer but i thank John Aldrin that guided me in the right direction.
You have to put the timestamp('birthdate') AFTER the default timestamps of the migration table (so at the end of the migration table)
I don't know why. If someone knows please explain !

Creating a public function inside a controller in Laravel

I'm trying to create a public/private function inside my controller (say PostController) to tidy up certain code.
I wrote something like this:
class PostController extends BaseController
{
public function store()
{
$startdate = dateformatchange(Input::get('startdate'));
}
public function dateformatchange($date)
{
$dateString = DateTime::createFromFormat('m-d-Y', $date);
$dateNew = $dateString->format('Y/m/d');
return $dateNew;
}
}
But I'm getting some error. Call to undefined function dateformatchange()
Am I doing it wrong? please advice where I went wrong. Sorry if it is a silly mistake.
You need to do it like this:
$startdate = $this->dateformatchange(Input::get('startdate'));

Laravel4 - Saving a model with multiple relationships/foreign keys

I've tried to understand a process of saving a model with multiple relationships but I still can't figure out how to do it "kosher" way.
To begin with - I have an Event model that belongs to a category (Eventcat) and a Location:
// Event.php
class Event extends Eloquent {
protected $table = 'events';
public function location()
{
return $this->belongsTo('Location');
}
public function eventcat()
{
return $this->belongsTo('Eventcat');
}
public function users()
{
return $this->belongsToMany('User');
}
}
// Location.php
class Location extends Eloquent
{
protected $table = 'locations';
public function events()
{
return $this->hasMany('Event');
}
}
// Eventcat.php
class Eventcat extends Eloquent
{
protected $table = 'eventcats';
public function events()
{
return $this->hasMany('Event');
}
}
I've seeded the database with a few categories and locations and now I trying to get events saving work. I thought that the $event->eventcat()->associate( $eventcat ) would work but I got a Call to undefined method eventcat() error.
public function postCreateEvent() {
$event = new Event();
$eventcat = Eventcat::find( Input::get('event-create-eventcat[]') );
$location = Location::find( Input::get('event-create-location[]') );
$event->title = Input::get('event-create-title');
$event->description = Input::get('event-create-description');
$event->price = Input::get('event-create-price');
$event->start_date = Input::get('event-create-start_date');
$event->end_date = Input::get('event-create-end_date');
$event->eventcat()->associate( $eventcat );
$event->location()->associate( $location );
$event->save();
}
I've read the documentation, API and a few threads here but I still can't figure out the best way to deal with this.
Thanks for replies!
I would actually bet that you have a conflict in your class name. Laravel contains an Event class and I wonder if that isn't what's being called in your code. As a quick test, you could rename your class FooEvent and see if it works.
The best solution is probably namespacing your model (see http://chrishayes.ca/blog/code/laravel-4-methods-staying-organized for a quick intro) so that your model can still be called Event without conflicting with the builtin class.

Magento: use various methods in 1 model

I'm having a problem using methods in my module's model class.
I have a public function which triggers 2 protected methods. The problem is that only the first 1 returns a value.
Here is my class:
<?php
class Osdave_Points_Model_Mysql4_Points_Collection extends Mage_Core_Model_Mysql4_Collection_Abstract
{
const POINTS_CONFIRMED = 2;
const POINTS_REDEEMED = 4;
protected $_customer;
public function _construct()
{
parent::_construct();
$this->_init('points/points');
$this->_customer = Mage::getSingleton('customer/session')->getCustomer();
}
public function getCustomerAvailablePoints()
{
$confirmed = $this->_getCustomerConfirmedPoints();
$redeemed = $this->_getCustomerRedeeemedPoints();
$balance = ($confirmed - $redeemed);
return $balance;
}
protected function _getCustomerConfirmedPoints()
{
$availablePoints = $this->addFieldToFilter('customer_id', $this->_customer->getId())
->addFieldToFilter('points_status', self::POINTS_CONFIRMED)
->addFieldToSelect('points_pending')
->addExpressionFieldToSelect('available_points', 'SUM({{points_pending}})', 'points_pending');
return $availablePoints->getFirstItem()->getAvailablePoints();
}
protected function _getCustomerRedeeemedPoints()
{
$redeemedPoints = $this->addFieldToFilter('customer_id', $this->_customer->getId())
->addFieldToFilter('points_status', self::POINTS_REDEEMED)
->addFieldToSelect('points_pending')
->addExpressionFieldToSelect('redeemed_points', 'SUM({{points_pending}})', 'points_pending');
return $redeemedPoints->getFirstItem()->getRedeemedPoints();
}
}
Now, if, in _getCustomerRedeeemedPoints(), I replace $this by Mage::getResourceModel('points/points_collection') it works fine. But as I already am insdide the class, I don't understand why I have to instance it through Mage: as far as I understand, $this is only available once.
So, am I doing something wrong?
thanks in advance.
I'm guessing this has to do with adding filters to the $this object for different purposes. Try adding this to the top of your methods:
$this->getSelect()->reset();
If that doesn't work, try echoing your queries before your getFirstItem calls and see if they behave as expected:
Mage::log($this->getSelect()."");
Hope that helps!
Thanks,
Joe

Resources