I have a draw system on my site. There are 2 draws in the database now, but for some reason, only the last created draw is displayed on the draw page. My code is:
public function raffling()
{
parent::setTitle('Items raffling | ');
$green_ticket = \DB::table('users')->where('id', $this->user->id)->value('green_ticket');
$kolvo=\DB::table('giveaway_items')->where('status',0)->orderBy('id', 'desc')->count();
$giveaway = Giveaway::orderBy('id', 'desc')->first();
$giveaway_users = \DB::table('giveaway_users')
->where('giveaway_id', $giveaway->id)
->join('users', 'giveaway_users.user_id', '=', 'users.id')
->get();
$giveAway = Giveaway::where('winner_id', '!=', 'NULL')->orderBy('created_at', 'DESC')->first();
$user = User::find($giveAway->winner_id);
$username = $user->username;
$userava = $user->avatar;
$usersteamid = $user->steamid64;
return view('pages.raffling', compact('kolvo', 'giveaway', 'giveaway_users', 'giveAway', 'user', 'username',
'userava', 'usersteamid', 'green_ticket'));
}
I tried make changes at $giveaway = Giveaway::orderBy('id', 'desc')->first(); and instead of first() use take(2), but i received error Undefined property: Illuminate\Database\Eloquent\Builder::$winner_id. And the same error appears if I add take(2) to the line $giveAway = Giveaway::where('winner_id', '!=', 'NULL')->orderBy('created_at', 'DESC')->first(); instead of first().
Where is my problem, how i can fix this error?
When you have used first() method means only one records retrieve.
Now as you said you want two records from the database. There are two methods available here.
1) take()
$giveaway = Giveaway::orderBy('id', 'desc')->take(2)->get();
2) limit()
$giveaway = Giveaway::orderBy('id', 'desc')->limit(2)->get();
*Your problem occurs because you have not used the get() method.
take() is a method that is available on collections not the query builder. You need to get a collection from the query builder first and then use the method on that.
$giveAway = Giveaway::where('winner_id', '!=', 'NULL')
->orderBy('created_at', 'DESC')
->get()
->take(2);
use take(2)
$giveaway = Giveaway::orderBy('id', 'DESC')->take(2)->get();
Related
public function clientcmd()
{
$client = Commande::join('clients', 'commande.id_cli', "=", 'clients.id')
->select('clients.*')
->where('commande.id_cli', '')
->get();
return response()->json($client);
}
I want to select all the client where id client = id cli in command table but that not work they return an empty array
We don't know the data in your table. I assume you want to select fields whose id_cli field is not empty string.
$client = Commande::join('clients', 'commande.id_cli', "=", 'clients.id')
->select('clients.*')
->where('commande.id_cli','!=', '')
->get();
Use Query builder:
use Illuminate\Support\Facades\DB;
$clients = DB::table('clients')
->join('commande', 'commande.id_cli', '=', 'clients.id')
->select('clients.*')
->where(//you can put condition here)
->get();
You can use Client Model to get all client records
$clients = Client::select('*')
->join('commande', 'commande.id_cli', '=', 'clients.id')
->where("clients.id",$client_id) // put your condition here
->get();
I'm getting the row in the following code :
$grpusrow = DB::table('grpusrs')
->where('group_id', $id)
->where('user_id', $u_id)
->get();
And then getting the ID in this line :
$grpusrid = $grpusrow->id;
I receive the following error on the line in number 2 :
Trying to get property of non-object
->get() will always return multiple objects at once. You're simply not telling it which index of $grpusrow to get the ID from.
$grpusrows = DB::table('grpusrs')
->where('group_id', $id)
->where('user_id', $u_id)
->get();
foreach ($grpusrows as $grpusrow) {
dump($grpusrow->id);
}
// Or
$grpusrow = DB::table('grpusrs')
->where('group_id', $id)
->where('user_id', $u_id)
->first();
dump($grpusrow->id);
Maybe 2 solutions:
If you want a unique row in return (with a primary key in arguments for example), you should try ->first() instead of ->get().
The problem is the using of where clauses, I think it works with array when you want more than one where clause, try like this :
->where([['group_id', '=', $id],['user_id', '=', $u_id]])->get(); ( or first() )
This is my query.
$mem = DB::table('user_zakathorg')
->JOIN('zakathorgs', 'user_zakathorg.zakathorg_id', '=', 'zakathorgs.ID')
->WHERE('user_id', '=', Auth::user()->id )
->SELECT('zakathorg_id', 'orgName')->get();
And the output is always one raw. like following image
If i want to accesss the zakathorg_id. I have to use a foreach loop. And I can't access
$mem[0]['zakathorg_id]
how can i use this as simply $mem['zakathorg_id']
Use first() method instead of get():
$mem = DB::table('user_zakathorg')
->JOIN('zakathorgs', 'user_zakathorg.zakathorg_id', '=', 'zakathorgs.ID')
->WHERE('user_id', '=', Auth::user()->id )
->SELECT('zakathorg_id', 'orgName')->get();
// You can access $mem->zakathorg_id now
In my controller I have wrote this following code
$usersCount = User::where('activated', '=', 1)->where('group_id', '=', 1)->where('availability_date', '<=', $opportunity_date)->count();
$locations_array_result = explode(",",$locations_array_result);
foreach ($locations_array_result as $param)
{
$usersCount = $usersCount->whereHas('location', function($q) use($param){
$q->where('location_id', '=', $param );
});
}
This code giving following error
Call to a member function whereHas() on a non-object
Can anyone help me to find out what i have done wrong!!!
$usersCount is already a number from the 1st line of your sample.
You want instead to replace $usersCount->whereHas with User::whereHas in your foreach loop.
Taking a very wild guess here, I would think you need to get all users with these requirements
->where('group_id', '=', 1)->where('availability_date', '<=', $opportunity_date)
plus having a location_id value which exists on an array named $locations_array_result
If this is the case, this is all you need:
User::where('activated', '=', 1)->where('group_id', '=', 1)->where('availability_date', '<=', $opportunity_date)->whereIn('location_id', $locations_array_result)->get();
EDIT
Following your comment below, I assume user has many to many relation with locations (defined in your model), so eager loading and then using a condition with a callback should do the job:
$users = User::where('activated', '=', 1)
->where('group_id', '=', 1)
->where('availability_date', '<=', $opportunity_date)
->with(array('location' => function($query) use ($locations_array_result)
{
$query->whereIn('location_id', $locations_array_result);
}))->get();
I have a query builder that works:
$article = Page::where('slug', '=', $slug)
->where('hide', '=', $hidden)
->first();
But I want to only add the second where statement if hidden is equal to 1. I've tried the code below which shows the logic of what I'm trying to do, but it doesn't work.
$article = Page::where('slug', '=', $slug);
if ($hidden == 1) {
$article->where('hide', '=', 1);
}
$article->first();
I'm using Laravel 4, but I think the question still stands with Laravel 3.
Yeah there's a little "gotcha" with Eloquent and the query builder. Try the code below ;)
$query = Page::where('slug', '=', $slug);
if ($hidden == 1) {
$query = $query->where('hide', '=', 1);
}
$article = $query->first();
Note the assigning of $query within the conditional. This is becuase the first where (statically called) returns a different object to the query object within the conditional. One way to get around this, I believe due to a recent commit, is like so:
$query = Page::where('slug', '=', $slug)->query();
This will return the query object and you can do what you want as per normal (Instead of re-assigning $query).
Hope that helps.