Laravel Eloquent eager loading, few columns related to one table - php

I have the Models structure from database like this:
class Player extends Model
{
protected $table = 'players';
}
related to Players (this table always have to have 5 fixed positions):
class FormationPositions
{
protected $table = 'formation_position';
public function fpos1()
{
return $this->belongsTo(Player::class, 'fpos_1');
}
public function fpos2()
{
return $this->belongsTo(Player::class, 'fpos_2');
}
public function fpos3()
{
return $this->belongsTo(Player::class, 'fpos_3');
}
public function fpos4()
{
return $this->belongsTo(Player::class, 'fpos_4');
}
public function fpos5()
{
return $this->belongsTo(Player::class, 'fpos_5');
}
}
when I try to eager load FormationPositions table like this:
FormationPositions::with(['fpos1', 'fpos2', 'fpos3', 'fpos4', 'fpos5'])
then I got 5 queries to database to load these object, maybe there is some way to combine this structure into one query with Elequent eager way SELECT * FROM table where id IN (?) and load data to that objects with one query?

Related

How can I get the combined query for two hasManyThrough

I have two relationships on the users' table:
public function tokens_records()
{
return $this->hasManyThrough(Record::class, Token::class);
}
and
public function websites_records()
{
return $this->hasManyThrough(Record::class, Website::class);
}
How can I merge both queries into one?
If you want to have the records for both relationships, this is gonna work:
class Entity extends Model
{
public function tokens_records()
{
return $this->hasManyThrough(Record::class, Token::class);
}
public function websites_records()
{
return $this->hasManyThrough(Record::class, Website::class);
}
}
$records = $entity->tokens_records
->merge($entity->websites_records);

Laravel Dingo get data from multiple tables

I'm trying to retrieve data from two tables with relationship one-to-many. I need it to return in one response since it will be displayed simultaneously. However, I don't understand how to return extended object. For now I have something like this in controller
public function show(Site $id)
{
foreach ($id->features() as $feature) {
$id->features[] = $feature;
}
return $id;
}
And this is my model
class Site extends Model
{
protected $fillable = ['path', 'site_link'];
public $timestamps = false;
public function features() {
return $this->hasMany('App\SiteFeature');
}
}
For now it returns an empty array of features property.
If you are using implicit model binding then Lazy eager Load features relationship in controller action because model is already loaded
public function show(Site $site)
{
$site->load('features');
return $site;
}
If no implicit model binding then Eager load features using with at time of model loading
public function show($id)
{
$site = Site::with('features')->find($id);
return $site;
}
Check details for loading relationship https://laravel.com/docs/5.6/eloquent-relationships#constraining-eager-loads

How to apply relationship with two id's in laravel

So I have a model called data_storage and another model entity_states
I have to fetch the record from data_storage with entity_states where entity_state has data_storage_id and state_id.
How can I use eloquent to achieve this ?.
Or Ill have to use Query builder and use innerJoin?
Update1
My Actual Query
$this->values['new_leads'] = $data_storages->with('actions','states','sla')->where('wf_id',$wfid)->get();
My data_storage modal
class data_storages extends Model
{
//
protected $fillable = ['layout_id','member_id','company_id','team_id','data','status','wf_id'];
function actions()
{
return $this->hasMany('App\Models\ActionDataMaps', 'data_id', 'id' );
}
function states()
{
return $this->hasOne('App\Models\workflow_states','id','status');
}
function sla()
{
//Here I have to get those row from entity_states model where , data_storage_id and state_id
}
}
Thanks
Here's the more reasonable way to do it:
class DataStorage extends Model {
public states() {
return $this->belongsToMany(State::class,"entity_states");
}
}
class State extends Model {
public storages() {
return $this->belongsToMany(DataStorage::class,"entity_states");
}
}
Then you can eager-load related models via e.g.:
$storage = DataStorage::with("states")->first();
$storage->states->first()->column_in_related_state;
Or via the state:
$state = State::with("storages")->first();
$state->storages->first()->column_in_related_storage;
If there are additional columns in the pivot table entity_states then you can refer to them in the relationship as e.g.:
public states() {
return $this->belongsToMany(State::class)->withPivot("pivot_column");
}
In your model data_storage you can define a property / method entity_states to get them:
class data_storage extends Model
{
public function entity_states()
{
return $this->hasMany('App\entity_states','data_storage_id')->where('state_id ','=',$this->table());
}
}
Then you can access them in an instance by
$entityStatesOfDataStorage = $yourDataStorageInstance->entity_states;
See this link:
https://laravel.com/docs/5.3/eloquent-relationships
for Query Builder you may use this:
DB::table('data_storage')
->join('entity_states','data_storage.data_storage_id','=','entity_states.state_id')
->get();
For your reference Laravel Query Builder

Laravel querying model relations

I'm still struggeling with the laravel Models. At first I tried doing it all using the tables, but thats not smart, I'll miss out on lots of the laravel functions.
I have the following setup
ProjectTwitterStatus links the projects and the twitter statuses.
TwitterStatus has all the details of a twitter status and has a unique ID ('posted at' datetime of tweet is among the details)
TwitterRetweets has the ID of the TwitterStatus - the actual retweet - and the tweet ID of the retweeted status
TwitterReplies has the ID of the TwitterStatus - that is the actual reply - and/or the user ID if not a reply to a status but to a user.
What I want? To get for each date (DATE(datetime)) the count of the statuses, retweets and replies, using the laravel model relations.
These are the models.
class ProjectTwitterStatus extends Eloquent {
protected $table = 'project_twitter_statuses';
protected $softDelete = true;
public function twitterStatus() {
return $this->belongsTo('TwitterStatus');
}
public function project() {
return $this->belongsTo('Project');
}
}
class TwitterStatus extends Eloquent {
protected $table = 'twitter_statuses';
public function twitterRetweet() {
return $this->hasMany('TwitterRetweet');
}
public function twitterReply() {
return $this->hasMany('TwitterReply');
}
public function twitterUser() {
return $this->belongsTo('TwitterUser');
}
public function projectTwitterStatus() {
return $this->hasMany('ProjectTwitterStatus');
}
}
class TwitterRetweet extends Eloquent {
protected $table = 'twitter_retweets';
public function twitterStatus() {
return $this->belongsTo('TwitterStatus');
}
}
class TwitterReply extends Eloquent {
protected $table = 'twitter_replies';
public function twitterStatus() {
return $this->belongsTo('TwitterStatus');
}
}
I got the count of the twitterStatuses using this:
$twitterStatuses = TwitterStatus::has('projectTwitterStatus')
->groupBy(DB::raw('DATE(datetime)'))
->get(array(DB::raw('COUNT(id) AS tweets'),DB::raw('DATE(datetime) AS date')));
I tried for example this to get the retweet count added but that has no effect (a reference to the model apears in the object -> array().
$twitterStatuses = TwitterStatus::has('projectTwitterStatus')
->with(array('twitterRetweet' => function($query)
{
$query->count();
}))
->groupBy(DB::raw('DATE(datetime)'))
->take(10)
->get(array(DB::raw('COUNT(id) AS tweets'),DB::raw('DATE(datetime) AS date')));
Can anyone point me in the right direction?
Not 100% sure how your intended solution is to be used - Assuming you simply want a count of the number of retweets related to twitterStatus?
$count = $twitterStatus->twitterRetweet()->count();
where $twitterStatus is an already retrieved model - not a collection.
if $twitterStatus is a collection to iterate through you can also eager load the related model using either with() or load()
Then you can iterate through each model in the collection - depends on how you wanted to use the results

Can same-table eager loads be combined in Laravel 4?

I have a model where I'm eager-loading two references to another table (in this case posts.created_by_id == users.id and posts.updated_by_id == users.id).
class Post {
protected $table = 'posts';
public function scopeLatest() {
return $query->with(['created_by', 'updated_by'])
->orderBy('created_at', 'desc');
}
public function createdBy() {
return $this->belongsTo('User');
}
public function updatedBy() {
return $this->belongsTo('User');
}
}
class User {
protected $table = 'users';
public function posts() {
return $this->hasMany('Post', 'created_by');
}
}
This results in something like the following queries:
SELECT * FROM tbl ORDER BY created_at DESC;
SELECT * FROM users WHERE id IN (?); (created_at)
SELECT * FROM users WHERE id IN (?); (updated_at)
This makes sense - we're loading all of the referenced records in created_by, and then updated_by - however we could optimise this to combine the ids make a single query to users.
My question is: is this something Eloquent currently supports?
Didn't work for me. It seems that the eager load works for only one instance of the table. For me only one of the relationships has been filled. Eloquent probably uses the the table name as a pointer for the eager load. It generates all the eager load queries but only one will be filled.
I had to separate the data into different tables just because of this problem (and haven't had the time to dig that deep into Eloquent code.)
I think this is what you might be looking for:
class Post {
protected $table = 'posts';
public function scopeLatest($query) {
return $query->with('createdBy', 'updatedBy')
->orderBy('created_at', 'desc');
}
public function createdBy() {
return $this->belongsTo('User','created_by_id');
}
public function updatedBy() {
return $this->belongsTo('User','updated_by_id');
}
}
class User {
protected $table = 'users';
public function postsCreated() {
return $this->hasMany('Post', 'created_by_id');
}
public function postsUpdated() {
return $this->hasMany('Post', 'updated_by_id');
}
}

Categories