Need help in registration system - laravel

In laravel9 I have created following tables
users (its laravel auth ui)
tournamnets
matches (in matches i have column "name")
registrations (in registration I have relation with users and one column status_id Bydefault its 0 and relation with matches is hasmany and belongsTo)
Note: match id is linked with hasmany and belongsto relation
Note: registrations and tournamnets have Pivot table which is reg_tours
Column names are tournaments_id and registrations_id.
Now I need with user registered for the specific tournament and registrations status is 0 the show {you are registered} in that specific tournament and if with user registered for the specific tournament and that registrations status is any match id which is linked with belongs to relation then show that match name
Tournament Controller
public function singleTournament($id) {
$game = Game::all();
$data = Tournament::find($id);
if ($data->teamsize == 1) {
$data->teamsize = "Solo";
} elseif ($data->teamsize == 2) {
$data->teamsize = "Duo";
} elseif ($data->teamsize == 4) {
$data->teamsize = "Squad";
}
return view('site.tournament',compact('data','game'));
}
Thank You!
I need to show status for specific tournament to specific user

you can add initialize for the status variable before

Related

Get hasMany relationship through 4 levels of users childs

I have a users table that contains:
id
parent_id //=> refers to the users table
role // admin, salesman, agent, agent_account
and cards table :
id
user_id //refers to a user with "agent_account" role
user with admin role has many childs of users with salesman role
user with salesman role has many childs of users with agent role
user with agent role has many childs of users with agent_account role
user with agent_account role has many cards
I want to define the relationship of policies in the user form so that the user can access all of their child policies
public function cards()
{
switch ($this->role) {
case 'admin':
# return all cards
break;
case 'agent':
# foreach $this->child as $ch
# $allCards += $ch->cards;
#return $allCards;
break;
case 'agent_account':
return $this->hasMany(Card::class, 'user_id');
break;
default:
# code...
break;
}
}
How do I return a variable of relationship type and how do I bring the relationships together :how i can do this =>($allCards += $ch->cards;)?
Is there a better way or mechanism to solve this problem?

Laravel left join check if a conditon is greater than a count

I would like to check a limit in number of user payment where a limit is set on user table.
So i have the following database structure
table user
id,name,...,pay_limit
and payment table
table payment
id, user_id, payment_ref
So i have created the following code
$query = User::query();
//other stuff
$query->leftJoin('payment','payment.user_id','=','user.id')
//stuck
Am stuck on how to check if the totals of payments on a user is not greater than the user pay_limit
How can i check the above in a query
Simple with relations. Suppose payment model is Payment and payment amount in payment_amount column
class User extends Model{
public function payments()
{
return $this->hasMany(Payment::class);
}
public function getIsOverLimitedAttribute(): bool
{
//if you check amount
return $this->payments()->sum('payment_amount') > $this->pay_limit;
//or if you check count of payments
return $this->payments()->count() > $this->pay_limit;
}
public function scopeNoOverLimited($query){
return $query->withCount('payments')->having('payments_count', '<', $this->pay_limit);
}
}
And use
if($user->isOverLimited){
//do stuff
}
Or get not over limited users:
User::noOverLimited()->get();
In terms of performance (since you want to be able to return all such users), the best approach is to store a summary column in your users table... total_payment
You can update all current users maybe via a migration file as such:
$table->integer('total_payment')->unsigned()->nullable();
DB::update("update users set total_payment = ('select count(id) from payments where user_id = id')");
Then to get all such users, you can do:
User::whereRaw('total_payment > pay_limit')->get();
Then add a PaymentObserver to increase the total_payment on new successful payments.
If you don't have access to modify the table, you can still use a scope like, but this can be performance intensive if being run all the time such as on user login without caching:
public function scopeAboveLimit($query)
{
$id = $this->id;//user id
return $query->whereRaw("pay_limit < ('select count(id) from payments where user_id = $id')");
}

laravel Many Records to One single record....but how?

I have two tables, one is Users and the other is Loans. These have one to many relationship between them. One user can have many loans and one loan belongs to one User.
Now I want to get the user from the list of loans.
Loan::where('user_id', $user)->get()
This give me repeated array of sample user associated to the multiple loans. But I want to get only a single record, like many loans associate to one user.
I assume you already set up relationships between User & Loan Models
// App\User
public function loans() {
return $this->hasMany('App\Loan');
}
// App\Loan
public function user() {
return $this->belongsTo('App\User');
}
Get user from the list of loans
$user = Loan::where('user_id', $id)->first()->user;
// eager loading version
$loansWithUsers = Loan::with('user')->where('user_id', $id)->get();
Loans associate to one user
$loans = User::findOrFail($id)->loans;
// eager loading version
$userWithLoans = User::with('loans')->findOrFail($id);
For what I understand in your question, you want to get the user together with it's loan right ? Your query should like this.
$user_loans = User::with('loans')->where('id', $user_id)->get();
Get the user informations from loan table
Loan::where('user_id', $user_id)->with('user')->get()
Ge the users from loans table depends on brach id
Loan::where('branch_id', $branch_id)->with('user')->get();

How to query from multiple tables Laravel DB

Im new to laravel db query. I have problem when want to query.
I have two table customer and history. In history it have few column which are customer_id, activity (purchase,buyback), product_type(gold,silver) and quantity. Currently I want to retrieve balance for each of customer. To get balance, purchase - buyback.
-> customer_id | gold_balance | silver_balance
Here I attach two part of table and code to be review.
Thanks in advance.
You should create a hasMany relationship between Customer and History (maybe called histories) and create a custom attribute that loops through histories and add/subtract amount.
Something like this (not tested):
class Customer {
public function histories() {
return $this->hasMany(History::class);
}
public function getBalanceAttribute() {
return $this->histories->reduce(function($total, $current) {
$grossAmount = $current->amount * $current->quantity;
return $current->activity === 'purchase' ? $total - grossAmount : $total + grossAmount;
}, 0;
}
}

Laravel 5.2 and belongsToMany combined query?

Could you please help me with combined query (I am beginner in laravel)
I have 3 tables
users : id and name
words : id and name
user_word : user_id ,word_id and level
Each user have his own Words
With relation model
Class User:
public function words()
{
return $this->belongsToMany('App\Word' , 'user_word','user_id','word_id')
->withPivot('level');
}
class Word
public function users()
{
return $this->belongsToMany('App\User' , 'user_word','word_id','user_id')
->withPivot('level');
}
In the WordController.php i need to mack query of other users words but if the current Auth::user have the same words I want it to be excluded.
$words = $user->words()
->paginate(10,["mword","tword","image","id"],"p");
this code gave me all the words for the Auth::user.
Thank you.

Resources