I can't seem to get the syntax right for this query.
Basically, I have a product that is listed, where an offer is made by a user:
So in Offer.php
public function user()
{
return $this->belongsTo('App\User', 'buyer_id');
}
public function product(){
return $this->belongsTo('App\Product');
}
And in User.php
public function offers()
{
return $this->hasMany('App\Offer', 'buyer_id');
}
Then in Product.php
public function offer()
{
return $this->hasMany('App\Offer');
}
I need to group offers by the user that has made them, and then group that again by the category of the product the offer is against.
Does anyone have ideas where I can start to group these correctly?
Try with Query Builder. Like this ( I don't know your tables`s structure):
$offers = DB::table('offers')
->joinLeft('users','users.id','=','offers.buyer_id')
->joinLeft('products','products.id','=','offers.product_id')
->groupBY('users.id','products.category_id')
->get();
Related
I'm trying to create offers and assign them to parent categories, to be more specific i have an Offer model and inside the offer model i have this many to many relationship
public function category() {
return $this->belongsToMany(Category::class);
}
I want the above function to return ONLY the categories which have NULL parent_category which mean they are the parent categories. Is it possible with the above code?
Without knowing the entire scope of your project, I'd suggest one of the following: either change the name of the relation (A) or keep the relation as is and query it when you need it (B).
Option A -
public function childCategory() {
return $this->belongsToMany(Category::class)->whereNull('parent_category');
}
Option B -
public function category() {
return $this->belongsToMany(Category::class);
}
$offer = Offer::with('category')
->whereHas('category' function ($query) {
$query->whereNull('parent_category');
});
public function category() {
return $this->belongsToMany(Category::class)->where('parent_category', null);
}
Laravel Eloquent. I try to intersect two intermediate table. Is there any query more efficient than this ?
Unit Model :
public function products()
{
return $this->belongsToMany('App\Product');
}
public function users()
{
return $this->belongsToMany('App\User');
}
Product Model :
public function units()
{
return $this->belongsToMany('App\Unit');
}
public function users()
{
return $this->belongsToMany('App\User');
}
User Model :
public function units()
{
return $this->belongsToMany('App\Unit');
}
public function products()
{
return $this->belongsToMany('App\Product');
}
Query Eloquent :
Product::get()
->filter(function ($product) {
return $product->units
->pluck('id')
->intersect(auth()->user()->units->pluck('id'))
->isNotEmpty();
})
I try to retrieve all product where product unit is equal user login unit
UPDATE
I think code below is more clean
Product::whereHas('units', function (Builder $query) {
$query->where([
'units.id' => auth()->user()->units->pluck('id'),
]);
})->get();
I think your solutions looks optimal! It looks like a way to compare attributes from two pivot tables in the way
Product::with('users')
->with('units')
->compareAttributesFromPivotTables('attribute_a','attribute_b')
->get();
isn't yet implemented.
I'm currently working on a laravel project, but I'm kind of stuck finding the right eloquent relations.
My tables and the connections (should) look like this:
Project Relations
My model relations look like this:
User
public function team()
{
return $this->hasMany(Team::class, 'user_id');
}
public function evaluation()
{
return $this->hasMany(Evaluation::class, 'user_id');
}
Team
public function user()
{
return $this->belongsTo(User::class);
}
public function survey()
{
return $this->hasMany(Survey::class, 'team_id');
}
Evaluation
public function user()
{
return $this->belongsTo(User::class);
}
public function survey()
{
return $this->hasMany(Survey::class, 'evaluation_id');
}
Survey
public function team()
{
return $this->belongsTo(Team::class);
}
public function evaluation()
{
return $this->belongsTo(Evaluation::class);
}
public function surveyresponse()
{
return $this->hasMany(SurveyResponse::class, 'survey_id');
}
SurveyResponse
public function survey()
{
return $this->belongsTo(Survey::class);
}
public function testquestion()
{
return $this->belongsTo('App\TestQuestion');
}
Is this the way to go? Do I need a "Has Many Through" relation here? Or a "Polymorphic Relationship"?
Seems correct to me, i just didnt see the TesteQuestion model (your last relation).
Answering your question:
The HasManyThrough relation is just a shortcut for accessing distant relations via an intermediate relation, in your case: Users has many evaluations that has many surveys. With this relationship you could get all surveys from a user.
Your relation would look like this:
/**
* Get all of the surveys for the user.
*/
public function surveys()
{
return $this->hasManyThrough('App\Survey', 'App\Evaluation');
}
You can access this relation like this:
$user->surveys();
But you can achieve the same (without using the HasManyThrough) by doing:
$user->evaluations()->surveys();
Beware that this will return the evaluations too, not just the surveys and it requires more processing.
So i recommend you doing the HasManyThrough relationship if you pretend to access the surveys a lot.
I have two models, User and Post
User Model:
public function posts()
{
return $this->hasMany('App\Post');
}
Post Model:
public function user()
{
return $this->belongsTo('App\User');
}
In my controller I have a public function which has:
$users = User::orderBy('is_ban', 'desc')->paginate(10);
$posts = Post::orderBy('created_at', 'desc')->paginate(10);
Which is working as expected.
I also have one column in users table `is_ban' It's of boolean type.
I am looking for a query which will return the following:
Only get post which has been made by the user which has is_ban=false
perhaps i haven't understood you, but i hope it will help. You can add it to your Post model
public function getBannedUsersPosts()
{
return self::whereIn('user_id', User::where('is_ban', 0)->pluck('id'))->get();
}
I have 3 models named "User","Order","Orders_products". The relations are following:
User has many Order
Order has many Orders_products
Here are the model's code
User Model
public function order(){
return $this->hasMany('App\Order');
}
Order Model
public function order_product(){
return $this-> hasMany('App\Orders_product');
}
public function user(){
return $this-> belongsTo('App\User');
}
Orders_products Model
public function order(){
return $this->belongsTo('App\Order');
}
An user could have multiple orders and all orders could have multiple ordered products. How can i get a particular users all orders along with the ordered products.
User model
public function order()
{
return $this->hasMany(Order::class);
}
Order Model
public function user()
{
return $this->hasOne(User::class);
}
public function orders_products()
{
return $this->belongsToMany(Orders_products::class);
}
Orders_products
public function order()
{
return $this->belongsToMany(Order::class);
}
This should work, $userid is how ever you specify the user id in your app.
User::with('order_product.order')->where('id', $usersid)->get();
$Order = Order::with('order_product')->where(['user_id'=>$id])->get();
where $id is the user_id