You are on page 1of 4

1 - C:\xampp\htdocs>composer create-project laravel/laravel sampleapp

2 - C:\xampp\htdocs>composer create-project --prefer-dist laravel/laravel sampleapp

===================================================================================
================================
for database migrations steps to follow

scope resolution operator = (::)

1- php artisan migrate


this will migrate all the tables to phpmyadmin database

2- php artisan make:migration create_post_table --create="posts"


this will create the migrations new table called posts
after creating any table you have to use "php artisan migrate" to update the db
with new table

3- php artisan migrate:rollback


this is used for to delete the most recent created tables

4-php artisan migrate:reset


it will remove all the tables from phpmyadmin

5-php artisan migrate:refresh


it will do both rollback and migrate at same time

6- php artisan make:model Role -m


this will craete the file in App directory and will also create the table file in
migrations directory

7-php artisan make:migration add_column_to_existing_table --table=users


this will add the new column to existing table
===================================================================================
================================
SIMPLE CRUD OPERATIONS

Route::get('/insert', function () {
DB::insert('insert into posts (title, body) values (?, ?)', ['Ausaf
Liaquat','ddddddddddddd']);
});

Route::get('/read', function () {
$result = DB::select('select * from posts where id= ?' , [1]);
foreach ($result as $value) {
return $value->title;
}
});
Route::get('/update', function () {
$update= DB::update('update posts set body = "This is my updated content" where
id = ?', [1]);
return $update;
});

===================================================================================
================================
Working With Models

1-php artisan make:model Post


Each database table has a corresponding "Model" which is used to interact with that
table.
like here we have table 'posts' in db and for this we have created model Post
through which can perform all db crud operations.

2-C:\xampp\htdocs\todoapp>php artisan make:controller TodoappController --resource

===================================================================================
================================
Eloquent ORM(object relational mapper)

Route::get('/findwhere', function () {

$post=Post::where('id',1)->orderby('id','desc')->take(1)->get();

return $post;

});

Route::get('/findmore', function () {

$post = Post::findOrFail(1);

findOrFail() is alike of find() function with one extra


ability - to throws the Not Found Exceptions

// $post = Post::where('id,'<',50)->firstOrFail();
return $post->title;
});

[ INSERTING DATA QUERY WITH ELOQUENT ]

Route::get('/basicinsert', function () {

$post = new Post;


// $post = Post::find(1);
//it will update the data of id 1
$post->title = "New Eloquent title insert";
$post->body= "Insert Saving Data ";
$post->save();

});

[ UPDATING DATA QUERY WITH ELOQUENT ]

Route::get('/update', function () {

Post::where('id',1)->update(['title'=>'Updated Content','body'=>'Eloquent
Updated Query']);

});

[ DELETING DATA QUERY WITH ELOQUENT ]

Route::get('/delete', function () {
$post = Post::find(4);
$post->delete();

//OR
// Post::destroy(3);
// Deletin Multiple data
//Post::destroy([1,2]);
//Post::where('id',1)->delete();
});

===================================================================================
================================
php artisan tinker
// Tinker is used for Database CRUD Operation

$post = App\post::create(['title'=>'Web Dev is love','body'=>'love is web dev'])


//Syntax to Insert the data into database

===================================================================================
================================
creating table with dumy/fake data through factory and seeder function.

C:\xampp\htdocs\todoapp>php artisan make:model Todo


C:\xampp\htdocs\todoapp>php artisan make:migration create_todos_table
C:\xampp\htdocs\todoapp>php artisan migrate

//ye factory aur seeder laravel mai dumy data pr kam krne k liye hote hain testing
purpose k liye
C:\xampp\htdocs\todoapp>php artisan make:factory TodoFactory
C:\xampp\htdocs\todoapp>php artisan make:seeder TodoSeeder

//jab hum factory aur seeder ki setting kr len ge tu phr ye command use kren ge
data ko db mai insert krne k liye
C:\xampp\htdocs\todoapp>php artisan db:seed

to enter in mysql through cmd

C:\xampp\htdocs\todoapp>mysql -u root

===================================================================================
================================

1-composer require laravel/ui


2-php artisan ui vue --auth
3-npm install && npm run dev

php artisan make:request CreateCategoryRequest

//we can validate data through it

===================================================================================
================================
Laravel functions

1 find($id) takes an id and returns a single model. If no matching model exist, it


returns null.
2 findOrFail($id) takes an id and returns a single model. If no matching model
exist, it throws an error1.

3 first() returns the first record found in the database. If no matching model
exist, it returns null.

4 firstOrFail() returns the first record found in the database. If no matching


model exist, it throws an error1.

5 get() returns a collection of models matching the query.

6 pluck($column) returns a collection of just the values in the given column. In


previous versions of Laravel this method was called lists.

7 toArray() converts the model/collection into a simple PHP array.

8 The in_array(search, array, type) function searches an array for a specific


value.

You might also like