Web Programming (Laravel)


here are the mid exam materialsWP Mid Exam Materialsuts-kisi.jpg webutsnotes.jpg Status: #idea Tags: web-programming References

I understand it NOWI UNDERSTAND IT NOW (WP)ah-ok.gif CTRL P to search for files in vsc run artisan make:model Post -m -s -f run artisan make:model Category -m -s -f run artisan make:model Writer -m -s -f run php artisan make:controller PostController --resource run php artisan make:controller CategoryController --resource run php artisan make:controller WriterController --resource Route edit web.php // web.php Route::resource('/category', CategoryController::class); Route::resource('/post', PostController::class); Route::resource('

Final Exam Materials

Mid Exam Materials

Initialization

composer create-project laravel/laravel newProject

not necessary since no wifi and project might be already given

Database

Setup Database

  1. Open .env file and look for DB_.. and make sure its the same as this
DB_CONNECTION=mysql // XAMPP runs mysql database engine
DB_HOST=127.0.0.1 
DB_PORT=3306 // port for database
DB_DATABASE=laravelproject // make a new database name 
DB_USERNAME=root // credentials for XAMPP
DB_PASSWORD= // empty by default by XAMPP

Open XAMPP and turn on Apache Web Server and MySQL Database

Create Tables

MigrationMigrationfiles used to design the database schema https://laravel.com/docs/11.x/migrations#main-content Status: #idea Tags: web-programming References
  1. Run the code below to generate migration files
php artisan make:migration create_[class_name]_table --create=[class_name]
// php artisan make:migration create_products_table --create=products
// php artisan make:migration create_customer_product_table --create=customer_product
// use --create for old laravel
  1. The file we will have to modify will look like this:
// xxxx_xx_xx_xxxxxx_create_[class_names]_table.php
public function up(){ 
	Schema::create('[class_names]', function (Blueprint $table) {
		$table->id();
		$table->string('[column_name]'); // custom column
		$table->integer('[column_name]'); // custom column
		$table->foreignId('[column_name]')->constrained(); // foreignID
		$table->timestamps();
	});
}

// example: 2024_10_13_021928_create_reviews_table.php
public function up(): void{
	Schema::create('reviews', function (Blueprint $table) {
		$table->id();
		$table->text('review');
		$table->unsignedTinyInteger('rating');
		$table->timestamps();
		$table->foreignId('book_id')->constrained()->cascadeOnDelete() ;
	});
}

what is constrained()constrained()The constrained() method is a shorthand in Laravel's migration schema builder that helps you quickly define a foreign key constraint for a column. $table->foreignId('column_name')->constrained('table_name'); * Automatically creates a foreign key constraint on the specified column. * Infers the referenced table if not explicitly provided. For example, user_id would refer to the users table by default. * Assumes the foreign key references the id column of the referenced table. Example Schema: and cascadeOnDelete()cascadeOnDelete()The cascadeOnDelete() method is a convenient way to define cascading behavior for foreign keys when a referenced record is deleted. Behavior * Automatically deletes the rows in the child table if the related row in the parent table is deleted. Example Schema::create('posts', function (Blueprint $table) { $table->id(); $table->foreignId('user_id')->constrained()->cascadeOnDelete(); $table->timestamps(); }); This ensures that if a user is deleted, all their related posts will als?

  1. After creating and modifying all migration files run php artisan migrate and your tables will be created 400

    400

SeederSeederfiles used to populate the table can be populated using class seeder and database seeder or factory and database seeder https://laravel.com/docs/11.x/database-testing#running-seeders There are 3 ways to insert data: DB RAW -> Manual Query // DB::raw("INSERT INTO USER BLABLABLA) // DB::raw("SELECT * FROM USERS) // 2. -> Query Builder -> Built in laravel // DB::table("users)->insert(['name'=> blabal]) // DB::table('users')->get(); // 3. -> El
  1. Make seeder for each model usingphp artisan make:seeder [Class_Name (UpperCase)]Seeder

  2. Fill run() method for class seeder

Using FakerFaker1. faker (lowercase) * In Laravel, faker is often used as a property in factories or test cases, and it provides access to an instance of the Faker class. * Laravel automatically resolves faker for you in many cases, such as in factory definitions or PHPUnit tests. * Example in a factory: 'name' => $this->faker->name, * It is a shorthand for using the Faker library in Laravel. 2. Faker (uppercase) * Faker is the actual class provided by the Faker library. * It's a PHP library used to ge:

// example class seeder: CustomerSeeder.php
public function run(){ 
	$faker = Faker::create('id_ID');
	for($i = 1;$i <= 10;$i++){
		DB::table('customers')->insert([
			'name' => $faker->name,
			'email' => $faker->email,
		]);
	}
}

400

Manually:

// example class seeder: ProductSeeder.php
public function run(){
	DB::table(table: 'products')->insert([
		['productname' => 'Beng Beng',
		'price' => 2000,
		'image' => 'chocolate.jpg',
		'category_id' => 1],
		
		['productname' => 'Chitato 100gr',
		'price' => 12000,
		'image' => 'chocolate.jpg',
		'category_id' => 2],
		
		['productname' => 'Yakult',
		'price' => 2000,
		'image' => 'chocolate.jpg',
		'category_id' => 4],
	]);
}
  1. Fill run() method for database seeder
// DatabaseSeeder.php
public function run(): void{
	$this->call([
		ProductSeeder::class,
	]);
}
  1. Execute the seeder using php artisan db:seed
ModelModelfiles used to define the relationships between tables https://laravel.com/docs/11.x/eloquent Status: #idea Tags: web-programming References
  1. Create models for each table using php artisan make:model [Name (Uppercase)] and it will be located at app\Models

  2. Define relationships by adding the relationship class as a method to each models

// relationship_class() in a Model
public function [relationship_class](){ // inside a model
	return $this->belongsToMany([Relationship_class]::class); // 1:N
	return $this->hasMany([Relationship_class]::class); // 1:N
}

// Category.php
class Category extends Model{
    use HasFactory;
    public function products(){
        return $this->hasMany(Product::class); // 1:N
    }
}

// Product.php
class Product extends Model{
    use HasFactory;
    public function category(){
        return $this->belongsTo(Category::class); // 1:1
    }
    public function customers(){
        return $this->belongsToMany(Customer::class); // 1:N
    }
}

// Customer.php
class Customer extends Model{
    use HasFactory;
    public function products(){
        return $this->belongsToMany(Product::class); // 1:N
    }
    public function idcard(){
        return $this->hasOne(Idcard::class); // 1:1
    }
}

Available relationship functions:

  • hasMany()
  • belongsTo()
  • belongsToMany()
  • hasOne()
  • ...

Cheats

Model WITH Migration Seeder Controller

CAN BE MADE WITH ONE LINE USING MODEL -M -S -C

php artisan make:model [ClassName] -m -s -c
php artisan make:model Clothes -m -s -c

Running this command would create the following files:

  • app/Models/Clothes.php: The model file.
  • database/migrations/xxxx_xx_xx_xxxxxx_create_clothes_table.php: The migration file to define the schema of the clothes table.
  • database/seeders/ClothesSeeder.php: The seeder file for populating the clothes table.
  • app/Http/Controllers/ClothesController.php: The controller file to manage CRUD operations for the Clothes model.
Migrate and Seed at Once

php artisan migrate:fresh --seed basically resets everything and populates it

Frontend

ControllerControllersBasically does the logic and connects view with the model https://laravel.com/docs/11.x/controllers Default php artisan help make:controller [ClassName]Controller will not include any methods in the controller file Resource --resource php artisan make:controller PhotoController --resource will a method for each of the available resource operations in the container file // these methods will be generated in the controller public function index() //Display a listing of the resource
  1. Create controller using php artisan make:controller [Class_name]Controller and it will be located at app\Http\Controllers
  2. Add method in the controller to query and pass into view
class TransactionController extends Controller{
    public function category(){
        $categories = Category::all(); // query all
        return view('category',compact('categories'));
        // return view("home", ["categories"=> $categories]); 
        // the same used if variable names are diff or duplicate names exist
    }
}
RouterRouterUsed to set up endpoints which execute the function of the controller https://laravel.com/docs/11.x/routing#main-content Status: #idea Tags: web-programming References
  1. Add controller function to web.php (Router) in \routes
Route:get('\endpoint', [Controller::class, 'function']); // template
Route::get('oneToMany',[TransactionController::class,'category']);
Route::get('manyToMany',[TransactionController::class,'customerTransaction']);
ViewsViewsFront end html css. What the users interact with. UI elements. https://laravel.com/docs/11.x/views Status: #idea Tags: web-programming References
  1. Recommended: Make a layout template inside \resources\views\ add a \layouts folder filled with app.blade.php
// example layouts\app.blade.php
<!DOCTYPE html>
	<html lang="en">
	
	<head>
	  <meta charset="UTF-8">
	  <title>Example</title> 
	</head>
	
	<body>
	  <h1>@yield('title')</h1> // section for the title
	  @yield('content') // section for the content
	</body>
</html>

@yield() used to create a section

// example of blade.php file that extends layouts\app.blade.php
@extends('layouts.app') // EXTENDS 

@section('title', 'The list of tasks') // one line use of section

@section('content')
    <nav>
        <a href="{{ route('tasks.create') }}">Add Task</a>
    </nav>
    @forelse ($tasks as $task)
        <div>
            <a href="{{ route('tasks.show', ['task' => $task->id]) }}" @class(['line-through' => $task->completed])>{{ $task->title }}</a>
        </div>
    @empty
        <div>There are no tasks!</div>
    @endforelse
    @if ($tasks->count())
        <nav>
            {{ $tasks->links() }}
        </nav>
    @endif
@endsection // use end section if not one line

Use ! to generate HTML template

BootstrapBootstrapCSS Framework Status: #idea Tags: web-programming References
  1. Locate Bootstrap Folder
  2. Pick the folder with the version according to the requirement
  3. Find \css folder and copy
  4. Paste \css folder to \public folder
  5. Inside <head></head> tag add link to Bootstrap using asset() and rel="stylesheet"
<head>
	<title>
		Table Relation
	</title>
	<link href="{{asset('css/bootstrap.min.css')}}" rel="stylesheet" >
</head>
  1. Add code in view file linked to controller to show queried results
@extends('layouts.app')

@section('title', 'Product Category') // one line use of section

@section('content')
	<ul>
		@forelse ($categories as $category) // for each category
		   <li>{{$category->category}}</li>  // show category name
		   <ul>
				@forelse ($category->products as $product) 
				// for each products in the category
				<li>
					{{$product->productname}} 
					// show product name
					<img src="{{asset($product->image)}}">
					// show product image
				</li>
				@empty
					<h4>No product found..</h4> // if category has no products
				@endforelse
		   </ul>
		@empty
			<h2>No category..</h2> // there are no categories
		@endforelse
	</ul>
@endsection 
  1. Previously we tried simulate the one to many relationship and below is the example for many to many relationship
PaginatePaginateuse simplePaginate() instead of all() and add links() at the bottom Status: #idea Tags: web-programming References
class TransactionController extends Controller{
    public function customerTransaction(){
        $customers = Customer::simplePaginate(5); 
        return view('customer',compact('customers'));
    }
}
@extends('layouts.app')

@section('title', 'Customer Transaction') // one line use of section

@section('content')
	<ul>
        @forelse ($customers as $customer)
            <li>{{ $customer->name }} {{ $customer->email }}</li>
        @empty
            <h2>No customers..</h2>
        @endforelse
    </ul>
    <h2>Transacation</h2>
    <ul>
        @forelse ($customers as $customer)
            <li>{{ $customer->name }}</li>
            <ul>
                @forelse ($customer->products as $product)
                    <li>{{ $product->productname }} {{ $product->price }}</li>
                @empty
                    <h4>No transaction..</h4>
                @endforelse
            </ul>
        @empty
            <h2>No customers..</h2>
        @endforelse
    </ul>
    {{ $customers->links() }} // for pagination links
@endsection 

Check out my Github repo for example implementation: https://github.com/far1h/utsProject

Check commits for step by step process

In case necessary: Forms-Simple-CreateSimple Form Implementation1. Edit .env DB_CONNECTION=sqlite DB_HOST=127.0.0.1 DB_PORT=3306 DB_DATABASE=FoodDB DB_USERNAME=root DB_PASSWORD= 2. Run php artisan make:model Food -m -s 2. Run php artisan make:model FoodCategory -m -s i want a resource controller so without -c 3. Edit migration file and run php artisan migrate // 2024_11_10_094235_create_food_table.php public function up(): void { Schema::create('food', function (Blueprint $table) { $table->id(); $table->string('name'); $table->integer('price');

Status: #MOC
Tags: