I am looking to use a Resource (destroy) post command to remove all rows within one column. The columns name is 'total' and it houses 'bigIntegers' but can be null. This is an SQLITE DB.
I have looked into using "truncate" but it seems that this removes all data from the table. I just want to clear the values within one particular column.
Is there an easy way using Eloquent in 4.2 to do this?
I'd try either with Eloquent:
YourModel::update([ 'total' => null ]);
Or the query builder:
DB::table('yourTable')->update([ 'total' => null ]);
Here's links to the docs:
Eloquent
Query Builder
DISCLAIMER: I'm not on my computer and can't check said solution, in the docs I found the update() always preceeded by a where() so it may be mandatory, but you can do where('id', '>', '0') or something. Also maybe null will build the actual SQL with nothing on it for a value, resulting in error, so maybe you actually need ['total' => 'null']
You don't need the where close. All rows in the table column total will be set to null.
Model::update(['total' => null]);
Related
I've been googling this for a while but to no avail. I'm finding the possible answers confusing and hoped someone could clear it up for me.
I've two tables (tasks and installs) which contain similar data, but not the same, and there's no relationship between the two tables, other than the fact they both belong to the same branch. So for example:
Tasks Table
id
branch_id
task_name
to_be_billed
created
Installs Table
id
branch_id
install_details
to_be_billed
created
I'm trying to figure out how to get a result set which would show each record from either table, arranged by date created order and only where the 'to_be_billed' column is '1'.
Can anyone give me some pointers please?
Thanks
I'm assuming that you want to get the results using the branch_id and these two tables (Tasks and Install) have some relationship with the BranchTable.
I'm also assuming the Tasks and Installs Table's have multiple records for a Branch.
BranchTable->find()
->contain([
'Tasks' => [
'sort' => ['Tasks.created' => 'ASC']
]
])
->contain([
'Installs' => [
'sort' => ['Installs.created' => 'ASC']
]
])
->matching('Tasks', function ($q){
return $q->andWhere(['Tasks.to_be_billed' => 1]);
})
->matching('Installs', function ($q){
return $q->andWhere(['Installs.to_be_billed' => 1]);
})
->where(['Branch.id' => $foo]);
If your doubt does not use these assumptions let me know.
If you are trying to get the data using one DB query then you would need to use the UNION operator.
In that case you would need these two queries to have the same columns, so for example:
select
id,
branch_id,
task_name,
NULL as install_details,
'task' as type,
to_be_billed,
created
from
tasks_table
UNION
select
id,
branch_id,
NULL as task_name,
install_details,
'install' as type,
to_be_billed,
created
from
install_table
but that's a rather dirty solution.
If I knew what exactly you are trying to achieve, maybe I could suggest a better answer.
I have a problem.
Needs to get the first date of creation of data in the table by the currently logged in user.
Working example:
$howMuchIPlay = Game::all('user_id')->where('user_id', '=', Auth::user()->id)->count();
Not working example:
$firstVoteInGame = Game::all('created_at')->first()->where('user_id','=', Auth::user()->id);
When I use count() function I get value 38. This is all the rows for the user, but I want to get only the earliest (first). How can I do this?
Sorry for my english and thank you in advance for help.
$firstVoteInGame = Game::where('user_id', '=', Auth::user()->id)->oldest()->first();
This will get the earliest (oldest) record from the current authenticated user.
You got the order wrong.
You should first limit by user Id.
Then order by created at in descending order so oldest is on top. Or if you want the newest, order by 'desc' instead of 'asc'
Then get the first.
Game::where('user_id','=', Auth::user()->id)->orderBy('created_at','asc')->first();
What you did was:
Get all records from database in a collection.
get first possible record from collection at index 0
limit a model object by a where query(it would fail here since you were in a model object, not a collection or query builder instance);
Your not working example, $firstVoteInGame = Game::all('created_at')->first()->where('user_id','=', Auth::user()->id);, starts off with Game::all('created_at').
What this means is that all Games are returned, but these are limited to the 'created_at' column only (see the documentation here).
Since you're limiting your result to the 'created_at' column, Eloquent will now not be able to compare the 'user_id' columns to the authenticated user id.
In addition, even if you were to select all columns (by performing Game::all()), you are calling ->first() after which you only have the first row remaining.
You say you want to
get the first date of creation of data in the table by the currently logged in user.
This would thus by done by:
$firstVoteInGame = Game::where('user_id', Auth::user()->id)->orderBy('created_at','asc')->first('created_at');
Why you use 'created_at' for ordering as easy/correct way is use primary key, Because you can just do:
$firstVoteInGame = Game::where('user_id','=', Auth::user()->id)->orderBy('id', 'desc')->first();
You can try, if you still want 'created_at':
$firstVoteInGame = Game::where('user_id','=', Auth::user()->id)->orderBy('created_at', 'desc')->first();
TIP
- The Eloquent all method will return all of the results in the model's table so avoid to use it with first method
I am using Laravel 5.1. below is my Database query
\App\Models\Project\Bids\ProjectBid_Model
::with(['Project' => function($query){
$query->where('WhoCreatedTheProject', 16);
}])
->where('ProjectBidID', 1)
->where('FreelancerAwardedProjectStatusID', \App\ProjectStatus::InProgress)
->first()
What's the Question ?
In the below part, if there exists no record, then the Project Object will be null. This seems a sub query in the select statement. Is there any way to return overall no record if 'WhoCreatedTheProjectvalue does not match in the database ? I meant I am looking forInner Join`.
::with(['Project' => function($query){
$query->where('WhoCreatedTheProject', 16);
}])
You can do this this way (you will have one query):
\App\Models\Project\Bids\ProjectBid_Model
::selectRaw('projectbid_table.*')
->join('projects_table','projectbid_table.project_id','=','project_table.project_id')
->where('WhoCreatedTheProject',16);
->where('ProjectBidID', 1)
->where('FreelancerAwardedProjectStatusID', \App\ProjectStatus::InProgress)
->first();
You need to of course alter table names in join to match your values.
But answering the question, in your example any subquery won't be run. You are getting all the project bids (this is 1st query) and later in separate query you get all the projects for this bids for which WhoCreatedTheProject = 16 (this is 2nd query). So this what you showed is something different what you want to probably achieve.
I am trying to make a database query where I search for all of the items which have a lower current level than low stock level. When running the query I am getting no results and I'm not sure why.
This is my query
public static function getLowStockItemsCache()
{
dd(\DB::table('items')->where('current_level', '<', 'low_stock_level')->get());
}
When I die and dump this I get an empty collection.
In my database I have the following records
Both of these fields at set at int(11)
If I reverse the query I get all 13 records.
Am I missing something small, as it's confusing me greatly and should be simple.
The third parameter is the value parameter which means it will be escaped when building the query to avoid SQL injection. If you want to compare two columns, you have to specifically tell the Query Builder that that part of the query should not be processed, either by using whereRaw to pass the raw SQL condition:
\DB::table('items')->whereRaw('current_level < low_stock_level')->get());
Or by using DB::raw() for the value only, so it's not escaped:
\DB::table('items')->where('current_level', '<', DB::raw('low_stock_level'))->get());
Laravel use 3 step argument on the "where" clause,
Pair::where('1st','2nd','3rd')
by default the 2nd arg is normally default equal sign, saying the 1st arg is equal to the 3rd arg,
Pair::where('name', 'john')->get();
Pair::where('stock', '<', '100')->get();
When you find this logics then you understand the "where" tricks
I have been stumped by CakePHP in how to query the DB in CakePHP and return things to $data only when the $data query table [id] has a matching [sub_id] in a second table
a standard query:
$data = $this->Table1->findAll(array("Table1.deleted" => "0"), null, "Table1.id DESC", 25, null, 1);
But I want to only have values put into $data when the $data['Table1']['id'] is found in ['table2']['sub_id']
Thanks!
If you have your relationships setup properly it should do this automatically. Can you paste your Model relationship setup for Table1 and Table2?
Supernovah -
Please clarify one thing for me: you write that you want to only have values put into $data when table1.id is found in table2.sub_id. Do you mean that table2.sub_id is a foreign key, linking to table1?
I think Beau is right -- if you have the models correctly linked, using a HABTM or belongsTo, etc., variable, the findAll should automatically pull the associated records from table2.
The final caveat is that the model associations are affected by the value of Model->recursive. If you have changed the value of the recursive property in your code, it would alter how deep the model relations are allowed to go on a given query.
HTH!
In the model, in the relation array add:
$hasMany = array(
.....
'required' => true
....
);
This should make it do an inner join in sql rather than a left join. Hope this helps.