// For many, supply an array of arrays of data.
DB::table('table_name')->save($data);
DB::lastId();
DB::table('table_name')->where('id', 1)->update($data);
DB::table('table_name')->where('id', 1)->delete();
DB::table('table_name')->delete();
DB::affectedRows();
DB::table('table_name')->get(); // returns all columns
$columns = 'column1, column2, column3 columnn' //as a string
OR
$columns = ['column1', 'column2', 'column3', 'columnn'] // as an array
DB::table('table_name')->get($columns);
DB::table('table_name')->where('id', 1)->get();
// multiple call to the where method creates WHERE AND AND AND ...
// call orWhere to and an OR
DB::table('table_name')->where('id', 1) ->where('age', 20 , '>')->where('gender', 'Male')->get();
DB::table('table_name')->row()->where('id', 1)->get();
OR
DB::table('table_name')->find(1); // default column name is 'id'
DB::table('table_name')->where('id', 1)->value();
DB::table('table_name')->where('id', 1)->count();
DB::table('table_name')->count(); // all rows without a condition
DB::table('table_name')->where('id', 1)->max();
DB::table('table_name')->where('id', 1)->min();
DB::table('table_name')->where('id', 1)->avg();
DB::table('table_name')->distinct()->get(); // supply columns if not all
// distinct with a condition
DB::table('table_name')->distinct()->where('id', 1)->get();
DB::table('table_name')->join('table2', 'table1.primary', 'table2.foregin')->get();
// call the join method multiple times to join mutliple tables using INNER JOIN.
Other options of join methods include
leftJoin(), rightJoin,() unionJoin()
DB::table('table_name')->between('age', 20, 25)->get();
DB::table('table_name')->range(1, 25)->get();
DB::table('table_name')->where('id', 1)->exits(); // retuns true if exists
DB::table('table_name')->where('id', 1)->doesNotExist(); // oposite of exist
Database::switchTo('database_name');
// start querying from here
DB::table('table_name')->use('database_name', 'table')->get();
$interns = new Intern($data); $interns->save();
$interns->affectedRows();
$interns->lastId();
$interns = new Intern(); $interns->name = "Godwin"; $interns->age = 20; $inters->save();
Intern::find(1)->update($data);
Intern::find(5)->delete();
Intern::all(); // same as DB::table('interns')->get();
Intern::where('id', 1)->get(); // same DB::table('interns')->where('id', 1)->get();
Intern::find(1);
Intern::with('course')->get(); // this will assume that the interns and courses table use the Id column as its primary key, forming INNER JOIN courses ON interns.course_id = courses.id
Interns::with('course')->join('supervisor', 'interns.supervisor_id', 'supervisor.id')->get();
DB::query('SELETE * FROM interns WHERE age > ?')->bindings([20])->get();
php manage make:db if the database name is not specified in the .env configurations, use php manage make:db dbnamephp manage make:migration create_migration_name This will create a migrations file under database/migrations directory. (tables names should be in a plural form)php manage migrate This will run all migrationsphp manage migrate --file=filename This will run migations for a single file. (do not put the file extension)php manage migrate:group All migration files will be grouped into one sql filephp mange migrate:group --run This will run grouped migrationsphp manage migrate:modifiy This will run migration modificationsphp manage migrate:list Lists all run migrationsphp manage migrate:rollback Rolls back migrationsphp manage migrate:refresh Rools back and re-runs migrationsphp manage migrate:log Logs Migrations errorsphp manage migrate:log --clear Clears migrations errorsMake Controller: | php manage make:controller ControllerName
Make a Resource Controller: | php manage make:controller ControllerName --resource
Make Model: | php manage make:model ModelName
Make Model and its migration: | php manage make:model -M ModelName
public function saveUser(Request $request) { $name = $request->post('name'); // get the value of name sent through an HTTP POST echo $name; }
// OR
public function saveUser(Request $request) { $name = $request->name; //dynamically assigned properties the Request Class echo $name; }
// OR
public function saveUser(Request $request) { $name = $request->body->name; //dynamically assigned properties the Request Class echo $name; }
public function saveUser(Request $request) { $name = $request->get('name'); // get the value of name sent through an HTTP GET echo $name; }
// OR
public function saveUser(Request $request) { $name = $request->name; echo $name; }
// OR
public function saveUser(Request $request) { $name = $request->params->name; echo $name; }
The response class has 2 methods, ie send and json. Send() send a plain text response while json send a json formated respeonse.
public function login(Request $request) { $email = $request->post('email'); return response()->send(200, $email); }
public function login(Request $request) { $email = $request->post('email'); return response()->json(200, $email); // can be received through the message property }
redirect('user/dashboard');
redirect()->back();Route::get('user/profile', [UserController::class, 'userProfile']);Route::post('user/profile', [UserController::class, 'userProfile']);Route::group(['prefix' => 'admin', function(){ Route::get('/dashboard', [AdminController::class, 'index']); }); in the template ` <a href='{{ url('admin/dashboard') }}'>DashboardRoute::resource('products', ProductController::class); // products is the prefix of the routeRoute::resource('products', ProductController::class)->except(['destroy']);Route::post('user/profile', [UserController::class, 'userProfile'])->name('user.p'); <a href='{{ route('user.p') }}'>Dashboard</a> // takes you to user/profile