Suppose I have just inserted row on table test as like Test::create($inputs);,which has auto increment primary key field id.How to get id of row that I have just inserted using laravel 4.2/5 ?
The following snippet:
$test = Test::create($inputs)
Will enable you to use $test->id.
You can get the inserted id by using the example
$inserted_id = Test::create($inputs)->id;
You can use the following line to fetch last insert id while save
after save the record you need to use $test->id;
Related
I am trying to fetch soecific columns data from relational table but it is giving me null
$allConsignments = Consignment::query();
$allConsignments->select(['id','customer_reference'])->with('customers:name,id')->orderBy('id', 'desc')->limit(5000)->get();
When I don't use select() then it gives correct data .
like this
$allConsignments = Consignment::query();
$allConsignments->with('customers:name,id')->orderBy('id', 'desc')->limit(5000)->get()
it is working but I also need specific columns from Consignment Table. what could be the reason?
You can also do like this.
$allConsignments = Consignment::query();
$allConsignments::with('customers:name,id')->orderBy('id', 'desc')->limit(5000)->get(['id','customer_reference']);
Actually, I also need to select the foreign key column from the table on which relationship is based. for example in my case I have customer_id in consignment table so it should be like that
$allConsignments = Consignment::query();
$allConsignments->select('id','customer_reference','customer_id')->with('customers:name,id')->orderBy('id', 'desc')->limit(5000)->get();
I need to select customer_id as well
I have a column name in a table named 'Test' which contains almost 100k rows, I want to update existing values of 'Test' column by adding 1000. For example if a record contains existing value 5555 , it should get updated with 5555+1000 = 6555. In same way, how can i update for all rows in a table?
update "Test" set "Test"="Test"+1000
In my laravel 5.7 / mysql 5.7 app using eloquent I can clear rows in table using model :
Model::truncate();
But can I to clear primary key id generator, so first row would have value for id=1 ?
Thanks!
Model::truncate() uses 'TRUNCATE' function of MySQL so it resets your 'auto_increment' counter as well.
Read more about 'TRUNCATE' here here
So basically the new insert will start from 1 if the primary key field of the table was made Auto Increment
I want to delete multiple rows from my post_tags table where post_id is given .
table structure post_tags is : post_id , tag_id , created_at , updated_at
I am able to do this operation with DB query builder , but i want to
delete multiple rows with single elequent command without using forloop
I don't know how to do this ?
You should try this:
$post_id = [1,2,3];
DB::table('post_tags')
->whereIn('id',$post_id)
->delete();
You can pass Model::destroy($ids) an array. Alternatively, loop the records you want to delete and call delete on each: $model->postTags->each->delete().
I don't know that is this a silly question or not but I need to solve this. I have search with goggle but no result to satisfy me. The question is > I am inserting some value into a table. I am using oracle as database and grails 2.1.1. when it is inserting value in the table the row id does not begin with 1 but I need to start that particular table to start with 1. Cause There will be only four row in that table and I need to insert them by the id like 1,2,3,4 so that i can get them in various part of application. But it is giving other id and after then I changed them to 1,2,3,4. Is there any way to do this in domain level in grails ?
You need to create an oracle sequence.
http://www.techonthenet.com/oracle/sequences.php
CREATE SEQUENCE sequence_name
MINVALUE value
MAXVALUE value
START WITH value
INCREMENT BY value
CACHE value;
CREATE SEQUENCE my_seq
MINVALUE 1
MAXVALUE 4
START WITH 1
INCREMENT BY 1
CACHE 1;
Then in the domain specify the sequence
class MyDomain {
...
static mapping = {
id generator:'sequence', params:[sequence:'my_seq']
}
}
EDITED:
If you are using liquibase migration then use add a new changeset and then add it to you changelog.groovy.
To create a sequence add a changeSet into your grails-app/migrations
sequence.groovy
changeSet(author: "me", id: "add-sequence") {
createSequence(sequenceName: "my_seq")
}
changelog.groovy
include file: 'sequence.groovy'
For more options on creating sequences using liquibase look here: http://www.liquibase.org/documentation/changes/create_sequence.html
instead of this create your own id field in table and use sequences to generate any id you like, for example : create sequence id_seq start with 1 increment by 1 and use it while inserting : insert int TABLE_NAME values (id_seq.nextval, ...);. I would recommend you not to depend on oracle's row_id, because it's controlled by db and is genereted for it's needs (see also oracle ref).