Laravel join use to get one to many relations? - mysql

Is there any way to get relational data ( one to many relation records ) using joins in laravel without creating different records in collection :
Example:
$orders = DB::table('orders')
->join('users', 'users.id', '=', 'orders.users_id')
->join('order_items', 'order_items.order_id', '=', 'orders.id')
->select('users.*', 'order_items.*')
->get();
So here what's happening is that its creating 6 records if order has 6 items but i want something like single records in which it has array or collection where are order items are listed.
Output I want is generally of this order:
Collection {# ▼
#items: array:[▼
0 => {
+'id':1,
...,
+ 'items: [
//here i want all the records of relation order items
]
}
]
}
Is there any way to achieve this result without using with() or load() and just only with joins or raw queries?

You should try this:
$orders = DB::table('orders')
->select('users.*', 'order_items.*')
->leftJoin('users', 'users.id', '=', 'orders.users_id')
->leftJoin('order_items', 'order_items.order_id', '=', 'orders.id')
->get();

This answer is speculative, but perhaps you want to report a single row for each user, with a CSV list of order items:
$orders = DB::table('orders AS o')
->join('users AS u', 'u.id', '=', 'o.users_id')
->join('order_items AS oi', 'oi.order_id', '=', 'o.id')
->select('u.id', DB::raw('GROUP_CONCAT(oi.name) AS orders'))
->groupBy('u.id')
->get();

If there's no reason stopping you from using the Eloquent, is the best way for working with databases.
The Eloquent ORM included with Laravel provides a beautiful, simple
ActiveRecord implementation for working with your database
Here is the Documentation Eloquent: Getting Started
The model relationship made easy in laravel, you can get you want as following:
The User model relationships:
//user orders
public function orders() {
return $this->hasMany('App\Order');
}
The Order model relationships:
//order items
public function items() {
return $this->hasMany('App\OrderItem');
}
//order owner
public function user() {
return $this->belongsTo('App\User');
}
The OrderItem model relationships:
//order
public function order() {
return $this->belongsTo('App\Order');
}
Here is a quick example of how you may get the order's items
$orders = User::find($id)->orders
foreach($orders as $order) {
$orderItems = $order->items;
}
you can make the collection the way you prefer, but strongly recommend using Eloquent Resources, since most apps these days expecting JSON responses.

Related

Add result of subquery to Eloquent query in Laravel 9

In Laravel 9 I am trying to add the result of a subquery to a query(for lack of better wording) and I am stuck. More concretely, I am trying to load all products and at the same time add information about whether the current user has bought that product.
Why do I want to do this?
I am currently loading all products, then loading all bought products, then comparing the 2 to determine if the user has bought a product, but that means extra queries which I would like to avoid. Pretend for the sake of this question that pagination doesn't exist(because when paginating the impact of those multiple queries is far diminished).
There is a many to many relationship between the 2 tables users and products, so these relationships are defined on the models:
public function products()
{
return $this->belongsToMany(Product::class);
}
and
public function users()
{
return $this->belongsToMany(User::class);
}
What I have tried so far:
I created a model for the join table and tried to use selectRaw to add the extra 'column' I want. This throws a SQL syntax error and I couldn't fix it.
$products = Product::query()
->select('id', 'name')
->selectRaw("ProductUser::where('user_id',$user->id)->where('product_id','products.id')->exists() as is_bought_by_auth_user")
->get();
I tried to use addSelect but that also didn't work.
$products = Product::query()
->select('id', 'name')
->addSelect(['is_bought_by_auth_user' => ProductUser::select('product_id')->where('user_id',$user?->id)->where('product_id','product.id')->first()])
->get();
I don't even need a select, I actually just need ProductUser::where('user_id',$user?->id)->where('product_id','product.id')->exists() but I don't know a method like addSelect for that.
The ProductUser table is defined fine btw, tried ProductUser::where('user_id',$user?->id)->where('product_id','product.id')->exists() with hardcoded product id and that worked as expected.
I tried to create a method on the product model hasBeenBoughtByAuthUser in which I wanted to check if Auth::user() bought the product but Auth wasn't recognized for some reason(and I thought it's not really nice to use Auth in the model anyway so didn't dig super deep with this approach).
$products = Product::query()
->select('id', 'name')
->addSelect(\DB::raw("(EXISTS (SELECT * FROM product_user WHERE product_users.product_id = product.id AND product_users.user_id = " . $user->id . ")) as is_bought_by_auth_user"))
->simplePaginate(40);
For all attempts $user=$request->user().
I don't know if I am missing something easy here but any hints in the right direction would be appreciated(would prefer not to use https://laravel.com/docs/9.x/eloquent-resources but if there is no other option I will try that as well).
Thanks for reading!
This should do,
$id = auth()->user()->id;
$products = Product::select(
'id',
'name',
DB::raw(
'(CASE WHEN EXISTS (
SELECT 1
FROM product_users
WHERE product_users.product_id = products.id
AND product_users.user_id = '.$id.'
) THEN "yes" ELSE "no" END) AS purchased'
)
);
return $products->paginate(10);
the collection will have purchased data which either have yes or no value
EDIT
If you want eloquent way you can try using withExists or withCount
i.e.
withExists the purchased field will have boolean value
$products = Product::select('id', 'name')->withExists(['users as purchased' => function($query) {
$query->where('user_id', auth()->user()->id);
}]);
withCount the purchased field will have count of found relationship rows
$products = Product::select('id', 'name')->withCount(['users as purchased' => function($query) {
$query->where('user_id', auth()->user()->id);
}]);

Optimize Laravel Eloquent?

I've got a working query on my laravel project. Is there a way to optimize or shorten this code?
$transaction = DB::connection('mysql')->table('pwn_transaction')
->join('pwn_transaction_has_details', 'pwn_transaction.ticket_number', '=', 'pwn_transaction_has_details.pwn_transaction_ticket_number')
->join('pwn_transaction_details', 'pwn_transaction_has_details.pwn_transaction_details_id', '=', 'pwn_transaction_details.id')
->join('pwn_transaction_has_transaction_items', 'pwn_transaction.ticket_number', '=', 'pwn_transaction_has_transaction_items.pwn_transaction_ticket_number')
->join('pwn_branch_has_transaction', 'pwn_transaction.ticket_number', '=', 'pwn_branch_has_transaction.pwn_transaction_ticket_number')
->join('pwn_transaction_has_customers', 'pwn_transaction.ticket_number', '=', 'pwn_transaction_has_customers.pwn_transaction_ticket_number')
->join('pwn_customers', 'pwn_transaction_has_customers.pwn_customers_id', '=', 'pwn_customers.id')
->groupBy('ticket_number')
->get();
First, you need to read Laravel eloquent
Eg.
Simple code with Transaction model in your controller
$transaction = Transaction::get();
You can use Eloquent Relationship in your model
Transaction Model
public function items()
{
return $this->hasOne(TransactionItem::class); //based on your model and relationship
}
Retrieve with relationship Constraining Eager Loads
$transaction = Transaction::with('items)->get();
Laravel use mysql as default connection or based on your config/database.php file
If you want to use multiple databases you can use something like that
Define below line in your Transaction Model
protected $connection = 'your-connection-name';
Based on your comment
Make sure you define proper relationship in Transaction model
Transaction::with('hasDetails', 'details', 'branch', 'hasCustomers', 'customer')
->groupBy('ticket_number')
->where('ticket_number', $request->pawnTicket) //assume ticket_number column is inside the transactions table
->get();

Count where query in Laravel

I am having a issue here that I am not solving.
I am trying to make a query in Laravel where I need to count how many offers does a business has, to show Business id, Total Offers for a Business and Business Name.
$business_offers = DB::table('offers')
->join('businesses', 'offers.id_business', '=', 'businesses.id')
->select(['businesses.id', 'businesses.name', DB::raw('count(offers.id) as total_offers')])
->orderby('total_offers', 'DESC')
->get();
but it is not working in this way. I have also done the query in mysql but can't integrate it in Laravel.
SELECT bu.id,(SELECT count(of.id) from offers of where of.id_business = bu.id )
as total_offers, bu.`name` from businesses bu ORDER BY total_offers DESC
thanks in advance
Reading it literally something like the following might work:
$business_offers = DB::table('businesses bu')
->select(['bu.id', DB::raw("(SELECT count(of.id) from offers of where of.id_business = bu.id )") ,'bu.name'])
->orderBy('total_offers', 'DESC')
->get();
If you want it a bit more efficient (maybe):
$business_offers = DB::table('offers')
->join('businesses', 'offers.id_business', '=', 'businesses.id')
->select(['businesses.id', DB::raw("COUNT(1) as total_offers"),'businesses.name'])
->groupBy("businesses.id","businesses.name")
->orderBy('total_offers', 'DESC')
->get();
If you have models and relations set up it should be as easy as:
$businesses = Business::withCount(['offers'])->get;
foreach ($businesses as $business) {
echo $business->offers_count;
}
From the Laravel documentation:
If you want to count the number of results from a relationship without
actually loading them you may use the withCount method, which will
place a {relation}_count column on your resulting models.
All you need in your Business model is:
class Business extends Model
{
/**
* Get all of the offers for the business.
*/
public function offers()
{
return $this->hasMany('App\Offer');
}
}
and in the Offer model:
class Offer extends Model
{
/**
* Get the business for the current offer.
*/
public function business()
{
return $this->belongsTo('App\Business', 'business_id');
}
}

How add to collection only models with no record joined - Eloquent

In my model Questions I have simple relation to Standpoint
public function standpoints_byrel()
{
// return $this->hasMany('App\Models\Standpoint');
return $this->hasMany('App\Models\Standpoint', 'question_id');
}
Now,
I have yet another model Userattitude (tableuser_attitudes`) which allow users to upvote and downvote Standpoints.
I am able to list Standpoints, which were voted by a given user:
$user_attitudes = Userattitude::join('entitystandpoints', function ($q) use($questionid,$user) {
$q->where('user_attitudes.item_type', '=', 'entitystandpoint');
$q->on('user_attitudes.item_id', '=', 'entitystandpoints.id');
$q->where('entitystandpoints.question_id', '=', $questionid);
$q->where('user_attitudes.creator_id','=', $user);
})
->select('user_attitudes.*')
->get();
TO DO
Now I try to list all standpoints, which were NOT voted by the given user.
I have no idea how to do it using Eloquent.
Any help appreciated.
edit
condition to meet:
if an user votes up or down, a new model Userattitude is created. Therefore Standpoint models not down- or upvoted have nothing to join. still, in the Userattitude there are two fields for upvoting : 'attitude' and 'importance'. often one of them is null
Try with a left join where the left parameter of the join is null.
Something like this (but please check the syntax out, I'm not an Eloquent expert):
$user_attitudes = Userattitude::leftJoin('entitystandpoints', function ($q) use($questionid,$user) {
$q->where('user_attitudes.item_type', '=', 'entitystandpoint');
$q->on('user_attitudes.item_id', '=', 'entitystandpoints.id');
$q->where('entitystandpoints.question_id', '=', $questionid);
$q->where('user_attitudes.creator_id','=', $user);
})
->whereNull('entitystandpoints.id')
->select('user_attitudes.*')
->get();
Let me know.

Retrieve data from one table and order by related table column using Laravel eloquent

I have two models Category and Transaction the table structures are like this
Categories:
id,category_name,..
and
Transactions:
id,category_id,amount..
The relation is
Category hasMany transactions
public function transactions()
{
return $this->hasMany('App\Transaction');
}
Transactions blongsTo Category
public function category()
{
return $this->belongsTo('App\Category', 'category_id');
}
I want to retrieve all data of transaction table which are sorted by category name.
Most importantly I want to get it using the eloquent method.
I have tried eager load which I think doesn't work on the belongsTo relationship.
Here is the code I have used for the eager load.
$transactions = Transaction::with(['category' => function ($query) {
$query->orderBy('category_name', 'asc');
}])->paginate(10);
So far I can achieve this by writing a query like below, but I'd like to use the eloquent method.
$transactions = Transaction::select(DB::raw('transactions.*'))
->leftJoin(DB::raw('(select id,category_name from categories) as categories'), 'categories.id', '=', 'transactions.category_id')
->orderBy('category_name', 'asc')
->paginate(10);
It'd be nice if someone can help me with this. Thank You.
Note: I am using Laravel 5.1
You have to provide the method name that defines the relationship, in your case this is category.
$transactions = Transaction::all()->with('category')->group_by('category.name')->get();
$transactions = Transaction::with('categories')->group_by('category.name')->get();
$cs = Course::where(['courses.active' => 1])
->whereHas('course_dates', function ($join) use ($now) {
$join->where('course_dates.start_date_time', '>', $now);
$join->orderBy('course_dates.start_date_time', 'asc');
})
->whereHas('category', function ($join) use ($cat_slug) {
$join->where('categories.url_slug', '=', $cat_slug);
})
->whereHas('language', function ($join) use ($cat_slug) {
$join->where('languages.string_id', '=', strtoupper(App::getLocale()));
})
->with(['course_dates' => function($q){
$q->orderBy('course_dates.start_date_time', 'desc');
}])
->join('course_dates' ,'course_dates.course_id', '=', 'courses.id')
->orderby('course_dates.start_date_time')
->limit(7)
->get();
To return Eloquent models ordered by related model (hasMany) column, I had to join the tables and then orderBy, still get the models, but correctly ordered by course_date.start_date_time.
Laravel 5.7, I don't think there is a cleaner solution (at least after few hours of tinkering and searching the web).