Make a sum() of column accessors Laravel - laravel

im improving my skills in laravel making a simple challenges web but i stuck on the next problem.
I have the users table with the id, name, mail, etc. The challenges table where i list all the challenges (with columns id, name, body, answer, score) and a third table called "Answers" where i post each try of the users solving a challenge with the columns id, user_id, challenge_id, success (boolean) and score.
Now i'am trying to make a ranking of users with their scores. The problem is to show a sum() of the column 'score' in the table answers where user_id.answers = users.id. So i can get easy the total score for each user.
For now i solved the problem doing this in blade:
#foreach($users as $user)
<tr>
<th>{{ $user->id }}</th>
<td>{{ $user->name }}</td>
<td>Team pindonga</td>
<td>{{ score = DB::table('answers')->where('user_id', $user->id)->sum('score') }} </td> </tr>
#endforeach
works perfectly, but i want to get them by desc score (for now i solved the desc with js but want to get desc order from the eloquent query) and have something like this in the blade:
#foreach($users as $user)
<tr>
<th>{{ $user->id }}</th>
<td>{{ $user->name }}</td>
<td>Team pindonga</td>
<td>{{ $team->score }} </td> </tr>
#endforeach
I was thinking about solve it with accessors doing something like this:
public function getScoreAttribute($value)
{
return $this->groupBy('user_id')->where('user_id', '=', $value)->sum('score');
}
also i have a relationship belongsTo in the answers modle to users and a hasMany in the users to answers. But i'm not sure of how pass the results to blade. Tried in tinker:
>>> \App\Answer::find(1)->score;
=> "3400"
>>> \App\Answer::find(2)->score;
=> "3400"
>>> \App\Answer::find(5)->score;
=> "3400"
allways getting the sum of the id = 1. And with $user->score; in the blade foreach i got a '0'.

Using the relationship with Blade would be {{ $user->answers->sum('score') }}. No accessor needed.
For sorting I would use the sortByDesc collection method.
$users = User::get();
$users = $users->sortByDesc(function ($user) {
return $user->answers->sum('score');
});

I think this is what you're looking for. It takes a while to get used to Eloquent ORM :-)
foreach($users as $user){
print $user->answers->sum('score');
}
https://laravel.com/docs/5.6/queries#aggregates

Related

Laravel what relation to use in order to see data from another model and column

I have two models
Item:
protected $fillable = [
'code',
'name',
'uom'
];
public function recipe()
{
return $this->belongsTo('App\Recipe');
}
Recipe:
protected $fillable = [
'recipecode',
'itemcode',
'qty'
];
public function item()
{
return $this->hasMany('App\Item');
}
In migration I have:
public function up()
{
Schema::table('recipes', function (Blueprint $table) {
$table->foreign('itemcode')->references('code')->on('items');
});
}
In RecipesController I have:
public function index()
{
$recipes = Recipe::all()->sortBy('recipecode');
$items = Item::all();
return view ('recipe.index', compact('recipes', 'items'));
}
I want to see in the view recipecode, itemcode and ItemName, ItemQty etc.
#foreach($recipes as $recipe)
<tr>
<td>{{ $recipe->recipecode }}</td> // this works
<td>{{ $recipe->itemcode }}</td> // this works
<td>{{ $recipe->item->name }}</td> // this doesn't work
<td>{{ $recipe->item->uom }}</td> // this doesn't work
<td>{{ $recipe->item->qty }}</td> // this doesn't work
</tr>
#endforeach
What should I do in order to see 'name' and 'uom' columns from Items table? I think there is a problem with relations...
First, $recipe->item->name does not work as you expected because $recipe->item is a collection of App\Item, not an App\Item model instance. That's because you have set the relationship to be "hasMany"
Second, please read more about eager loading. What you're doing now are multiple queries. That's the N+1, for each recipe you are querying the database to get the items. You do not need to do that, as it's not performant at all. Use Recipe::with('item')->sortBy('recipecode')->get().
Now, keep in mind that $recipe->item is not a single item, but a collection of items that belong to that Recipe. You'll probably have to work a bit on your table to display multiple items for one Recipe.
Since you are using hasMany you must follow naming convention in naming function
public function items()
{
return $this->hasMany('App\Item');
}
In RecipesController you can eager load.
$recipes = Recipe::with('items')->sortBy('recipecode')->get();
Read documentation here: https://laravel.com/docs/master/eloquent-relationships
Also to show it in your blade
#foreach($recipes as $recipe)
<tr>
<td>{{ $recipe->recipecode }}</td> // this works
<td>{{ $recipe->itemcode }}</td> // this works
#foreach($recipe->items as $item)
<td>{{ $item->name }}</td>
#endforeach
</tr>
#endforeach
Other answers share why you can't directly use $recipe->item. However, I think there is a bigger mistake in database design.
The database that you have designed have a one to many relation. Like, one recipe has a lot of items. However, the reverse one that one item has only one recipe seems wrong. You can cook many recipes with one items. I.E. you can make chicken fry and chicken curry with chicken.
The database design should be something like this,
recipes: code, name, uom
items: code, name
item_recipe: item_code, recipe_code, qty

laravel Prevent foreach loop from repeating same results

Am doing a loop where a product can have many sizes so I display the name of the product and its 1st size and the name of the same product again with its 2nd size but anytime i try, it repeats both sizes on both product names. For example:
I want Product 1 = small, Product 1 = medium but in this case it displays...
Product 1 = small medium, Product 2= small medium. How do I solve this. Below is my blade;
#foreach ($productProductOptions as $productProductOption)
#foreach($productProductOption->product->options as $productOptions)
<tr>
<th>{{ $loop->iteration }}</th>
<td>{{ $productProductOption->product->title }}</td>
<td>{{ $productOptions->name }}</td>
</tr>
#endforeach
#endforeach
Below is my controller code which am passing to the view;
$productProductOptions = ProductProductOption::all();
This is the picture of the table am displaying the items inside...https://cdn.pbrd.co/images/H9UulFq.png
Distinct()
is the function you are seeking, which returns only distinct values from the database, I would suggest updating your controller to send only this specific detail as to save time and gain performance, however it only depends how you show your data and how big is it.
DB Query
$productProductOptions = DB::table('products')
->select('title', 'size')
->distinct()
->groupBy('title')
->orderBy('title', 'asc')
->get();
Eloquent
$productProductOptions = ProductProductOption::select('title', 'size')
->distinct()
->groupBy('title')
->orderBy('title', 'asc')
->get();

Laravel print role of user in table

I have a Laravel application. I'm using this package for roles and permissions. It works great.
I would now like to display a table of all users with their roles. Currently I'm solving it through the definition of the following in my UsersController.
public function listUsers()
{
$users_admin = Role::where('name', 'admin')->first()->users()->get();
$users_buyers = Role::where('name', 'buyer')->first()->users()->get();
$users_sellers = Role::where('name', 'seller')->first()->users()->get();
return view('admin.users.index', [
'users_admin' => $users_admin,
'users_buyers' => $users_buyers,
'users_sellers' => $users_sellers
]);
}
In the view, I then have 3 separate loops to display 3 tables (for admin users, buyers and sellers).
Needless to say I don't really like this approach and I would ideally have a single table displaying the users with their role.
So the idea is that I can write the following code (or similar) in the UsersController:
$users = User::all();
while my view contains a single foreach stating the following:
#foreach( $users_sellers as $user )
<tr>
<td align="center" class="hidden-xs">{{ $user->id }}</td>
<td>{{ $user->name }}</td>
<td>{{ $user->email }}</td>
<td>{{ $user->displayRole($user->id) }}</td>
</tr>
#endforeach
In the above pseudocode, I have defined a helper that retrieves the role based on the user-id. Problem is I can't figure out what to put in the helper function?
Note: if there are other (better) methods than a helper, that would work as well for me.
As far as I see this package adds a relation in your User model, so $user->roles should be a collection with the roles assigned to the current user. To not make a query for each user you should eager load the users with their roles, like this: $users = User::with('roles')->get();.
In your Controller, get the Role, if you have it in your table
public function listUsers()
{
$users_admin = Role::where('name', 'admin')->first()->users()->get();
$users_buyers = Role::where('name', 'buyer')->first()->users()->get();
$users_sellers = Role::where('name', 'seller')->first()->users()->get();
$role = Role::get();
return view('admin.users.index', [
'users_admin' => $users_admin,
'users_buyers' => $users_buyers,
'users_sellers' => $users_sellers
'role' => $role
]);
}
In your view, just print the Role's name like this
#foreach( $users_sellers as $user )
<tr>
<td align="center" class="hidden-xs">{{ $user->id }}</td>
<td>{{ $user->name }}</td>
<td>{{ $user->email }}</td>
<td>{{ $role['$user->id']['role_name']</td>
</tr>
#endforeach
Note :
<td>{{ $role['$user->id']['role_name']</td>
This line depends on how you send the $role from controller to your view. It's purpose is to return the name of the role's id's array
Hope this helps you.

Join 2 tables to a Pivot table LARAVEL 4.2

How can i show the classes only for student who is enrolled in a class and i have 2 tables and a pivot table?
Table 1: Home_Students : home_id , home_studid ....
Table 2: Hw_Classes :class_id , class_desc , class_date ....
Pivot table:Hw_StudentClasses :Stclass_id, Stclass_classid, Stclass_studid
So i made a model MySeminarClasses to communicate with the table Hw_StudentClasses and a Controller MySeminarClassesController
I made relations in the model of the other tables belongsToMany
public function Users(){
return $this->belongsToMany('User','home_id');
}
public function SeminarClass(){
return $this->belongsToMany('SeminarClass','class_id');
}
Also in the Controller i did this which im not so sure if its right but i did this from the instructions of the laravel 4.2 documentation
$myclasses = DB::table('Hw_StudentClasses')
->join('Hw_Classes','Hw_StudentClasses.Stclass_classid','=','Hw_Classes.Class_id')
->join('Home_Students','Hw_StudentClasses.Stclass_studid','=','Home_Students.home_studid')
->orderBy('class_date',strtotime('-4 month'))
->get();
Finally in the blade
<tbody>
#foreach($myclasses as $i=>$myclass)
<tr>
<td>{{ $i+1 }}</td>
**<td>{{ link_to_route('class.show',$myclass->Class_desc,$myclass->Class_id) }}</td>**
<td class="text-center">{{ date('j-n-Y G:i',strtotime($myclass->class_date)) }}</td>
<td class="text-center">{{ UserEnroll::where('Stclass_classid',$myclass->Class_id)->count() }}</td>
</tr>
#endforeach
</tbody>
After a 16 hours of struggle ......i aswered my own question ! This query with Auth::user helped me. Show the classes only for every user who is logged in
$id = Auth::user()->home_studid;
$date = date('m-d-Y', strtotime('-4 month'));
$seminars = Seminar::where('Package_Display', 1)
->orWhere('Package_Display', 2)
->orderBy('Package_Name', 'asc')
->get();
$myclasses = DB::table('Hw_StudentClasses')
->join('Hw_Classes','Hw_StudentClasses.Stclass_classid','=','Hw_Classes.Class_id')
->join('Home_Students','Hw_StudentClasses.Stclass_studid','=','Home_Students.home_studid')
->where('Home_Students.home_studid','=',$id)
->orderBy('class_date',strtotime('-4 month'))
->get();
HOPE THIS HELPS SOMEONE ! ^_^

Laravel Pagination with eloquent Queries

I am trying to paginate my template which has a table on it.
This is my function in the controller which has the eloquent queries.
public function orderbydate()
{
$order =DB::table('sales_flat_order_items as s')
->leftJoin('sales_flat_orders as w', 'w.entity_id','=','s.order_id')
->select(array(DB::Raw('sum(s.amount_refunded) as amount_refunded'),
DB::Raw('sum(s.row_total) as row_total'),
DB::Raw('sum(s.discount_amount) as discount_amount'),
DB::Raw('sum(s.tax_amount) as tax_amount'),
DB::Raw('sum(s.qty_ordered) as qty_ordered'),
DB::Raw('sum(w.subtotal) as subtotal'),
DB::Raw('sum(w.total_invoiced) as total_invoiced'),
DB::Raw('sum(w.shipping_amount) as shipping_amount')))
->where('qty_canceled','=','0')
->where('status','!=','canceled')
->get();
$orderbydate = DB::table('sales_flat_order_items as s')
->leftJoin('sales_flat_orders as w', 'w.entity_id','=','s.order_id')
->select(array(DB::Raw('sum(s.amount_refunded) as amount_refunded'),
DB::Raw('sum(s.row_total) as row_total'),
DB::Raw('sum(s.discount_amount) as discount_amount'),
DB::Raw('sum(s.tax_amount) as tax_amount'),
DB::Raw('sum(s.qty_ordered) as qty_ordered'),
DB::Raw('sum(w.subtotal) as subtotal'),
DB::Raw('DATE(w.created_at) days'),
DB::Raw('sum(w.total_invoiced) as total_invoiced'),
DB::Raw('sum(w.shipping_amount) as shipping_amount')))
->where('qty_canceled','=','0')
->where('status','!=','canceled')
->groupBy('days')
->orderBy('s.created_at')
->get();
return View::make('sales_flat_orders.orderbydate', compact('order','orderbydate'), ['orderbydate'=>Paginate(3)]);
}
I want to use pagination in my template. So i ma doing like this.
#foreach(array_chunk($orderbydate->getCollection()->all(), 3) as $row)
<div class = "row">
#foreach($row as $s)
<article class = "col-md-4">
<tbody>
<tr class="odd gradeX">
<td>{{date("d F, Y",strtotime($s->days))}}</td>
<td>{{ round($s->qty_ordered, 2) }}</td>
<td>{{round($s->subtotal,2)}}</td>
<td>{{round($s->tax_amount,2)}}</td>
<td>{{round($s->discount_amount,2)}}</td>
<td>{{round($s->row_total,2)}}</td>
<td>{{round($s->amount_refunded,2)}}</td>
<td>{{round($s->total_invoiced,2)}}</td>
<td>{{round($s->shipping_amount,2)}}</td>
</tr>
</article>
#endforeach
#endforeach
But thins does not work. Where am i going wrong?
Thanks in advance
If you want to use pagination instead of get method you need to use paginate method.
For example the working pagination should work like this:
Paginator::setCurrentPage($page);
Paginator::setBaseUrl($baseUrl);
Paginator::setPageName('page');
$users = User::paginate(15);
now in Blade template you can display users in foreach loop and add pagination links:
#foreach ($users as $user)
{{{ $user->name }}}
#endforeach
{{ $users->links() }}
You should definitely look also at Pagination documentation

Resources