I'm working with Eloquent and Laravel 5, added a field at $appends attribute on model and a mutator to get it value. My issue is, how get this field only when it is called?
public static $model = 'Model';
protected $table = 'table';
protected $primaryKey = 'idtable';
public $incrementing = false;
public $timestamps = false;
protected $appends = ['DescComposta'];
//Mutators
public function getDescCompostaAttribute()
{
return $this->attributes['desc_composta'] = 'yes';
}
Thank you all!
Related
I am trying to work on an eloquent model Post where it has two hasOne relationships: offer_post and request_post.
Post.php
class Post extends Model
{
use HasFactory;
protected $table = 'tbl_post';
protected $fillable = ['email', 'postIdentity', 'postStatus'];
public $timestamps = false;
public $primaryKey = 'indexPost';
public static function boot()
{
parent::boot();
self::creating(function ($model) {
$model->postNumber = (string) Uuid::generate(4);
});
}
public function pasabuy_user() {
$this->belongsTo(PasabuyUser::class, 'email', 'email');
}
public function offer_post() {
return $this->hasOne(OfferPost::class, 'postNumber', 'postNumber');
}
public function request_post() {
return $this->hasOne(RequestPost::class, 'postNumber', 'postNumber');
}
}
OfferPost.php
class OfferPost extends Model
{
use HasFactory;
protected $table = 'tbl_shoppingOfferPost';
protected $fillable = ['postNumber', 'postStatus', 'deliveryArea', 'shoppingPlace', 'deliverySchedule', 'transportMode', 'capacity', 'paymentMethod', 'caption'];
public $timestamps = false;
public $primaryKey = 'indexShoppingOfferPost';
public function post() {
return $this->belongsTo(Post::class, 'postNumber', 'postNumber');
}
}
RequestPost.php
class RequestPost extends Model
{
use HasFactory;
protected $table = 'tbl_orderRequestPost';
protected $fillable = [
'postNumber',
'postStatus',
'deliveryAddress',
'shoppingPlace',
'deliverySchedule',
'paymentMethod',
'shoppingList',
'caption'
];
public $timestamps = false;
public $primaryKey = 'indexOrderRequestPost';
/**
* [post description]
* #author Al Vincent Musa
* #return [type] [description]
*/
public function post() {
return $this->belongsTo(Post::class, 'postNUmber', 'postNumber');
}
}
This is how my tables looks like.
Now I want to get all post(both offer post and request post)
Get offer post like
SELECT from tbl_post INNER JOIN tbl_shoppingOfferPost ON tbl_post.postNumber = tbl_shoppingOfferPost.postNumber
and likewise for the request post.
I came as far as
$post = Post::has('offer_post')->get();
but this only returns data from tbl_post. But want both from tbl_post and tbl_shoppingOfferPost.
I am having a hard time understanding the docs. I don't know where to start. Any help is much appreciated. I just need a pointer where to look at. Thanks
You can do like this
$post::with(['offer_post','request_post'])->get();
Not working my many to many relationship hmcs_tender is my table name
This is My Airportcode model
class AirPortCode extends Model
{
Protected $table = 'hmsc_air_port_codes';
public $timestamps = false;
Protected $primaryKey = 'id';
public function airPorts()
{
return $this->belongsToMany('App\Tender_Module\Admin_Module\TenderMaster','hmsc_bid_origin_port_comm','bid_id','port_id');
}
}
This is my Model(TenderModel.php)
class TenderMaster extends Model
{
protected $table = 'hmcs_tender';
public $timestamps = false;
protected $primarykey = 'id';
public function bits()
{
return $this->belongsToMany('App\Tender_Module\Masters\Air\AirPortCode','hmsc_bid_origin_port_comm','port_id','bid_id');
}
}
i have 2 model Article and ArtCategories
I made the one-to-many relation between two model using belongsTo() and hasMany(). hasMany() relation works perfectly but belongTo() doesn't work. Does anyone know where did I make a mistake?
.
Code model
class ArtCategories extends Model
{
protected $table = 'pjt_categories_article';
protected $primaryKey = 'cate_id';
protected $fillable = ['cate_id','categories'];
public function Article(){
return $this->hasMany(Article::class);
}
}
class Article extends Model
{
protected $table = 'pjt_article';
protected $primaryKey = 'article_id';
protected $fillable = ['article_id','title','descriptions','username','cate_id','status','visit','reference'];
public function ArtCategories(){
return $this->belongsTo(ArtCategories::class,'cate_id');
}
public function admin(){
return $this->belongsTo(Admin::class);
}
}
This is DB structure Table up is pjt_article ,table down is pjt_categories_article
Result
$art = Article::findOrFail($article_id);
$cate = ArtCategories::pluck('categories', 'cate_id');
dd($art);
Relation Not working
You should add foreign key in relation method of ArtCategories
class ArtCategories extends Model
{
protected $table = 'pjt_categories_article';
protected $primaryKey = 'cate_id';
protected $fillable = ['cate_id','categories'];
public function article(){
return $this->hasMany(Article::class, 'cate_id');
}
}
Now fetch article with ArtCategories as:
$art = Article::with('ArtCategories')->findOrFail($article_id);
$cate = ArtCategories::pluck('categories', 'cate_id');
dd($art);
As laravel convention Article() method should be articles() and ArtCategories() should be artCategory().
I have a table with column ID. I want to make a accessor for the same column.
Model.
protected $table = 'repair_category';
protected $fillable = ['ID,','Name','Active','Background_Color','Icon_File_Name','ListOrder'];
protected $primaryKey = 'ID';
I tried
public function getIDAttribute($value)
{
return Crypt::encrypt($value);
}
and
public function getIdAttribute($value)
{
return Crypt::encrypt($value);
}
But its not working.
Anyone please suggest a way to solve this issue.
Try reading from $this->attributes[] in the accessor method
protected $primaryKey = 'ID';
public function getIDAttribute()
{
return Crypt::encrypt($this->attributes['ID']);
}
I'm trying to bind multiple models to a form. Currently, I have 4 models:
<?php
class DemDataSet extends Eloquent {
public $timestamps = false;
protected $connection = 'epcr_dem_data';
protected $table = 'DEMDataSet';
protected $primaryKey = 'pk_DEMDataSet';
public function DemographicReport(){
return $this->hasOne('DemographicReport','fk_DemDataSet','pk_DemDataSet');
}
}
class DemographicReport extends Eloquent {
public $timestamps = false;
protected $connection = 'epcr_dem_data';
protected $table = 'DemographicReport';
protected $primaryKey = 'pk_DemographicReport';
public function DemDataSet(){
return $this->belongsTo('DemDataSet','fk_DemDataSet','pk_DemDataSet');
}
public function dAgency(){
return $this->hasOne('dAgency','fk_DemographicReport','pk_DemographicReport');
}
}
class dAgency extends Eloquent {
public $timestamps = false;
protected $connection = 'epcr_dem_data';
protected $table = 'dAgency';
protected $primaryKey = 'pk_dAgency';
public function DemographicReport(){
return $this->belongsTo('DemographicReport','fk_DemographicReport','pk_DemographicReport');
}
public function dAgency_10(){
return $this->hasMany('dAgency_10','fk_dAgency','pk_dAgency');
}
}
class dAgency_10 extends Eloquent {
public $timestamps = false;
protected $connection = 'epcr_dem_data';
protected $table = 'dAgency_10';
protected $primaryKey = 'pk_dAgency_10';
public function dAgency(){
return $this->belongsTo('dAgency','fk_dAgency','pk_dAgency');
}
}
?>
And I'm passing it to my view through my controller like so:
public function index()
{
//THIS is the line I need help with (I think):
$agency = dAgency::with('dAgency_10','DemographicReport')->find(1);
//for troubleshooting:
echo "<pre>",print_r(dAgency::with('dAgency_10','DemographicReport')->find(1)->toArray()),"</pre>";
return View::make('test')
->with('agency', $agency);
}
I'm able to bind everything except for data from the DemDataSet model, which I can't even figure out how to establish the relationship between it and my dAgency model. So basically, I just want the fields in the DemDataSet model to be available to my view, then obviously I want to be able to perform all the CRUD operations eventually.
Looking at your models, you should just be able to access it via the DemographicReport.
i.e.,
$dataset = $agency->DemographicReport->DemDataSet;