I encountered an issue that blocks my way forward.
I have tried getting count of polls as of now as shown below:
public function user_dashboard(){
$activepolls = DB::table('larapoll_polls')
->selectRaw('count(*) As total')
->join('larapoll_options', 'larapoll_polls.id', '=', 'larapoll_options.poll_id')
->join('larapoll_votes', 'larapoll_votes.option_id', '=', 'larapoll_options.id')
->where('larapoll_votes.user_id', auth()->user()->id)
->count();
return view('user_dashboard',compact('activepolls'));
}
As of Now I am thinking that this below function would have help me but i am not sure.
public function hasVoted($poll_id)
{
$poll = Poll::findOrFail($poll_id);
if ($poll->canGuestVote()) {
$result = DB::table('larapoll_polls')
->selectRaw('count(*) As total')
->join('larapoll_options', 'larapoll_polls.id', '=', 'larapoll_options.poll_id')
->join('larapoll_votes', 'larapoll_votes.option_id', '=', 'larapoll_options.id')
->where('larapoll_votes.user_id', auth()->user()->id)
->where('larapoll_options.poll_id', $poll_id)->count();
return $result !== 0;
}
return $this->options()->where('poll_id', $poll->id)->count() !== 0;
}
Simply i had made a poll box in my website and i want to show the count of polls in right corner of that box.
i need the count of poll in which user has not voted.
if user votes on poll it should remove from count as well.
If anyone can help , the response would much appreciated..
Thanks..
I have Solved The issue by following code...
$activepolls = DB::table('larapoll_polls')
->selectRaw('count(*) As total')
->join('larapoll_options', 'larapoll_polls.id', '=', 'larapoll_options.poll_id')
->join('larapoll_votes', 'larapoll_votes.option_id', '=', 'larapoll_options.id')
->where('larapoll_votes.user_id', auth()->user()->id)
->count();
$polls = Poll::count();
$unvotedpolls = ($polls - $activepolls);
in ($unvotedpolls) i got the count of polls in which auth user has not voted...
Related
I have a search form with four fields of customer email, vendor name, status, and fromdate, todate,. My search filter is not working properly. I want if someone searches a lead with vendor name and status Active then it shows only leads of that vendor with status active status but here it shows a leads with both Accept or Reject also my date filter is not working so please help me. Please guide me
my controller code is
public function search(Request $request){
$users = DB::table('users')->where('is_admin', Null)->get();
$customer_email = $request->input('customer_email');
$vendor_id = $request->input('vendor_id');
$status = $request->input('lead_status');
$leads = DB::table('leads')
->leftJoin('users', 'leads.vendor_id', '=', 'users.id')
->select('leads.*', 'users.name')
->where('vendor_id', $vendor_id)
->orWhere('customer_email', $customer_email)
->orWhere('lead_status', $status)
->orWhere('leads.created_at', array($request->start_date, $request->end_date))
->orderBy('leads.created_at', 'DESC')->get();
//dd($leads);
return view('admin.view-leads', compact('leads'), compact('users'));
}
please help.
Thanks in advance
query image
That's a pretty simple thing. Just follow the code below.
To get the users where is_admin is null.
$users = DB::table('users')->whereIsNull('is_admin')->get();
To check if you are submitting a value for any filter.
if($request->filled('name_of_filter_input')){ //code goes here }
So your query to filter records will go like.
$query = DB::table('leads')->query();
$query->leftJoin('users', 'leads.vendor_id', '=', 'users.id');
$query->select('leads.*', 'users.name');
if($request->filled('vendor_id')) {
$query->where('vendor_id', $request->input('vendor_id'));
}
if($request->filled('customer_email')) {
$query->where('customer_email', $request->input('customer_email'));
}
if($request->filled('lead_status')) {
$query->orWhere('lead_status', $request->input('lead_status'));
}
if($request->filled('start_date') && $request->filled('end_date')) {
$query->whereBetween(DB::raw('date(leads.created_at)'), [$request->input('start_date'), $request->input('end_date')]);
}
$leads = $query->orderBy('leads.id', 'DESC')->get();
Further you can replace DB::table('table_name') syntax with respective model classes in case query() seems to be undefined function.
Wrap leads.created_at with date() function and ensure your date filter(s) has date in yyyy-mm-dd format if the created_at column is of type timestamp.
you should write your query like as below
$leads = DB::table('leads')
->leftJoin('users', 'leads.vendor_id', '=', 'users.id')
->select('leads.*', 'users.name')
->where('vendor_id', $vendor_id)
->when($customer_email, function($q,$customer_email){
$q->where('customer_email', $customer_email);
})
->when($status, function($q,$status){
$q->where('lead_status', $status);
})
->where(function($q) use($request){
if(isset($request->start_date) && isset($request->end_date)){
$fromDate = date('Y-m-d H:i:s',strtotime($request->start_date));
$toDate= date('Y-m-d H:i:s',strtotime($request->end_date));
$q->where('leads.created_at', '>=', $fromDate)
->where('leads.created_at', '<=', $toDate);
}
})
->orderBy('leads.created_at', 'DESC')->get();
I updated my answer please look into it
Right now I have a subquery to get the count of payments for the current month and then getting the 4 products with the highest payment_count. This query works fine but I'm wondering if there's a more simple way to do the same since its getting difficult to read.
$latestPayments = DB::table('payments')
->select('product_id', DB::raw('COUNT(*) as payments_count'))
->whereMonth('created_at', Carbon::now()->month)
->groupBy('product_id');
$trendingProducts = DB::table('products')
->joinSub($latestPayments, 'latest_payments', function ($join) {
$join->on('products.id', '=', 'latest_payments.product_id');
})->orderBy('payments_count', 'DESC')->take(4)->get();
This did it!
$trendingProducts = Product::withCount(['payments' => function($query) {
$query->whereMonth('created_at', Carbon::now()->month);
}])->orderBy('payments_count', 'DESC')->take(4)->get();
If you are using eloquent query with relational database you can do like this:
$latestPaymentWithTrendingProduct = App\Payment::with(['products', function($product) {
$product->orderBy('payments_count', 'DESC')->take(4);
}])->whereMonth('created_at', date('m'))->get()->groupBy('product_id');
This will lessen the code but still do the same thing.
I'm quite lost here. I am trying to get the list of people who are on the Records table which aren't on the Profiles tables which has the specific course (e.g Psychology) and year (e.g 2011). So far, this is what I came up with and unfortunately it's not working.
$check = Record::join('Profiles', function ($join) {
$join->on('Records.firstname', '!=', 'Profiles.firstname');
$join->on('Records.lastname', '!=', 'Profiles.lastname');
$join->on('Records.middlename', '!=', 'Profiles.middlename');
})
->select('Records.*', 'Profiles.*')
->where('Records.year', '2011')
->where('Records.course', 'Psychology')
->get();
dump($check);
Is there any way that I can go around about this? I'm new to this. Thanks in advance. Advises and tips on joining tables would be greatly appreciated.
Please try a NOT EXISTS subquery:
use Illuminate\Database\Query\Builder;
$check = \DB::table('Records')
->whereNotExists(function(Builder $query) {
$query->select(\DB::raw(1))
->from('Profiles')
->whereRaw('Records.firstname = Profiles.firstname')
->whereRaw('Records.middlename = Profiles.middlename')
->whereRaw('Records.lastname = Profiles.lastname');
})
->select('firstname', 'middlename', 'lastname')
->where('year', 2011)
->where('course', 'Psychology');
->get();
I would look at doing a left outer join and then selecting the records that have a null profile name.
$check = Record::leftJoin('Profiles', function ($join) {
$join->on('Records.firstname', '=', 'Profiles.firstname');
$join->on('Records.lastname', '=', 'Profiles.lastname');
$join->on('Records.middlename', '=', 'Profiles.middlename');
})
->select('Records.*', 'Profiles.*')
->where('Records.year', '2011')
->where('Records.course', 'Psychology')
->whereNull('Profiles.firstname')
->get();
I am trying to get the records from a database where players have either played as player 1 or 2 in a game to calculate the gross profit for a player on a profile. I have the following Advanced Eloquent code:
$user = User::with('profile')->where('name', $username)->first();
$user_id = $user->id;
$games_model = Games::where(function ($q) {
global $user_id;
$q->where('player1', '=', $user_id)->orWhere('player2', '=', $user_id);
});
$games = $games_model->limit(3)->get();
// get gross profits
$gross_profits = $games_model->sum('bet');
dd($games_model->get()->toArray());
But when I try to take a look at the results dumped, I see a blank array. I am sure my user's ID is set correctly. The funny thing is it was only returning one result before instead of all the resulkts in the database. I'm confused. Thanks for the help in advance!
Something like this should work, assuming the database and naming conventions are follow.
Users::class
public function games()
{
return $this->hasMany(Games::class);
}
Query:
Users::where('name', $username)
->has('games')
->with('profile')
->limit(3)
->get();
Haven't tested but this should give you an idea.
here's the solution.
global dosen't work every time depending on variable scope. I changed
$games_model = Games::where(function ($q) {
global $user_id;
$q->where('player1', '=', $user_id)->orWhere('player2', '=', $user_id);
});
to
$user_id = $user->id;
$games_model = Games::where(function ($q) use ($user_id) {
$q->where('player1', '=', $user_id)->orWhere('player2', '=', $user_id);
});
and it works perfectly every time.
Im trying to Joing 2 table in Fluent trough my Model i run the query on mysql client and give me the return i want.
this is the standard query
select `songlist`.*, `queuelist`.* from `songlist` inner join `queuelist` on `songlist`.`id` = `queuelist`.`songID` where `queuelist`.`songID` = songlist.ID and `songlist`.`songtype` = 'S' and `songlist`.`artist` <> "" order by `queuelist`.`sortID` ASC limit 4
it does return the data im looking for.
now on my model using fluent.
i did like this.
public static function comingUp()
{
$getcomingUp = DB::table('songlist')
->join('queuelist', 'songlist.id', '=', 'queuelist.songID')
->select('songlist.*','queuelist.*')
->where('queuelist.songID', '=', 'songlist.ID')
->where('songlist.songtype', '=', 'S')
->where('songlist.artist', '<>', '')
->orderBy('queuelist.sortID', 'ASC')
->take('4')
->get();
return $getcomingUp;
}
and my controller to test if i get the data look like this
public function getComingUp()
{
$getcomingUp = Radio::comingUp();
foreach ($getcomingUp as $cup)
{
echo $cup->title;
}
// return View::make('comingup', compact('comingup'));
}
as you can see the foreach is no returning any data there is nothing wrong with the query because laravel give me no error at all. but i cant just get the foreach to display the data im looking for.
i try with $cup->songlist.title;
and it dont work ether.
Thanks any help will be appretiated. againg sorry for my English.
You can do something like this.
public static function comingUp()
{
$getcomingUp = DB::table('songlist')
->select('songlist.*','queuelist.*')
->join('queuelist', 'queuelist.songID', '=', 'songlist.id')
->where('songlist.songtype', '=', 'S')
->where('songlist.artist', '<>', '')
->orderBy('queuelist.sortID', 'ASC')
->take('4')
->get();
return $getcomingUp;
}