Feartan Laravel mar a chruthaicheas agus a chleachdas tu feartan ann am pròiseact PHP Laravel 11
Un Feartan Laravel na sheata de dhòighean ath-chleachdadh a ghabhas a thoirt a-steach do chlasaichean Laravel.
Tha feartan a’ tabhann dòigh air còd a chuir còmhla agus ath-chleachdadh thar grunn chlasaichean gun chuingealachaidhean oighreachd.
Bidh iad a’ toirt seachad dòigh goireasach air comasan Laravel a leudachadh fhad ‘s a chumas iad còd glan, modular.
Ùine leughaidh tuairmseach: 2 minuti
Le bhith a’ toirt a-steach Traits, is urrainn dhuinn èifeachdas agus seasmhachd an tagraidh againn a leasachadh, a’ brosnachadh ath-chleachdadh còd agus dealachadh dhleastanasan taobh a-staigh frèam Laravel.
Cruthaich Trait
larla 11 chuir e òrdugh Artisan ùr ris gus feart àbhaisteach a chruthachadh. Chì sinn eisimpleir de Cùmhnant le dòighean air seilcheagan a ghineadh agus dearbhadh cho sònraichte ‘s a tha iad. Cleachdaidh sinn e ann am modal Post. Mar sin leig dhuinn sùil a thoirt air an eisimpleir shìmplidh ceum air cheum:
Faodaidh tu an àithne a leanas a chleachdadh gus fear ùr a chruthachadh Cùmhnant in larla 11.
php artisan make:trait {traitName}
Imrich agus Modail
Ann an stàladh de dhreach Laravel Framework 11, thèid sinn air adhart gus am Model Post a chruthachadh:
php artisan make:model Post -m
Bidh an àithne a rinn thu dìreach a’ cruthachadh an dà chuid an migration create_posts_table.php
che il model Post
. Rachamaid air adhart le bhith a’ gnàthachadh an imrich mar a leanas:
<?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('posts', function (Blueprint $table) {
$table->id();
$table->string('title');
$table->string('slug');
$table->text('content');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::dropIfExists('posts');
}
};
Feuch an tòisich sinn air an àithne imrich
php artisan migrate
agus an uairsin thèid sinn air adhart gu bhith a’ gnàthachadh an model
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use App\Traits\Sluggable;
class Post extends Model
{
use Sluggable;
/**
* Write code on Method
*
* @return response()
*/
protected $fillable = ['title', 'content', 'slug'];
/**
* Write code on Method
*
* @return response()
*/
public function setTitleAttribute($value)
{
$this->attributes['title'] = $value;
$this->attributes['slug'] = $this->generateSlug($value);
}
}
Cruthachadh Clas Trait
Cruthaichidh sinn an clas Sluggable.php agus bidh sinn a’ mìneachadh nan dòighean airson gineadh bhoiteag agus dearbhaich a h-aonachd.
Feuch an ruith sinn an àithne a leanas gus Trait ùr a chruthachadh:
php artisan make:trait Traits/Sluggable
bidh sinn a’ fosgladh am faidhle app/Traits/Sluggable.php agus a’ gnàthachadh a’ chòd mar a leanas:
<?php
namespace App\Traits;
trait Sluggable
{
/**
* Write code on Method
*
* @return response()
*/
public function generateSlug($string)
{
$slug = strtolower(str_replace(' ', '-', $string));
$count = 1;
while ($this->slugExists($slug)) {
$slug = strtolower(str_replace(' ', '-', $string)) . '-' . $count;
$count++;
}
return $slug;
}
/**
* Write code on Method
*
* @return response()
*/
protected function slugExists($slug)
{
return static::where('slug', $slug)->exists();
}
}
Cruthachadh Slighe
Cruthaichidh sinn Slighe airson deuchainn, leis an òrdugh a leanas:
<?php
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\PostController;
Route::get('/post', [PostController::class, 'index']);
Cruthachadh Rianadair
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\Post;
class PostController extends Controller
{
/**
* Write code on Method
*
* @return response()
*/
public function index(Request $request) {
$post = Post::create([
"title" => "Example Post",
"content" => "This is an example post.",
]);
$post2 = Post::create([
"title" => "Example Post",
"content" => "This is an example post 2.",
]);
$post3 = Post::create([
"title" => "Example Post",
"content" => "This is an example post 3.",
]);
dd($post->toArray(), $post2->toArray(), $post3->toArray());
}
}
Mu dheireadh, a’ leantainn slighe a’ phuist, cruthaichidh sinn na trì puist a gheibh an toradh toraidh a leanas:
Leughaidhean Co-cheangailte
Ercole Palmeri