It consist some common operations like create, edit , delete, update with implementation of image upload and update. Sending Notifications To Telegram. Sending Mail.
$ composer create-project --prefer-dist laravel/laravel crud-api
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Route;
Route::apiResource('category', 'CategoryController');
Route::apiResource('product', 'ProductController');
$ php artisan make:model Category -mf
$ php artisan make:model Product -mf
<?php
namespace App;
use Carbon\Carbon;
use Illuminate\Database\Eloquent\Model;
class Category extends Model
{
protected $fillable = ['name'];
// change created_at & updated_at format using Carbon
public function getCreatedAtAttribute($attr)
{
return Carbon::parse($attr)->format('d.m.Y H:i');
}
public function getUpdatedAtAttribute($attr)
{
return Carbon::parse($attr)->format('d.m.Y H:i');
}
// Relationship one to many
public function product()
{
return $this->hasMany('App\Product');
}
}
namespace App;
use Carbon\Carbon;
use Illuminate\Database\Eloquent\Model;
class Product extends Model
{
protected $fillable = ['name','price','category_id'];
public function getCreatedAtAttribute($attr)
{
return Carbon::parse($attr)->format('d.m.Y H:i');
}
public function getUpdatedAtAttribute($attr)
{
return Carbon::parse($attr)->format('d.m.Y H:i');
}
//One To Many (Inverse)
public function category()
{
return $this->belongsTo('App\Category');
}
}
public function up()
{
Schema::create('categories', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('name');
$table->timestamps();
});
}
public function up()
{
Schema::create('products', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('image');
$table->string('name');
$table->double('price');
$table->timestamps();
$table->bigInteger('category_id')->unsigned();
$table->foreign('category_id')->references('id')->on('categories')->onDelete('cascade');
});
}
$ php artisan migrate
$factory->define(Category::class, function (Faker $faker) {
return [
"name" => $faker->word
];
});
use Illuminate\Database\Seeder;
use App\Category;
class CategoryTableSeeder extends Seeder
{
public function run()
{
factory(Category::Class, 10)->create();
}
}
$ php artisan db:seed
$ php artisan make:resource CategoryResource
$ php artisan make:resource CategoryResourceCollection --collection
$ php artisan make:resource ProductResource
$ php artisan make:resource ProductResourceCollection --collection
$ php artisan make:controller CategoryController
$ php artisan make:controller ProductController
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Category;
use App\Http\Resources\CategoryResource;
use App\Http\Resources\CategoryResourceCollection;
class CategoryController extends Controller
{
/*
* @return CategoryResource
*/
public function show(Category $category): CategoryResource{
return new CategoryResource($category);
}
/*
* @return CategoryResourceCollection
*/
public function index(): CategoryResourceCollection{
$category = Category::orderBy('id', 'desc')->get();
return new CategoryResourceCollection($category);
}
/*
* @return CategoryResource
*/
public function store(Request $request): CategoryResource{
$request->validate(['name' => 'required']);
$category = Category::create($request->all());
return(new CategoryResource($category));
}
/*
* @return CategoryResource
*/
public function update(Category $category, Request $request): CategoryResource{
$category->update($request->all());
return(new CategoryResource($category));
}
/*
* @return
*/
public function destroy(Category $category){
$category->delete();
return response()->json();
}
}
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Resources\ProductResource;
use App\Http\Resources\ProductResourceCollection;
use App\Product;
class ProductController extends Controller
{
/*
* @return ProductResource
*/
public function show(Product $product): ProductResource{
return new ProductResource($product);
}
/*
* @return ProductResourceCollection
*/
public function index(): ProductResourceCollection{
$product = Product::with('category')->orderBy('id', 'desc')->get();
return new ProductResourceCollection($product);
}
/*
* @return ProductResource
*/
public function store(Product $product, Request $request): ProductResource{
$request->validate([
'name' => 'required',
'price' => 'required',
'category_id' => 'required'
]);
//
if($request->hasFile('image'))
{
$image_name = time().'_'.rand(999,9999).'.'.$request->image->getClientOriginalExtension();
$request->image->move(public_path('images'), $image_name);
$product->image = 'http://localhost/crud-api/public/images/'.$image_name;
}else{
$product->image = 'http://localhost/crud-api/public/images/image-placeholder.png';
}
$product->name = $request->name;
$product->price = $request->price;
$product->category_id = $request->category_id;
$product->save();
return(new ProductResource($product));
}
/*
* @return ProductResource METHOD POST with Param _method PUT
https://url/id?_method=put
*/
public function update(Product $product, Request $request): ProductResource{
if($request->hasFile('image'))
{
$image_name = time().'_'.rand(999,9999).'.'.$request->image->getClientOriginalExtension();
$request->image->move(public_path('images'), $image_name);
$product->image = 'http://localhost/crud-api/public/images/'.$image_name;
}
$product->name = $request->name;
$product->price = $request->price;
$product->category_id = $request->category_id;
$product->save();
return(new ProductResource($product));
}
/*
* @return
*/
public function destroy(Product $product){
$product->delete();
return response()->json();
}
}
First create a telegram bot. To do this, send a message to @BotFather.
$ composer require irazasyed/telegram-bot-sdk
$ php artisan make:controller SendNotification
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Mail;
use Telegram\Bot\Laravel\Facades\Telegram;
class SendNotification extends Controller
{
//
public function ToTelegram(Request $request){
Telegram::sendMessage([
'chat_id' => env('TELEGRAM_CHANNEL_ID', '***************'),
'parse_mode' => 'HTML',
'text' => $request->text,
]);
}
}
Add entry to 'config/app.php'
'Telegram' => Telegram\Bot\Laravel\Facades\Telegram::class,
Publish configuration file
php artisan vendor:publish --provider="Telegram\Bot\Laravel\TelegramServiceProvider"
Add telegram token in 'config/telegram.php'
'bots' => [
'common' => [
'username' => 'username',
'token' => env('TELEGRAM_BOT_TOKEN', 'telegram token'),
'commands' => [
// Acme\Project\Commands\MyTelegramBot\BotCommand::class
],
],
use Illuminate\Support\Facades\Mail;
....
public function SendMail(Request $request){
$data = [
"email" => $request->email,
"msg" => $request->message
];
Mail::send([], [], function($message) use ($data) {
$message->from('example', 'name');
$message->to($data['email']);
$message->subject('New Message');
$message->setBody($data['message'], 'text/html');
});
}
Laravel is a web application framework with expressive, elegant syntax. We believe development must be an enjoyable and creative experience to be truly fulfilling. Laravel takes the pain out of development by easing common tasks used in many web projects, such as:
- Simple, fast routing engine.
- Powerful dependency injection container.
- Multiple back-ends for session and cache storage.
- Expressive, intuitive database ORM.
- Database agnostic schema migrations.
- Robust background job processing.
- Real-time event broadcasting.
Laravel is accessible, powerful, and provides tools required for large, robust applications.
Laravel has the most extensive and thorough documentation and video tutorial library of all modern web application frameworks, making it a breeze to get started with the framework.
If you don't feel like reading, Laracasts can help. Laracasts contains over 1500 video tutorials on a range of topics including Laravel, modern PHP, unit testing, and JavaScript. Boost your skills by digging into our comprehensive video library.
We would like to extend our thanks to the following sponsors for funding Laravel development. If you are interested in becoming a sponsor, please visit the Laravel Patreon page.
- Vehikl
- Tighten Co.
- Kirschbaum Development Group
- 64 Robots
- Cubet Techno Labs
- Cyber-Duck
- British Software Development
- Webdock, Fast VPS Hosting
- DevSquad
- UserInsights
- Fragrantica
- SOFTonSOFA
- User10
- Soumettre.fr
- CodeBrisk
- 1Forge
- TECPRESSO
- Runtime Converter
- WebL'Agence
- Invoice Ninja
- iMi digital
- Earthlink
- Steadfast Collective
- We Are The Robots Inc.
- Understand.io
- Abdel Elrafa
- Hyper Host
- Appoly
- OP.GG
Thank you for considering contributing to the Laravel framework! The contribution guide can be found in the Laravel documentation.
In order to ensure that the Laravel community is welcoming to all, please review and abide by the Code of Conduct.
If you discover a security vulnerability within Laravel, please send an e-mail to Taylor Otwell via taylor@laravel.com. All security vulnerabilities will be promptly addressed.
The Laravel framework is open-sourced software licensed under the MIT license.