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;
Related
im using laravel 7.24 and php 7.4 on my project
What i want to is fundementally creating relations between 3 table and using them in 'one' query.
to be specific i need to access 'ordered products' from my order detail page.
public function orderdetail($id)
{ //certainorder model access to 'ShoppingCard'model from below
$orderDetails = CertainOrder::with('ShoppingCard.shoppingCardProducts.product')
->where('ShoppingCard.id' , $id)->firstorFail();
return view('orderdetails', compact ('orderDetails'));
}
CertainOrder model access to 'ShoppingCard' model from top and in the ShoppingCard model it contains shoppingCardProducts function which you will see in below and with shoppingCardProducts function my 'products' table had a relation. the problem is something in relations is wrong and i can't get data from shoppingcardproduct
class ShoppingCard extends Model
{
protected $table = "shopping_card";
protected $fillable = ['id', 'user_id', 'created_at','updated_at'];
public function shoppingCardProducts()
{
return $this->hasMany('App\ShoppingCardProduct');
}
class CertainOrder extends Model
{
protected $table = "certain_orders";
protected $guarded = [];
public function shoppingCard()
{
return $this->belongsTo(ShoppingCard::class, 'sepet_id');
//sepet_id is a foreign key.
}
class ShoppingCardProduct extends Model
{
use SoftDeletes;
protected $table = "shopping_card_product";
protected $fillable = ['id', 'sepet_id', 'urun_id','quantity','price','status','created_at','updated_at','deleted_at'];
public function product()
{
return $this->belongsTo('App\Product');
}
I think you missed it somewhere in the code
class ShoppingCard extends Model
{
protected $table = "shopping_card";
protected $fillable = ['id', 'user_id', 'created_at','updated_at'];
public function shoppingCardProducts()
{
return $this->hasMany('App\ShoppingCardProduct');
}
public function CertainOrder(){
return $this->hasMany('path\to\model');
}
public function ShoppingCardProduct(){
return $this->hasMany('path\to\model');
}
}
class CertainOrder extends Model
{
protected $table = "certain_orders";
protected $guarded = [];
public function shoppingCard()
{
return $this->belongsTo('App\path\to\model', 'sepet_id');
//sepet_id is a foreign key.
}
}
class ShoppingCardProduct extends Model
{
use SoftDeletes;
protected $table = "shopping_card_product";
protected $fillable = ['id', 'sepet_id', 'urun_id','quantity','price','status','created_at','updated_at','deleted_at'];
public function ShoppingCard()
{
return $this->belongsTo('App\ShoppingCard');
}
}
and make the call this way
public function orderdetail($id)
{ //certainorder model access to 'ShoppingCard'model from below
$orderDetails = ShoppingCard::with('CertainOrder, ShoppingCardProduct')
->where('id' , $id)->firstorFail();
return view('orderdetails', compact ('orderDetails'));
}
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'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!
I try to connect table with ORM.
I have a 'Customers' table and 'Contacts'.
In my models I have write this :
class Customer extends Eloquent {
protected $fillable = [];
protected $softDelete = true;
protected $guarded = array();
public function contacts()
{
return $this->hasMany('Contact');
}
}
and this
class Contact extends Eloquent {
protected $fillable = [];
protected $softDelete = true;
protected $guarded = array();
public function customer()
{
return $this->belongsTo('Customer');
}
}
And in my controller when I try
return Customer::find($id)->contacts
I have no result :/
What I missing ?
Thank you
My Bad!
Ma database seeder save seed 140 customers and 35 contacts.
I try to get the 140th contact...
My code works perfectly