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();
Related
I have orders table, and for each user weekly payments table.
I want to show user that he wil take total payment count on depending week.
I have created the query but on blade side, when i try to access other tables via eloquent it gives does not exits in this collection.
If i dont use group by function it works. But with group by function it doent
$earnings = user()->orders()->where('payment_status',0)-
>where('status',1)->get()->groupBy(function($date) {
return Carbon::parse($date->created_at)->format('W');
});
In blade side my codes like following;
#foreach($earnings as $earning)
<tr>
<td>{{$earning->user->name}}</td>
<td>{{$earning->order->commission}}</td>
<td>{{$earning->user->bank}}</td>
<td>{{$earning->user->iban}}</td>
<td>{{$earning->user->phone}}</td>
#endforeach
get() should be last:
$earnings = user()->orders()
->where('payment_status',0)
->where('status',1)
->groupBy('created_at')
->get();
How about using DATE_FORMAT function:
$earnings = user()->orders()
->where('payment_status',0)
->where('status',1)
->groupBy(DB::raw("DATE_FORMAT(created_at, '%W')"))
->get();
Assuming your table is orders, you can do like this:
$earnings = user()->orders()->select('orders.*', DB::raw("DATE_FORMAT(created_at, '%W') as week"))
->where('payment_status',0)
->where('status',1)
->get()->groupBy('week');
Laravel Collection groupBy() function returns a collection of grouped collections.
$earningGroups = user()->orders()
->where('payment_status',0)
->where('status',1)
->get()
->groupBy(function($earning) {
return Carbon::parse($earning->created_at)->format('W');
});
Now this is a collection of earning groups, not earnings.
#foreach($earningGroups as $earcningGroupName => $earcningGroup)
// So here you have a group or earnings
// I don't know why did you want to group.
// For an example I will print the group name as a table row here.
// But do whatever you want.
<tr>
<td>{{ $earningGroupName }}</td>
</tr>
// So if you want to loop through earnings, you have to loop again.
#foreach($earningGroup as $earning)
<tr>
<td>{{$earning->user->name}}</td>
<td>{{$earning->order->commission}}</td>
<td>{{$earning->user->bank}}</td>
<td>{{$earning->user->iban}}</td>
<td>{{$earning->user->phone}}</td>
</tr>
#endforeach
#endforeach
I currently have a form/view that allows a User to save an Order. This also updates a pivot table with the respective Products and qtys. What I am trying to do, is after this information is inputted and saved into the database, I want to send the user to a checkout page that shows all the information they inputted for that Order.
Relationships
User hasMany Order
Order belongsToMany Product
Product belongsToMany Order
checkout.blade.php
#foreach ($orders as $order)
#foreach ($order->products as $product)
<tr>
<td>{{ $product->name }}</td>
<td>{{ $product->pivot->qty }}</td>
</tr>
#endforeach
#endforeach
OrderController.php
public function checkout($id)
{
$user = Auth::user();
$user->load("orders.products"); //eager load pivot table
$orders = $user->orders->where('order_id', $id);
return view('orders.checkout', compact('orders', 'user'));
}
I know I am calling the order incorrectly, because if I change
$orders = $user->orders->where('order_id', $id);
to
$orders = $user->orders;
It displays correctly, except of course, it populates with every Order's detail.
Or is there some elegant way for me to pass the data from the checkout function without this additional query? (I understand about moving data in Sessions, but I am working with a pivot table, and that complicates things.
If on the checkout page, you want to use just the latest order that the user has made, then you can just load that order using route model binding
Route::get('checkout/{order}', 'OrdersController#checkout');
Then in your controller:
public function checkout(Order $order)
so from here, you can pass this order to the view, and list all the products from this order, and also in your Order model you should have a reference to the user that this order belongs to:
public function user()
{
return $this->belongsTo(User::class);
}
Accessing columns from the pivot table will be:
#foreach($order->products as $product)
<div> {{ $product->pivot->column }} </div>
#endforeach
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
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 ! ^_^
I'm having a bit of a struggle displaying results from a query in a blade template. The basics: I have two tables; countries and issuers. The Country model has a hasMany relation to Issuer, and vice versa. Both relations are properly defined in the models. I am trying to display a list of issuers that contains the name (nation) of the country as well. My query, in the IssuersController, is as follows:
$issuers = ISSUER::join('countries', 'issuers.country_id', '=', 'countries.id')
->select('issuers.*', 'countries.nation')
->orderBy('nation', 'asc')
->orderBy('author', 'asc')
->get();
This query works and dd(); shows it returning an array for each issuer that includes all of the issuer data as well as the corresponding country name as nation. Perfect. However, when I attempt to display this in my view I run into a wall. My first attempt was to just use
#foreach($issuers as $issuer)
<tr>
<td>{{ $issuer->id }}</td>
<td>{{ $issuer->nation }}</td>
<td>{{ $issuers->author }}</td>
This returns an undefined variable on $nation. I'm not sure why this happens as I'm not attempting to access the relationship. I'm simply trying to access the results of an array that was returned from the query. The relationship should not be relevant at that point. Anyway, attempting to use the relationship I try
#foreach($issuers as $issuer)
<tr>
<td>{{ $issuer->id }}</td>
#foreach($issuer->nation as $nation)
<td>{{ $issuer->nation }}</td>
#endforeach
<td>{{ $issuers->author }}</td>
Which returns and invalid argument supplied in foreach() error. Next I attempt to use the method...
#foreach($issuers as $issuer)
<tr>
<td>{{ $issuer->id }}</td>
#foreach($issuer->Country() as $nation)
<td>{{ $issuer->nation }}</td>
#endforeach
<td>{{ $issuers->author }}</td>
This throws no error but also returns nothing. The colum is simply skipped and everything else that is echoed gets shifted one column to the left.
I'm sort of lost here and I think it's because my brain is stubbornly holding on to the idea that I'm accessing elements of a query result rather than elements of a relationship, so I can't quite figure out why I need a separate loop for that one column, or how that loop should work. Any help would be appreciated.
EDIT: Changed the last part because I typed it wrong.
Answering this for anyone that stumbles upon it later. I was able to access the value of nation directly from the query result without the model relation by changing the query from
->select('issuers.*', 'countries.nation')
to
->select('issuers.*', 'countries.nation as nation')
From their I simply accessed the value as I would everything else:
<td>{{ $issuer->id }}</td>
<td>{{ $issuer->nation }}</td>
<td>{{ $issuers->author }}</td>
Change this
$issuers = ISSUER::join('countries', 'issuers.country_id', '=', 'countries.id')
->select('issuers.*', 'countries.nation')
->orderBy('nation', 'asc')
->orderBy('author', 'asc')
->get();
TO
$issuers = ISSUER::
->join('countries', 'issuers.country_id', '=', 'countries.id')
->derBy('nation', 'asc')
->orderBy('author', 'asc')
->get();
Get all the content in your Blade like this
{{ $issuers ['type_the_name_of_the_column_you_want_to_display'] }}