I have the following model:
class ProjectTwitterStatus extends Eloquent {
protected $table = 'project_twitter_statuses';
protected $softDelete = true;
protected $guarded = array('id');
public static function boot()
{
parent::boot();
ProjectTwitterStatus::deleting(function($projectTwitterStatus)
{
$projectTwitterStatus->deleted_by = Auth::user()->id;
});
}
public function twitterStatus() {
return $this->belongsTo('TwitterStatus');
}
public function twitterRetweet() {
return $this->belongsTo('TwitterRetweet','twitter_status_id','twitter_status_id');
}
public function project() {
return $this->belongsTo('Project');
}
}
Somewhere in my app an item is deleted using one of the following statements:
ProjectTwitterStatus::find($id)->delete();
ProjectTwitterStatus::whereIn('twitter_status_id', $twitterStatusIds)->delete();
I can see in the database the item had been (soft) deleted. But the deleted_by column is not filled.
Does anyone know what I am doing wrong?
Try using Late Static Binding, like following-
class ProjectTwitterStatus extends Eloquent {
public static function boot ()
{
parent::boot();
static::deleting(function($projectTwitterStatus)
{
$projectTwitterStatus->deleted_by = Auth::user()->id;
});
}
}
Related
We have the following class using $with:
class CargaHorasEmpleado extends Model
{
protected $table = "empleados_horas";
protected $with = ["tipoTarea", "proyecto", "empleado", "empleadoQueHizoLaCarga"];
public function tipoTarea()
{
return $this->belongsTo('App\TipoTarea', 'id_tipo_tarea', 'id')->withTrashed();
}
public function empleado()
{
return $this->belongsTo('App\Empleado', 'id_empleado', 'id')->withTrashed();
}
public function empleadoQueHizoLaCarga()
{
return $this->belongsTo('App\Empleado', 'id_empleado_cargo_hs', 'id')->withTrashed();
}
public function proyecto()
{
return $this->belongsTo('App\Proyecto', 'id_proyecto', 'id')->withTrashed();
}
}
This is the class TipoTarea
namespace App;
use Illuminate\Database\Eloquent\Model;
class TipoTarea extends Model
{
protected $table = 'tipos_tareas';
public $timestamps = false;
protected $fillable = [
'titulo', 'descripcion'
];
}
Thep page throws the error: "Call to undefined relationship [tipoTarea] on model [App\CargaHorasEmpleado]". That's the only relationship that's not working. The others are fine. What's wrong?
Well, isn't the relationship called "tipoTarea"? You wrote "tiposTarea"
The problem was that my class "TipoTarea" didn't use softdeletes. So the error was in using the "WithTrashed" method. The correct way is:
public function tipoTarea()
{
return $this->belongsTo('App\TipoTarea', 'id_tipo_tarea', 'id');
}
I am trying to understand something.
I have a Project Model.
A Project can have many Document.
A Document has many DocumentData.
So this is straight forward, I have my models set up like so
class Project extends Model
{
protected $table = 'projects';
protected $guarded = [];
use SoftDeletes;
public function document()
{
return $this->hasMany('App\document', 'projectId');
}
public static function boot()
{
parent::boot();
static::deleted(function($document)
{
$document->delete();
});
}
}
class Document extends Model
{
use SoftDeletes;
protected $table = 'document';
protected $guarded = [];
public function project()
{
return $this->belongsTo('App\Project', 'projectId');
}
public function documentData()
{
return $this->hasMany('App\DocumentData', 'documentId');
}
public static function boot()
{
parent::boot();
static::deleted(function($document)
{
$document->documentData()->delete();
});
}
}
class DocumentData extends Model
{
use SoftDeletes;
protected $table = 'document_data';
protected $guarded = [];
public function document()
{
return $this->belongsTo('App\Document', 'documentId');
}
}
I am trying to understand the boot function and whether I have set it up properly? When I delete a project, its deleted_at timestamp is set. I am also looking for it to set the deleted at timestamp for all of that Projects Documents and DocumentData.
At the moment, when I delete a Project, on its deleted_at timestamp is set. The Document and DocumentData remains null.
How can I get it soft deleting through all related models?
Thanks
You're using the boot method correctly. The only thing i notice is a mistake in the handler of your Project's deleted event. You're trying to delete the instance again after it's been deleted. Instead, i suppose, you want to delete the associated Documents like so:
public static function boot()
{
parent::boot();
static::deleted(function($project)
{
$project->document()->delete();
});
}
Alternative Method:
The way i usually delete children is to override the delete() method on the parent models. I prefer the resulting code that way, but that's just a personal preference.
In your case that would be:
class Project extends Model {
public function delete()
{
parent::delete(); // first delete the Project instance itself
$this->document()->delete(); // delete children
}
}
class Document extends Model {
public function delete()
{
parent::delete();
$this->documentData()->delete();
}
}
trying to get some polymorphic relations working in laravel 4.
I have a CoreUserAccount table that looks like so:
core_user_account:
id
profile_id
other columns
I then have 2 other tables core_user_client_profile and core_user_consultant_profile. They both have id fields.
i am trying to link them like so:
In the CoreUserAccount model:
public function profile()
{
return $this->morphTo();
}
And in both the other tables i have:
public function user()
{
return $this->morphOne('CoreUserAccount', 'profile');
}
But i don't seem to be able to get any of the profile data through the user object. Any ideas?
Cheers
EDIT
Here is my core_user_account table:
And here are my 2 different profile type tables core_user_client_profile and core_user_consultant_profile:
Here is my model for core_user_account:
<?php
use Illuminate\Auth\UserInterface;
use Illuminate\Auth\Reminders\RemindableInterface;
class CoreUserAccount
extends Eloquent
implements UserInterface, RemindableInterface
{
protected $table = 'core_user_account';
protected $hidden = array('password');
protected $guarded = array('id', 'password');
public function profileType()
{
return $this->belongsTo('CoreUserProfileType');
}
public function address()
{
return $this->belongsTo('CoreUserAddress', 'user_account_id', 'id');
}
public function profile()
{
return $this->morphTo();
}
public function getAuthIdentifier() {
return $this->getKey();
}
public function getAuthPassword() {
return $this->password;
}
public function getReminderEmail() {
return $this->email;
}
public function getRememberToken() {
return $this->remember_token;
}
public function setRememberToken($value) {
$this->remember_token = $value;
}
public function getRememberTokenName() {
return 'remember_token';
}
}
And my core_user_client_profile model:
class CoreUserClientProfile
extends Eloquent
{
protected $table = 'core_user_client_profile';
public function profileLanguage()
{
return $this->belongsTo('CoreUserProfileLanguages');
}
public function user()
{
return $this->morphOne('CoreUserAccount', 'profile',null,'profile_id',null);
}
}
And my core_user_consultant_profile model:
class CoreUserConsultantProfile
extends Eloquent
{
protected $table = 'core_user_consultant_profile';
public function profileLanguage()
{
return $this->belongsTo('CoreUserProfileLanguages', 'consultant_profile_id', 'id');
}
public function qualifications()
{
return $this->belongsTo('CoreUserConsultantQualifications', 'profile_id', 'id');
}
public function registration()
{
return $this->belongsTo('CoreUserConsultantRegistrations', 'profile_id', 'id');
}
public function specialities()
{
return $this->belongsTo('CoreUserConsultantProfileSpeciality', 'profile_id', 'id');
}
public function user()
{
return $this->morphOne('CoreUserAccount', 'profile');
}
}
I have read the document pointed out in the comments below and understand i seem to have to have a type column in my DB. But what data needs to be stored here? does it automatically get stored or do i have to store it?
Cheers
You should have to have profile_type, and profile_id columns. In profile_type will be stored values: 'CoreUserConsultantProfile' or 'CoreUserClientProfile'. This fields will be populated automaticlly when you save data.
This is example how save data in polymorph relation.
$profile = CoreUserConsultantProfile::find(1);
$account = new CoreUserAccount();
$profile->profile()->save($account);
So now, profile_id=1, and profile_type = 'CoreUserConsultantProfile'
I have 2 models defined:
Program model:
class Program extends \Eloquent {
protected $guarded = [];
public static function boot()
{
parent::boot();
static::deleting(function($program)
{
DB::table('descriptions')->where('id',$program->id)->delete();
return true;
});
}
public function description()
{
return $this->hasOne('Description');
}
}
Description model, where description for the program is defined
class Description extends \Eloquent {
protected $guarded = [];
public function program()
{
return $this->belongsTo('Program','id','id');
}
}
When I delete Program with specific name I want the description for that program to be deleted too.
So:
Program::where('name',Input::get('name'))->delete();
Unfortunately this code doesn't fire "deleting" event for Program model and the description isn't deleted.
Whats wrong ?
The solution is :
Program::where('name',Input::get('name'))->first()->delete();
I would like to do something like this.
Location::where('city', '=', 'Chicago')->chef();
With these relationships:
class Location extends Eloquent {
protected $table = 'locations';
public function chef() {
return $this->belongsTo('Chef');
}
}
class Chef extends Eloquent {
protected $table = 'chefs';
public function location() {
return $this->hasMany('Location');
}
}
This should work:
class Location extends Eloquent {
protected $table = 'locations';
public function chefs() {
return $this->belongsTo('Chef');
}
public function getAllChefsByCity($city)
{
$this->with('chefs')->where('city', $city)->get();
}
}
Then in your code:
$array = $location->getAllChefsByCity('Chicago');