I am working on an exam/quiz app and it creates tests/quizzes for users and now I must create a set of spreadsheets that contain data such as the present students in a given exam, grades charts and so on.
Bu so far all I managed to create is a sheet with ALL the users using `->fromModel' but if I use any relation and or constrain I get an empty sheet.
I have this models:
<?php
namespace EMMA5;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\DB;
class Exam extends Model
{
//
protected $dates = ['created_at', 'updated_at', 'applicated_at'];
protected $fillable = [
'applicated_at',
'duration',
'board_id',
'passing_grade',
'annotation'
];
public function board()
{
return $this->belongsTo('EMMA5\Board');
}
public function users()
{
return $this->belongsToMany('EMMA5\User')->withPivot('active', 'started_at', 'ended_at', 'seat', 'location_id');
}
...
User model (abbreviated)
class User extends Authenticatable
{
...
//Relations
public function answers()
{
return $this->hasMany(Answer::class);
}
public function exams()
{
return $this->belongsToMany('EMMA5\Exam')->withPivot('active', 'started_at', 'ended_at', 'location_id');
}
...
And I am trying to create a sheet with the users for a given exam:
(This is from my ExamController.php)
/**
* Returns a spreadsheet with only the students that were present
*
* #return PHPOffice
*/
public function gradesSpreadshet(Exam $exam)
{
$grade = new Grade;
$gradedStudents = $grade->allStudents($exam)->toArray();
//dd(\EMMA5\Exam::find(195)->with('users')->get());
//dd($exam->answers->answer);
$data = $exam->users;
return Excel::create("FinalGrades", function ($excel) use($data) {
//Create sheet to be able to return something to keep on testing
//Another sheet
$excel->sheet('Primera hoja', function ($sheet) use($data) {
$sheet->fromArray($data);
});
})->export('xlsx');
}
And I get an empty sheet.
I already tried with ->fromArray() and ->fromModel() .
Will appreciate any input.
Some time passed and nobody answered but I found a solution.
I do not know if it will be helpful for someone else.
The way I got the results couldn't be read by Excel Laravel. So I created a helper function with a callback.
public static function collectionToArray(Array $collect = null)
{
return array_map(function ($array) use($collect)
{
foreach ($array as $key => $value) {
$resultArray[$key] = $value;
}
return $resultArray;
}, $collect);
}
This returns a simplified version of the Collection that can be easily read by Excel Laravel.
Related
I could not find this in the laravel docs on aggregate relationships
I was able to do something like this
private function refreshUsers()
{
$this->users = User::withSum(['taskTimeSessions'=> function ($query) {
$query->whereMonth('created_at',$this->month)
->where('is_reconciled',1);
}],'session_duration_in_seconds')
->get();
}
But now I am trying to query what is the total time a Sprint has or at the very least what the individual tasks inside a sprint have so that I can just sum the total of those somehow.
Sprint has many SprintTasks (pivot table)
SprintTask belongs to one Task
Task has many TaskTimeSessions
So I am trying to go find the total time of the TaskTimeSessions
Sprint::with([
'sprintTasks.task'=> function ($query) {
$query->withSum('taskTimeSessions','session_duration_in_seconds');
}])
->get();
I am not getting any errors, but not finding the result anywhere when dd
I thought i would get lucky and have something like this work
->withSum('sprintTasks.task.taskTimeSessions', 'session_duration_in_seconds')
But I am getting this error
Call to undefined method App\Models\Sprint::sprintTasks.task()
If anyone can help me out with some guidance on how to go about this, even if it doesn't include withSum it would be much appreciated.
As requested, these are the models.
// Sprint
public function sprintTasks()
{
return $this->hasMany(SprintTask::class, 'sprint_id');
}
// SprintTask
protected $fillable = [
'sprint_id',
'task_id',
'is_completed'
];
public function task()
{
return $this->belongsTo(Task::class,'task_id');
}
public function sprint()
{
return $this->belongsTo(Task::class,'sprint_id');
}
// Task
public function taskTimeSessions()
{
return $this->hasMany(TaskTimeSession::class, 'task_id');
}
// TaskTimeSessions
protected $fillable = [
'task_id',
'session_duration_in_seconds'
];
public function task()
{
return $this->belongsTo(Task::class,'task_id');
}
Is it possible to abstract this into the model as like
public function totalTaskTime() {
// using the relationship stuff to figure out the math and return it?
}
Looking for any advice on what the best approach is to do this.
Right now I am literally doing this in the blade and seems very bad
#php
$timeTracked = 0;
foreach ($sprint->sprintTasks as $sprintTask) {
$timeTracked += $sprintTask->task->time_tracked_in_seconds;
}
#endphp
You have a many to many relation between sprint and task
For that you can setup a direct relation belongsToMany with sprint_tasks as the pivot table
// Sprint
public function sprintTasks()
{
return $this->hasMany(SprintTask::class, 'sprint_id');
}
public function tasks()
{
return $this->belongsToMany(Task::class, 'sprint_tasks', 'sprint_id', 'task_id')->withPivot('is_completed');
}
Now you can use that relation to query your needs
Sprint::with(['tasks'=> function ($query) {
$query->withSum('taskTimeSessions','session_duration_in_seconds');
}])
->get();
There is a good package for Laravel for complex relationships - eloquent-has-many-deep. You can use it to build relationships through an unlimited number of tables.
composer require staudenmeir/eloquent-has-many-deep:"^1.7"
Sprint.php
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Staudenmeir\EloquentHasManyDeep\HasManyDeep;
use Staudenmeir\EloquentHasManyDeep\HasRelationships;
class Sprint extends Model
{
use HasRelationships;
public function tasks(): BelongsToMany
{
return $this->belongsToMany(Task::class, 'sprint_tasks');
}
public function taskTimeSessions(): HasManyDeep
{
return $this->hasManyDeepFromRelations($this->tasks(), (new Task())->taskTimeSessions());
}
}
Task.php
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\HasMany;
class Task extends Model
{
use HasFactory;
public function taskTimeSessions(): HasMany
{
return $this->hasMany(TaskTimeSession::class);
}
}
Result:
$sprints = Sprint::withSum('taskTimeSessions', 'session_duration_in_seconds')->get();
i trying to load all rows from a model without the relationship.
The attributes $with it not event set on my Event model but when i do
$events = Event::all();
all my relationship are loaded, and i can see all the query with the dbquerylog.
i don't understand why theses relationship are loaded,
Please help me !
Thanks you.
I'm using Laravel 8.
here's an example.
class Event extends Model {
public function items() {
return $this->hasMany(Item::class);
}
public function items2() {
return $this->hasMany(Item2::class);
}
public function items3() {
return $this->hasMany(Item3::class);
}
public function items4() {
return $this->hasOne(Item4::class);
}
}
$events = Event::all();
If you have a single instance of a model object, you can do:
$obj->withoutRelations();
As laravel documentations says you can use without: https://laravel.com/docs/8.x/eloquent-relationships
Model
protected $with = ['item1','item2','item3','item4'];
Controller
$events = Event::without(['item1','item2','item3','item4'])->get();
I met this problem one day, and it turned out that I was using relation in scope method. Because of this relation values were added to response.
Check out this example:
class Event extends Model {
public function items() {
return $this->hasMany(Item::class);
}
[...]
public function scopeItemsGreen() {
return $this->items->every(function ($item) {
return $item->color == 'green';
});
}
I have a application that I am trying to add a second Module to.
Basically i'm cloning an existing module that is already working with my database. All code is exactly the same as the original/working module, other than a different table name and different controller/class/model names.
I have 3 new tables that are related to the new module, just like with the old module investments, investment_categories and investment_vendors.
Every time I try to use my new module it wants to upload the vendor and category name value to the investments table when it should be uploading that to the respective investment_categories and investment_vendor tables.
I don't believe the issue is with my new module code, but somewhere else within composer.
I don't expect that I would need to run composer dump-autoload as I've always edited my modules without running that (but this is my first time updating one to query/write to a different table).
Is there something else I need to be running to get this to work?
The below error I see in the log is:
[2018-01-24 21:13:21] production.ERROR: PDOException: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'category_name' in 'field list' in /var/www//vendor/laravel/framework/src/Illuminate/Database/Connection.php:390
Controller File:
<?php
namespace MC\Modules\Investments\Controllers;
use MC\Http\Controllers\Controller;
use MC\Modules\Investments\Models\Investment;
use MC\Modules\Investments\Models\InvestmentCategory;
use MC\Modules\Investments\Models\InvestmentVendor;
class InvestmentController extends Controller
{
use ReturnUrl;
public function index()
{
$this->setReturnUrl();
$investments = Investment::defaultQuery()
->keywords(request('search'))
->categoryId(request('category'))
->vendorId(request('vendor'))
->sortable(['investment_date' => 'desc'])
->paginate(config('mc.defaultNumPerPage'));
return view('investments.index')
->with('investments', $investments)
->with('displaySearch', true)
->with('categories', ['' => trans('mc.all_categories')] + InvestmentCategory::getList())
->with('vendors', ['' => trans('mc.all_vendors')] + InvestmentVendor::getList())
}
Create Controller File:
<?php
namespace MC\Modules\Investments\Controllers;
use MC\Http\Controllers\Controller;
use MC\Modules\Investments\Models\Investment;
class InvestmentCreateController extends Controller
{
public function create()
{
return view('investments.form')
->with('editMode', false)
->with('currentDate', DateFormatter::format(date('Y-m-d')))
}
public function store()
{
$record = request()->except('attachments');
$record['investment_date'] = DateFormatter::unformat($record['investment_date']);
$record['amount'] = NumberFormatter::unformat($record['amount']);
$record['tax'] = ($record['tax']) ? NumberFormatter::unformat($record['tax']) : 0;
$investment = Investment::create($record);
return redirect($this->getReturnUrl())
->with('alertSuccess', trans('mc.record_successfully_created'));
}
}
Model:
<?php
namespace MC\Modules\Investments\Models;
use Illuminate\Database\Eloquent\Model;
class Investment extends Model
{
use Sortable;
protected $table = 'investments';
protected $guarded = ['id'];
protected $sortable = ['investment_date', 'investment_categories.name', 'description', 'amount'];
public static function boot()
{
parent::boot();
}
/*
|--------------------------------------------------------------------------
| Relationships
|--------------------------------------------------------------------------
*/
public function attachments()
{
return $this->morphMany('MC\Modules\Attachments\Models\Attachment', 'attachable');
}
public function category()
{
return $this->belongsTo('MC\Modules\Investments\Models\InvestmentCategory');
}
public function vendor()
{
return $this->belongsTo('MC\Modules\Investments\Models\InvestmentVendor');
}
/*
|--------------------------------------------------------------------------
| Accessors
|--------------------------------------------------------------------------
*/
public function getAttachmentPathAttribute()
{
return attachment_path('investments/' . $this->id);
}
public function getFormattedAmountAttribute()
{
return CurrencyFormatter::format($this->amount);
}
public function getFormattedTaxAttribute()
{
return CurrencyFormatter::format($this->tax);
}
public function getFormattedInvestmentDateAttribute()
{
return DateFormatter::format($this->investment_date);
}
/*
|--------------------------------------------------------------------------
| Scopes
|--------------------------------------------------------------------------
*/
public function scopeCategoryId($query, $categoryId = null)
{
if ($categoryId)
{
$query->where('category_id', $categoryId);
}
return $query;
}
public function scopeDefaultQuery($query)
{
return $query->select('investments.*', 'investment_categories.name AS category_name',
'investment_vendors.name AS vendor_name', 'clients.unique_name AS client_name')
->join('investment_categories', 'investment_categories.id', '=', 'investments.category_id')
->leftJoin('investment_vendors', 'investment_vendors.id', '=', 'investments.vendor_id');
}
public function scopeVendorId($query, $vendorId = null)
{
if ($vendorId)
{
$query->where('vendor_id', $vendorId);
}
return $query;
}
}
One thing to note is that I do have existing data in the table and the query works fine when viewing it from my web page, however the issue is when trying to edit it or insert new data/record.
As you can see from the model above,
investments.*', 'investment_categories.name AS category_name
the query works because of that command.
I cannot find anywhere else where it determines how to take "investment_categories.name as category_name".
Can you tell me why it is trying to write the category_name value to the investments table instead of the investment_categories table's name column?
i have this table structure, project has one to many relation with rewards , rewards and shipping has many to many relation with pivot table reward_ship.
projects rewards shipping reward_ship
--------- -------- -------- ------------
id id id id
title amount location reward_id
amount project_id name ship_id
i am trying to extract one particular project details with all other associate tables data(rewards and shipping data using reward_ship table) in one query.
These is how i am trying
Projects Model
class Rewards extends Model {
public function projs(){
return $this->hasMany('App\Rewards');
}
public function rewds(){
return $this->belongsToMany('App\Shipping')
->withPivot('reward_ship', 'ship_id', 'reward_id');
}
public function shiplc(){
return $this->belongsToMany('App\Rewards')
->withPivot('reward_ship', 'ship_id', 'reward_id');
}
}
class Rewards extends Model {
public function proj() {
return $this->belongsTo('App\Projects');
}
}
Controller api class
Route::get('projects/{id}', function($id) {
$p = Projects::find($id);
$getd = Rewards::with('proj')
->where('rewards.project_id', '=', $p->id)
->get();
});
it doesn't work.
i search and tried many related model base query in larvel.
i know my implementation are wrong. Please suggest me to work out.
You can use Laravel 5.5 new feature API Resources.
It helps you to format the output of objects such as models or collections, to display attributes and also relationships.
So, you could do something like this in your ItemResource:
<?php
namespace App\Http\Resources;
use Illuminate\Http\Resources\Json\Resource;
class Project extends Resource
{
/**
* Transform the resource into an array.
*
* #param \Illuminate\Http\Request
* #return array
*/
public function toArray($request)
{
return [
'project_id' => $this->project_id,
'title' => $this->title,
'amount' => $this->amount,
// To access relationship attributes:
'rewards' => $this->rewards->load('shippings'),
];
}
}
Then in your controller, you just need to create a new Resource instance and pass the item object that you want to return:
use App\Http\Resources\Project as ProjectResource;
// some code
/**
* Show a single formatted resource.
*
* #param Project $project
* #return ProjectResource
*/
public function show($project)
{
return new ProjectResource($project);
}
// the rest of your code
The output should be the expected.
You have to fix the relationships that you have :
Projects Model :
public function rewards(){
return $this->hasMany('App\Rewards');
}
Rewards Model :
public function projects() {
return $this->belongsTo('App\Projects');
}
public function shippings(){
return $this->belongsToMany('App\Shipping','reward_ship', 'reward_id', 'ship_id');
}
Shipping model:
public function rewards(){
return $this->belongsToMany('App\Rewards','reward_ship', 'ship_id', 'reward_id');
}
After that you can call the relationships in the controller to eager load the wanted elements like this :
$project = Projects::with('rewards.shippings')
->where('id', $project_id)
->get();
And in the view you can loop over the rewards then get the shippings like this :
#foreach ($project->rewards as $reward)
<p>This is a reword {{ $reward->amount }}</p>
#foreach ($reward->shippings as $shipping)
<p>This is a shipping {{ $shipping->name }}</p>
#endforeach
#endforeach
class Project extends Model
{
public function rewds()
{
return $this->hasMany('App\Rewards');
}
public function shiplc()
{
return $this->hasManyThrough('App\Shipping', 'App\Rewards');
}
}
class Rewards extends Model
{
public function shiplc()
{
return $this->belongsToMany('App\Shipping');
}
public function projs()
{
return $this->belongsTo('App\Project');
}
}
class Shipping extends Model
{
public function shiplc()
{
return $this->belongsToMany('App\Shipping');
}
}
Route::get('projects/{id}', function($id) {
$p = Projects::with(['rewds', 'shiplc'])->find($id);
});
Project.php
class Project extends Model {
public function rewards() {
return this->hasMany(Reward::class, 'project_id', 'id');
}
}
Reward.php
class Reward extends Shipping {
public function shipping(){
return $this->belongsToMany(Shipping::class, 'reward_ship', 'reward_id', 'ship_id');
}
public function project(){
return $this->belongsTo(Project::class);
}
}
You can retrieve it like this:
$projectDetails = Project::where('id', $projectId)
->with(['rewards', 'rewards.shipping'])->get();
I hope I can explain this clearly, apologies in advance if it is confusing. I have a goals table which hasOne of each of bodyGoalDescs, strengthGoalDescs and distanceGoalDescs as shown below
goals.php
class Goal extends BaseModel
{
protected $guarded = array();
public static $rules = array();
//define relationships
public function user()
{
return $this->belongsTo('User', 'id', 'userId');
}
public function goalStatus()
{
return $this->hasOne('GoalStatus', 'id', 'goalStatus');
}
public function bodyGoalDesc()
{
return $this->hasOne('BodyGoalDesc', 'id', 'bodyGoalId');
}
public function distanceGoalDesc()
{
return $this->hasOne('DistanceGoalDesc', 'id', 'distanceGoalId');
}
public function strengthGoalDesc()
{
return $this->hasOne('StrengthGoalDesc', 'id', 'strengthGoalId');
}
//goal specific functions
public static function yourGoals()
{
return static::where('userId', '=', Auth::user()->id)->paginate();
}
}
each of the three tables looks like this with the function details changed
class BodyGoalDesc extends BaseModel
{
protected $guarded = array();
public static $rules = array();
/**
* The database table used by the model.
*
* #var string
*/
protected $table = 'bodyGoalDescs';
//define relationships
public function goal()
{
return $this->belongsTo('Goal', 'bodyGoalId', 'id');
}
}
a goal has either a body goal, a strength goal, or a distance goal. I am having a problem with this method in the controller function
<?php
class GoalsController extends BaseController
{
protected $goal;
public function __construct(Goal $goal)
{
$this->goal = $goal;
}
/**
* Display the specified resource.
*
* #param int $id
* #return Response
*/
public function show($id)
{
$thisgoal = $this->goal->find($id);
foreach ($this->goal->with('distanceGoalDesc')->get() as $distancegoaldesc) {
dd($distancegoaldesc->DistanceGoalDesc);
}
}
}
when I pass through goal 1 which has a distance goal the above method dies and dumps the Goal object with the details of goal 1 and an array of its relations including an object with DistanceGoalDes.
when I pass through goal 2 it passes through exactly the same as if I had passed through goal 1
if I dd() $thisgoal i get the goal that was passed through
what I want ultimately is a method that returns the goal object with its relevant goal description object to the view but this wont even show me the correct goal details not too mind with the correct relations
this function is now doing what I want it to do, I am sure there is a better way (besides the fact that its happening in the controller right now) and I would love to hear it.
public function show($id)
{
$thisgoal = $this->goal->find($id);
if (!$thisgoal->bodyGoalDesc == null) {
$goaldesc = $thisgoal->bodyGoalDesc;
return View::make('goals.show')
->with('goal', $thisgoal)
->with('bodygoaldesc', $goaldesc);
} elseif (!$thisgoal->strengthGoalDesc == null) {
$goaldesc = $thisgoal->strengthGoalDesc;
return View::make('goals.show')
->with('goal', $thisgoal)
->with('strengthgoaldesc', $goaldesc);
} elseif (!$thisgoal->distanceGoalDesc == null) {
$goaldesc = $thisgoal->distanceGoalDesc;
return View::make('goals.show')
->with('goal', $thisgoal)
->with('distancegoaldesc', $goaldesc);
}
}