tutorial101 is the one place for high quality web development, Web Design and software development tutorials and Resources programming. Learn cutting edge techniques in web development, design and software development, download source components and participate in the community.
A new file named Employee.php will be created in the app directory and database/migrations directory to generate the table in our database
app/Models/Employee.php
//app/Models/Employee.php
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Employee extends Model
{
use HasFactory;
protected $fillable = [
'first_name',
'last_name',
'email',
'avatar'
];
}
database\migrations\create_employees_table.php
//database\migrations\create_employees_table.php
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::create('employees', function (Blueprint $table) {
$table->id();
$table->string('first_name');
$table->string('last_name');
$table->string('email');
$table->string('avatar');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::dropIfExists('employees');
}
};
Database Migration
php artisan migrate
C:\xampp\htdocs\laravel\laravel10project>php artisan migrate
Migration table created successfully.
Datatables
DataTables is a plug-in for the jQuery Javascript library. It is a highly flexible tool, built upon the foundations of progressive enhancement, that adds all of these advanced features to any HTML table.
https://datatables.net/
sweetalert2
A BEAUTIFUL, RESPONSIVE, CUSTOMIZABLE, ACCESSIBLE (WAI-ARIA) REPLACEMENT FOR JAVASCRIPT'S POPUP BOXES
https://sweetalert2.github.io/
A new file named Product.php will be created in the app directory and database/migrations directory to generate the table in our database
app/Models/Product.php
//
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Product extends Model
{
use HasFactory;
protected $fillable = ['name', 'image', 'description', 'price', 'sold', 'user_id'];
public function seller(){
return $this->belongsTo(User::class, 'user_id');
}
}
database\migrations\create_products_table.php
//database\migrations\create_products_table.php
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::create('products', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->string('image');
$table->text('description');
$table->bigInteger('price');
$table->boolean('sold')->default(false);
$table->bigInteger('user_id');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::dropIfExists('products');
}
};
//app/Models/ProductSold.php
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class ProductSold extends Model
{
use HasFactory;
protected $table = 'product_solds';
protected $fillable = ['product_id', 'buyer_id'];
public function detail(){
return $this->belongsTo(Product::class, 'product_id');
}
}
//database\migrations\create_product_solds_table.php
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::create('product_solds', function (Blueprint $table) {
$table->id();
$table->bigInteger('product_id');
$table->bigInteger('buyer_id');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::dropIfExists('product_solds');
}
};
Update User Model and table database\migrations\create_users_table.php
//database\migrations\create_users_table.php
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::create('users', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->string('email')->unique();
$table->timestamp('email_verified_at')->nullable();
$table->string('password');
$table->string('username');
$table->rememberToken();
$table->timestamps();
});
}
/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::dropIfExists('users');
}
};
app/Models/User.php
//app/Models/User.php
<?php
namespace App\Models;
// use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Laravel\Sanctum\HasApiTokens;
class User extends Authenticatable
{
use HasApiTokens, HasFactory, Notifiable;
/**
* The attributes that are mass assignable.
*
* @var array<int, string>
*/
protected $fillable = [
'name',
'email',
'password',
'username',
];
/**
* The attributes that should be hidden for serialization.
*
* @var array<int, string>
*/
protected $hidden = [
'password',
'remember_token',
];
/**
* The attributes that should be cast.
*
* @var array<string, string>
*/
protected $casts = [
'email_verified_at' => 'datetime',
];
public function purchases(){
return $this->hasMany(ProductSold::class, 'buyer_id')->orderBy('created_at', 'DESC');
}
}
Database Migration
php artisan migrate
C:\xampp\htdocs\laravel\laravel10project>php artisan migrate
Migration table created successfully.
//app/Http/Controllers/AuthController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\User;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Hash;
class AuthController extends Controller
{
public function login(){
if(Auth::check()){
return redirect()->route('product.index');
}
return view('auth.login');
}
public function post(Request $request){
$credentials = [
'username' => $request->username,
'password' => $request->password
];
if(Auth::attempt($credentials)){
return redirect()->route('product.index');
}
return back()->with('error', 'Incorrect User User Name or Password');
}
public function register(){
if(Auth::check()){
return redirect()->route('product.index');
}
return view('auth.register');
}
public function postRegister(Request $request){
$check_email = User::where('email', $request->email)->first();
if($check_email){
return back()->with('error', 'Email already in use');
}
$user = User::create([
'username' => $request->username,
'name' => $request->name,
'email' => $request->email,
'password' => Hash::make($request->password)
]);
$credentials = [
'username' => $user->username,
'password' => $request->password,
];
Auth::attempt($credentials);
return redirect()->route('profile.index')->with('success', 'Congratulations, your account can be used! After exiting, Login using User ID and Password');
}
public function logout(){
Auth::logout();
return redirect()->route('login');
}
}
//app/Http/Controllers/ProfileController.php
<?php
namespace App\Http\Controllers;
use App\Models\Product;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
class ProfileController extends Controller
{
public function index(){
return view('pages.profile');
}
public function purchase(){
return view('pages.purchase');
}
}
A new file named Product.php will be created in the app directory and database/migrations directory to generate the table in our database
app/Models/Product.php
//app/Models/Product.php
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Product extends Model
{
use HasFactory;
protected $fillable = ['product_name', 'photo', 'price', 'product_description'];
}
database\migrations\create_products_table.php
//database\migrations\create_products_table.php
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::create('products', function (Blueprint $table) {
$table->id();
$table->string('product_name');
$table->text('product_description');
$table->string('photo');
$table->string('price');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::dropIfExists('products');
}
};
Database Migration
php artisan migrate
C:\xampp\htdocs\laravel\laravel10project>php artisan migrate
Migration table created successfully.
//
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class StripeController extends Controller
{
public function checkout()
{
return view('checkout');
}
public function session(Request $request)
{
\Stripe\Stripe::setApiKey(config('stripe.sk'));
$productname = $request->get('productname');
$totalprice = $request->get('total');
$two0 = "00";
$total = "$totalprice$two0";
$session = \Stripe\Checkout\Session::create([
'line_items' => [
[
'price_data' => [
'currency' => 'USD',
'product_data' => [
"name" => $productname,
],
'unit_amount' => $total,
],
'quantity' => 1,
],
],
'mode' => 'payment',
'success_url' => route('success'),
'cancel_url' => route('checkout'),
]);
return redirect()->away($session->url);
}
public function success()
{
return "Thanks for you order You have just completed your payment. The seeler will reach out to you as soon as possible";
}
}