laravel database

 can werite in raw sql, fluent query builder, eloquent orm


laravel suports 

mysql 5.7+

postgresql 9.6+

sqlite 3.8.8+

sql server 2017+


db::connection()->getPDO;

DB FACADE PROVIDES METHODS FOR EACH TYPE OF QUERY

select,update,insert and delete statements

db:sselect

DB::insert('INSERT INTO customers (name,email) values(?,?)",{'vicky','vickya@gmail.com']);


find,get,first,pluck,select,

aggregate statements

count,max(),sum(),exists(),min(),avg(),doesnExist(),

select,distinct()

addselect();


joins()

join(),leftjoin,rightjoin,crossjoin,union,unionall(),



crossjoin





methods

where/orWhere

wherebetween /orwherebetween, wherenotbetween,orwherernotbetween,wherein,wherenotin,

wherenull,wherenotnull,orwherenull,orwherenotnull,whreredate,

wherermonth,whereday,whereyear,wheretime

wherecolumn

wherein


laravel query builder ordering

orderby

latest

oldest

inrandomorder

reorder


groupby

having

havingbetween

limit and offset

limit('5')


insert,update,delete,create

insertorignore->if there it wil ignored

insertgetid

where,count();

truncate

delete

migrations

php artisan migrate:rollback --step=2 upto some migratiosn only

php aritsan migrate:reset-> rollback all migrations

php artisan migrate:refresh

php artisan migrate:fresh


database seeding

for create fake databse, to test our application

php artisan make:seeder testseeder

db:table('cars')->insert($data):

php artisan db:seed -class=testseeder

which seeder u writerm that will register in databseseeder


if u wanna see logs

storage->logs->laravel.log


php artisan migrate:fresh --seed-> runn all seeders again


run the seeder along with migration

\illuminate\support\facades\artisan::call('db:seed',['--class'=>'casrsseedr']);



DB::statement() is a method provided by Laravel's Database Query Builder that allows you to execute a raw SQL query directly against the database.

The syntax for DB::statement() is:

css
DB::statement('YOUR_SQL_QUERY');

Here, YOUR_SQL_QUERY should be replaced with the actual SQL query you want to execute. This method does not return any data, it just executes the SQL query and returns void.

For example, if you want to create a new table using DB::statement(), you can do it like this:

css
DB::statement('CREATE TABLE users (id INT, name VARCHAR(255), email VARCHAR(255))');

This will create a new table called users with three columns: id, name, and email. Note that you should only use DB::statement() when you need to execute a raw SQL query, and you should always sanitize any user input to prevent SQL injection attacks.


  • Laravel does not provide first-party support for _______.

    Marked Answer : MongoDB

    Correct Answer : MongoDB

  • The ____ facade provides methods for each type of query.

    Marked Answer : DB

    Correct Answer : DB

  • Which DB façade method is used to execute a general statement?

    Marked Answer : select()

    Correct Answer : statement()

  • The _____ method returns a fluent query builder instance for the given table.

    Marked Answer : get()

    Correct Answer : table()

  • DB::table('table_name');
  • Which method is used to retrieve a single row?

    Marked Answer : first()

    Correct Answer : first()

  • Which method will return the value of the column directly?

    Marked Answer : value()

    Correct Answer : value()

  • Which of the given is not a aggregate function?

    Marked Answer : value()

    Correct Answer : value()

  • Which raw method is not provided by DB façade?

    Marked Answer : selectRaw()

    Correct Answer : distinctRaw()

  • Which where method is not supported?

    Marked Answer : whereDate()

    Correct Answer : None of the above

  • Which method is used to paginating query builder results?

    Marked Answer : paginate()

    Correct Answer : paginate()






  • No comments:

    Post a Comment

    Event listening in react

     How we can listen to som eevents some envents fire like click or automatically user enters into input button , that is event on word type i...