So I have a select that the options are froma laravel Foreach
#foreach($courseMonth as $cm)->groupBy('description');
<optgroup label="{{ nameMonth($cm->month) }}">
#foreach($days as $date)
#if(date('m', strtotime($date['date'])) == $cm->month)
<option value="{{ date('d/m/Y', strtotime($date['date'])) ." - ". $date['hours'] }}">{{ $date['description'] . ' - ' . date('d/m/Y', strtotime($date['date'])) . ' - ' . $date['hours'] . ' ('. $date['total'] . 'h)'}}</option>
#endif
#endforeach
</optgroup>
<?php $i++;?>
#endforeach
This is the options. And I want if $date['description'] is the same in two options, to combine both options. More detailed, if three options have their description as "Work", to combine all of the options.
I've tried a groupby in front of the foreach
#foreach($courseMonth as $cm)->groupBy('description');
But this didn't do anything. The table for the options only has one row (ex: One row has only one description but it can have multiple day options in the select) so I need to group those only when there is the same description in two options.
Table structure:
-----------------------------------------------
| id | description | date | hours | total |
|---------------------------------------------|
| 4 | work |2019-23-4 | 14h | 6h |
| 5 | work |2019-23-5 | 13h | 5h |
| 6 | school |2019-23-5 | 13h | 5h |
-----------------------------------------------
So the first two would appear in the same option and the last one as the second option of the select.
Related
I had a problem on my mail::table in Laravel Markdown Mailables. The table is not rendering. I have also no indents on it.
emails/inquiry.blade.php
#component('mail::message')
#New Inquiry
We have received a new inquiry
#component('mail::table')
| Laravel | Table | Example |
| ------------- |:-------------:| --------:|
| Col 2 is | Centered | $10 |
| Col 3 is | Right-Aligned | $20 |
#endcomponent
Thanks
#endcomponent
Mailtrap output
I want to loop through with all main categories. This is to use where-condition to check which categories have the id on main category and output it.
The DB look like this:
id | name | parent_ids
10 | Cat1 |
12 | Cat3 |
17 | Cat2 | 10,12
25 | Cat4 | 12
#foreach($lagerMainCategories-\>where('parent_ids', $lagerMainCategory-\>id) AS $lagerSubCategories)
<li>
{{$lagerSubCategories-\>name}} ({{$lagerSubCategories-\>id}})</li>
#include('Skote.layouts.lagerCategory.lagerSubCategory', \['lagerMainCategory' =\> $lagerSubCategories\])
#endforeach
I get out
Cat1
Cat3
Cat4
I want to
Cat1
Cat2
Cat3
Cat2
Cat4
Ok, thanks for Help. I have found a solution.
#foreach($lagerMainCategories->where('parent_ids', '!=', null) AS $lagerSubCategories)
#if(in_array($lagerMainCategory->id, explode(',', $lagerSubCategories->parent_ids)))
<li>{{$lagerSubCategories->name}} ({{$lagerSubCategories->id}}) </li>
#include('Skote.layouts.lagerCategory.lagerSubCategory', ['lagerMainCategory' => $lagerSubCategories])
#endif
#endforeach
I am trying to figure out how to make this query work. These two tables do not have a direct relation (i.e. hasOne, hasMany, etc). I am looking to only get back records from client_vendor_relationship that do NOT have a collection_opt_in. Since I don't have a way of doing ->whereDoesntHave(), I am not sure how to get this data back.
client_vendor_relationship
| id | client_id | vendor_id | active |
|----|-----------|-----------|--------|
| 1 | 23484 | 1872 | 1 |
| 2 | 5643 | 345 | 1 |
| 3 | 431 | 4443 | 1 |
collection_opt_in
| id | client_id | vendor_id | year |
|----|-----------|-----------|------|
| 1 | 23484 | 23484 | 2020 |
| 2 | 23484 | 23484 | 2019 |
| 3 | 431 | 4443 | 2019 |
Current Query
$relationships = ClietVendorRelationship::where('active', 1)
->leftJoin('collection_opt_in', function($join){
$join->on('client_vendor_relationship.client_id', '=', 'collection_opt_in.client_id')
->on('client_vendor_relationship.vendor_id', '=', 'collection_opt_in.vendor_id');
})
->where() // Not sure what to put here to ONLY get rows back that the left join didnt find entries for
->groupBy('client_vendor_relationship.id')
->get();
The end goal of this above query would to be to only get the row back from client_vendor_relationship with the id of 2. I need to do this in Eloquent. I know I could easily just do a collection filter but the front end table I am using requires an eloquent query to be returned.
why using left join? while there is another straightforward ways?
you can use 'whereNotExists':
$relationships = ClietVendorRelationship::where('active', 1)
->whereNotExists(function ( $query){
$query->select('collection_opt_in.id')->from('collection_opt_in')->
whereColumn('collection_opt_in.client_id', '=', 'client_vendor_relationship.client_id')->
whereColumn('collection_opt_in.vendor_id', '=', 'client_vendor_relationship.vendor_id');
})
->groupBy('client_vendor_relationship.id')
->get();
also i must say i don't recommend using group by without aggregation
I want to show product sizes related to product I already have made a relation b/w tables into database i just only want to know that when i click the specific product the size related to that product will be shown in the table.inside function how to make joining b/w three tables please make the join table using DB class because it will be easy to understand for me thanks.
Does Anyone have an idea ?
please check product sizes image
https://i.stack.imgur.com/Yt6Jk.png
Database
product_sizes table
----------------------------------------------------
id | size_name | Body_length | Body_width |
----------------------------------------------------
1 | M | 2.3 | 3.4
2 | L | .5 | 4.4
3 | s | 2.5 | 3.2
4 | xs | 3.5 | 2.4
products table
----------------------------------------
id | product_name | Description |
----------------------------------------
1 Ultraclub 1 | Ultraclub 300 |
2 Ultraclub 2 | Ultraclub 200 |
3 Ultraclub 3 | Ultraclub 500 |
4 Ultraclub 4 | Ultraclub 600 |
5 Ultraclub 5 | Ultraclub 400 |
available_product_sizes table
----------------------------------------
id | product_id | product_size_id
----------------------------------------
1 | 2 | 1
2 | 3 | 4
3 | 4 | 3
4 | 1 | 2
5 | 2 | 3
Controller
public function single_product($product_slug){
$Sizes=DB::table('product_sizes')->get();
$single_product=DB::table('products')->where('product_slug',$product_slug)->get();
return view('front_end/single_product',compact('single_product','Sizes'));
}
html view
<div class="product-tabs__pane product-tabs__pane--active" id="chart">
<table class="table table-bordered text-center">
<tr style="background-color:#3366cc;color:white">
<td><b>Sizes</b>
</td>
<td><b>Body length</b>
</td>
<td><b>Body width</b>
</td>
<td><b>Sleeve length</b>
</td>
</tr>
#foreach($Sizes as $size)
<tr>
<td><b>{{$size->sizes_name}}</b>
</td>
<td>{{$size->Body_length}}</td>
<td>{{$size->Body_width}}</td>
<td>{{$size->Sleeve_length}}</td>
</tr>
#endforeach
</table>
</div>
I assume you're already has/desfined many-to-many relationship between products and product_sizes tables
//App\Product.php
public function sizes()
{
return $this->belongsToMany('App\ProductSize', 'available_product_sizes', 'product_id', 'product_size_id');
}
//App\ProductSize.php
public function products()
{
return $this->belongsToMany('App\Product', 'available_product_sizes', 'product_size_id', 'product_id');
}
In order to get available product sizes you can use :
$sizes = App\Product::find(1)->sizes()->orderBy('size_name')->get();
Controller fix
public function single_product($product_slug) {
$single_product = Product::with('sizes')->where('product_slug',$product_slug)->first();
return view('front_end/single_product',compact('single_product'));
}
View Fix
//...
#foreach($single_product->sizes as $size)
<tr>
<td>
<b>{{$size->sizes_name}}</b>
</td>
<td>{{$size->Body_length}}</td>
<td>{{$size->Body_width}}</td>
<td>{{$size->Sleeve_length}}</td>
</tr>
#endforeach
//...
Answer to the question without using eloquent
use Illuminate\Support\Facades\DB; //include this
$products = DB::table('products as p')
->join('available_product_sizes as aps','aps.product_id','p.id')
->join('product_sizes ps','ps.id','aps.product_size_id')
->where('product_slug',$product_slug)->get();
But it is highly recommended to use eloquent relationships.
I'm doing a Laravel query to query a list of elements that his relationship is grater than 0.
The table concerts:
| ID | NAME |
|----|-----------|
| 1 | Concert A |
| 2 | Concert B |
| 3 | Concert C |
And the Position table.
| ID | concert_id | user_id | Content |
|----|----------------|------------|----------|
| 1 | 1 | 1 | xxx |
| 2 | 1 | 2 | yyy |
| 3 | 3 | 1 | zzz |
| 4 | 3 | 2 | www |
| 5 | 1 | 3 | xyx |
| 6 | 3 | 3 | rer |
The query that I need to do is, get the concerts where their position has in the content a value like $some_query$.
The code I've is the following:
$result = $this->model->whereHas('positions')
->with(['positions' => function ($query) {
$query->where('content', 'like', "%{$content}%");
}])->get();
But as far as I can tell, this will bring all the concerts, and also this is going to bring only the positions that has the desired content.
So I've two problems, the first one is I need to get the concerts that his queried positions are greather than 0.
And the second one is that also I need to bring all the positions, not only the queried ones.
Basically the query is just a way to know which concerts I need to bring.
Is there possible to achieve this on a single query?
You'll have to move your query to the whereHas.
If I understood well, this is what you want:
$result = $this->model
// Take the Concerts that has positions with a similar content
->whereHas('positions', function ($query) use ($content) {
$query->where('content', 'like', "%{$content}%");
})
// Take (all) the Positions of these concerts
->with('positions')
->get();