My concern:
if ($case=='private') {
$langtitle = 'title';
}
else {
$langtitle='title_gov';
}
if it is Government (falls under else case above) I want to select, 'title' as well as 'title_gov' with Select in query as,
Images::select($langtitle ,'id', 'title')
->groupBy('id')
->paginate('10');
If it is private, then only 'title' to be selected. I do not want to use if else case for Query, instead I want to call it using Variable or regex or some method. How can I?
You were on the right track, the only issue you were having is that when the case is "private" it will load the column "title" twice, instead you can do the following:
if ($case == 'private') {
$langtitle = ['id', 'title'];
}else{
$langtitle = ['id', 'title', 'title_gov'];
}
Images::select($langtitle)->groupBy('id')->paginate('10');
I think you can use the When eloquent function
$query = Images::query();
$query->when(
$case == 'private',
function ($query) {
return $query->select('title', 'id');
},
function ($query) {
return $query->select('title_gov' ,'id', 'title'));
}
)
->groupBy('id')
->paginate('10');
You can read more about it here.
Related
How can I make this query in Laravel eloquent. Please, no DB Record solution.
SELECT slo.batch,
slo.type,
(SELECT Count(sl1.id)
FROM sync_log sl1
WHERE sl1.status = 1
AND sl1.batch = slo.batch) AS success,
(SELECT Count(sl2.id)
FROM sync_log sl2
WHERE sl2.status = 0
AND sl2.batch = slo.batch) AS failed,
slo.created_at
FROM sync_log slo
GROUP BY batch
ORDER BY slo.created_at
Below is the database table.
Try something like :
$result=DB::table('sync_log as slo')
->select('slo.batch','slo.type', 'slo.created_at', DB::raw(('SELECT Count(sl1.id) FROM sync_log sl1 WHERE sl1.status=1 AND sl1.batch = slo.batch) AS success'), DB::raw(('SELECT Count(sl2.id) FROM sync_log sl2 WHERE sl2.status = 0 AND sl2.batch = slo.batch) AS failed')
->groupBy('batch')
->orderBy('slo.created_at')
->get();
Without any idea on your structure or models. guessing a manyToMany relation wetween batch and type where sync_log is the pivot table between them.
$batchs = Batch::withCount([
'types as success' => function ($query) {
$query->where('status', 1);
},
'types as failed' => function ($query) {
$query->where('status', 0);
}])
->get();
Using Eloquent ORM it can be tricky but I guess will work, you can define hasMany relation(s) in same model which will relate to same model using batch attribute/key like
class SyncLog extends Model
{
public function success_batches()
{
return $this->hasMany(SyncLog::class, 'batch', 'batch')->where('status',1);
}
public function failed_batches()
{
return $this->hasMany(SyncLog::class, 'batch', 'batch')->where('status',0);
}
}
Then using your model you can get count for these relations using withCount
$bacthes = SyncLog::withCount(['success_batches','failed_batches'])
->select(['batch','type'])
->distinct()
->orderBy('created_at')
->get();
If you don't want to define it twice based on where clause then you can follow the approach explained in #N69S's answer like
class SyncLog extends Model
{
public function batches()
{
return $this->hasMany(SyncLog::class, 'batch', 'batch');
}
}
$bacthes = SyncLog::withCount([
'batches as success' => function ($query) {
$query->where('status', 1);
},
'batches as failed' => function ($query) {
$query->where('status', 0);
}])
->select(['batch','type'])
->distinct()
->orderBy('created_at')
->get();
I have draws on my site, in order to take part in the draw, you need to do a certain action per day. And there is a code that checks it all:
$date = Carbon::today();
$sta = \DB::table('ets')->where('user_id', $this->user->id)->where('created_at', '>=', $date)->get();
$sta = \DB::table('ets_1x1')->where('user_id', $this->user->id)->where('created_at', '>=', $date)->get();
$sta = \DB::table('ets_low')->where('user_id', $this->user->id)->where('created_at', '>=',$date)->get();
$sta = \DB::table('ets_duel')->where('user_id', $this->user->id)->where('created_at', '>=', $date)->get();
if ($sta == NULL) {
return response()->json(['status' => 'error', 'msg' => 'Error']);
}
This code checks if there is a user record in 4 possible tables. I made an entry in the table ets_1x1, but still I can’t take part, because the error seemed to not find me in the database. I removed all the tables and left only ets_1x1 and I was accepted into the drawing.
As I understand it, the value is taken from the last request. How can I combine a query into 1 and do a check on these 4 tables?
UPD:
I also tried to give new names to the variables and display the response code differently, now participation in the drawing is accepted from all people, even from those who have not fulfilled the conditions, now it looks:
if(!empty($sta_1) || !empty($sta_2) || !empty($sta_3) || !empty($sta_4)) {
return response()->json(['status' => 'error', 'msg' => 'Error']);
}
Where my mistake?
That code is not going to work because:
The first piece of code will evaluate only the last request (and in consecuence, only if there is any existent user on the last table only).
The second piece of code is not being evaluated correctly, you are running empty function on a Laravel collection.
Why don't you try this? I think it should work:
$date = Carbon::now();
$userExists = false;
$tables = ['ets', 'ets_1x1', 'ets_low', 'ets_duel'];
foreach ($tables as $tableName) {
$result = \DB::table($tableName)
->where('user_id', $this->user->id)
->where('created_at', '>=', $date)
->get()
;
if ($result->isNotEmpty()) {
$userExists = true;
break;
}
}
if (!$userExists) {
return response()->json(['status' => 'error', 'msg' => 'Error']);
}
I'm trying to figure out how to get all rows except few (A and B) in Eloquent ORM modal.
User Model
public function notifications()
{
return $this->hasMany('notification','listener_id','id');
}
Model Notification
public function scopeFriendship($query)
{
return $query->where('object_type', '=', 'Friendship Request');
}
public function scopeSent($query)
{
return $query->where('verb', '=', 'sent');
}
Here how can I get all notifications of a user except other than (Friendship and Sent) scope.
Something like:- all rows except !(Friendship AND Sent)
It is possible to use scopes in combination with eager loading. Like that:
User::with(['notifications' => function($q){
$q->friendship();
}])->get();
However we now need to invert the scope somehow. I can think of two ways to solve this.
1. Add negative scopes
public function scopeNotFriendship($query){
return $query->where('object_type', '!=', 'Friendship Request');
}
public function scopeNotSent($query){
return $query->where('verb', '!=', 'sent');
}
User::with(['notifications' => function($q){
$q->notFriendship();
$q->notSent();
}])->get();
2. Optional parameter
Or you could introduce an optional parameter to your current scopes. Something like this:
public function scopeFriendship($query, $is = true)
{
return $query->where('object_type', ($is ? '=' : '!='), 'Friendship Request');
}
public function scopeSent($query, $is = true)
{
return $query->where('verb', ($is ? '=' : '!='), 'sent');
}
This way you would only have to pass in false:
User::with(['notifications' => function($q){
$q->friendship(false);
$q->sent(false);
}])->get();
Edit
You can even gain more control by adding a second parameter for the boolean (AND or OR of the where:
public function scopeFriendship($query, $is = true, $boolean = 'and')
{
return $query->where('object_type', ($is ? '=' : '!='), 'Friendship Request', $boolean);
}
And if you wanted either scope to be true:
$q->friendship(true, 'or');
$q->sent(true, 'or');
2nd Edit
This one finally worked (from the chat)
Notification::where('listener_id', $user_id)
->where(function($q){
$q->friendship(false)
$q->sent(false, 'or')
})
->get();
I need to build a query with eloquent model including conditional where clause. I created something by searching google. But it doesn't seems to be working.
if ($status) {
$this->where('status', $status);
}
if ($assignedto) {
$this->where('assigned_to', $assignedto);
}
if ($fromDate != null && $toDate != null) {
$this->whereBetween('date', array($fromDate, $toDate));
}
$quotes = $this->orderBy('date', 'desc')->paginate(40);
This one returns all results without the filtering of status, assigned to and dates.
I just updated it by assigning $this to a new variable because when I assigned the query to $this, it shown an error, $this cannot be over written.
$quoteModel = $this;
if ($status) {
$quoteModel = $quoteModel->where('status', $status);
}
if ($assignedto) {
$quoteModel = $quoteModel->where('assigned_to', $assignedto);
}
if ($fromDate != null && $toDate != null) {
$quoteModel = $quoteModel->whereBetween('date', array($fromDate, $toDate));
}
$quotes = $quoteModel->orderBy('date', 'desc')->paginate(40);
which works perfectly now. But if somebody have some better option, please suggest. Thanks.
I added a scope to my model and call it from the controller. This way the actual filtering work is done in the model, the controller is just running the query through the filter scope.
Model:
I am getting URL parameters with Input::get, but you would also pass them as input parameters to the function.
public function scopeFilter($query)
{
$published = Input::get('published');
if (isset($published)) $query->where('published', '=', $published);
return $query;
}
Controller:
Here the query is run through the filter() before returning it to the view.
public function index()
{
return View::make('articles.index', [
'articles' => Article::with('writer')->filter()->get()
]);
}
I'm trying to update the updated_at column to the current time, each time a user logs in.
But I get the following error:
InvalidArgumentException A four digit year could not be found Data missing
PHP
$input = Input::all();
$remember = (Input::has('remember')) ? true : false;
$auth = Auth::attempt([
'username' => $input['username'],
'password' => $input['password'],
'active' => 1
],$remember
);
if ($auth)
{
$user = Auth::user();
$user->updated_at = DB::raw('NOW()');
$user->save();
if ($user->userType==1)
{
return Redirect::intended('admin');
}
elseif ($user->userType==2)
{
return Redirect::intended('corporate');
}
elseif ($user->userType==3)
{
return Redirect::intended('trainer');
}
elseif ($user->userType==4)
{
return Redirect::intended('user');
}
}
You can use the Eloquent method touch() for this:
//instead of
$user->updated_at = DB::raw('NOW()');
$user->save();
// simply this:
$user->touch();
For one I would not use the updated_at column as that's the default timestamps name.
You would be better of with last_login
And just use the PHP date method.
$user->updated_at = date('Y-m-d G:i:s');
Hope this helps.
I think you're accidentally assigning a value instead of using array syntax. eg:
Model::create([
'key'='val'
]);
instead of:
Model::create([
'key'=>'val'
]);