- Razer is a PHP Framework that provides a convenient MVC (Model, View Controller) structure for systems development.
- Do you want something small but yet powerfull, this is your best choice
// 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();
- You can extend the Model class to you have you model called on its corresponding table name.
- The Model name should be singular and the table name in plural form
- The Eloquent model will convert your model name from singular to plural before querying the model objects.
$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();
- Only queries with bindings are executed with the query method
Create Database: | php manage make:db if the database name is not specified in the .env configurations, use php manage make:db dbname
Make Migration: | php manage make:migration create_migration_name This will create a migrations file under database/migrations directory. (tables names should be in a plural form)
Migration a specific file: | php manage migrate --file=filename This will run migations for a single file. (do not put the file extension)
Group Migrations into 1 sql file | php manage migrate:group All migration files will be grouped into one sql file
Make Controller: | php manage make:controller ControllerName
- //in singular Will create a controller under app/Controller/
- The resource controller is created with methods, index, create, store, show, edit, update, and destroy
Make a Resource Controller: | php manage make:controller ControllerName --resource
- // Creates a resource controller with CRUD methods
Make Model: | php manage make:model ModelName
- // in singular Creates a model under app/Models
Make Model and its migration: | php manage make:model -M ModelName
- // in singular
- For more information or inquiries, please call
- +256 754438448 OR
- Email godwintumuhimbise96@gmail.com
- All methods that receive data through an HTTP POST request should have a $request paramater
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.
- Both methods have 2 parameters
- status --> http status code. Supported status codes are 200,202,302, 400, 401, 402, 403, 404, 408, 422, 500, 502
- Message --> text / json reponse to send.
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
redirect()->back();
- The Routes class has 6 methods, get, post, group, except, name. and resource
- The get and post methods takes 2 arguements, $url (the url to go to) and $callback, an array of controller name and its method
- Simple get route
Route::get('user/profile', [UserController::class, 'userProfile']); - Simple get route
Route::post('user/profile', [UserController::class, 'userProfile']); - Takes in 2 arguments, $prefix (array), $callback (closure)
Route::group(['prefix' => 'admin', function(){ Route::get('/dashboard', [AdminController::class, 'index']); });in the template ` <a href='{{ url('admin/dashboard') }}'>Dashboard- This Route method is used to create routes for resource controllers
Route::resource('products', ProductController::class);// products is the prefix of the route- The above creates the following routes
- /products (GET)
- /products (PUT)
- /products/create (GET)
- /products/product_id (GET)
- /products/product_id/edit (GET)
- /products/product_id (DELETE)
- /products/store (POST)
- You can call the except method to ignore the specified class methods when creating routes
Route::resource('products', ProductController::class)->except(['destroy']); - On top of get and post methods, you can call the name method and register a short route name to use. This name must be used within the route method
- In routes
Route::post('user/profile', [UserController::class, 'userProfile'])->name('user.p'); - In the template
<a href='{{ route('user.p') }}'>Dashboard</a>// takes you to user/profile