Xây dựng trang quản trị laravel 9 với Laravel-admin bằng demo 1 trang blog cá nhân phần 7 : Chú ý những vấn đề Rules unique update và listbox ,hasmany ,Danh mục cha
1 Rules unique
Chúng ta tiếp theo sẽ lưu ý phần tiếp theo đó chính là rules các dữ liệu khi update không được trùng lặp nhau với cú pháp
$form->text('slug', __('admin.Slug'))->updateRules(['unique:posts,slug,{{id}}']);
2 Listbox
Riêng phần listbox thì dữ liệu dưới dạng string
$table->string('postrelate','200')->comment('danh sách bài viết liên quan')->nullable();
Trong phần model chúng ta viết 2 function get_tencot_Attribute và set_tencot_Attribute để sử dụng .Cực kì lưu ý chỗ này nếu không cho vào Model thì phần listbox sẽ chạy sai nhé
public function getPostrelateAttribute($value)
{
return explode(',', $value);
}
public function setPostrelateAttribute($value){
$this->attributes['postrelate'] = implode(',', $value);
}
Trong phần controller chúng ta code như sau :
$form->listbox('postrelate','Bài viết liên quan')->options(function (){
$relate = Post::all()->pluck('title','id');
return $relate;
});
Vậy là xong listbox nhé .Tiếp theo chúng ta sẽ đến với phần tiếp theo đó chính là hasMany
3 Form Hasmany
Đầu tiên chúng ta cần xác định vấn đề như sau : Chúng ta có 2 bảng dữ liệu 1 là Category 2 là Post .Mối quan hệ giữa 2 bảng này là 1 Category có nhiều Post và mỗi Post thuộc 1 category
Trong model chúng ta xác định như sau :
Category Model
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
class Category extends Model
{
use HasFactory;
use SoftDeletes;
protected $fillable=['title','slug','parent_id','description','tags'];
public function post(){
return $this->hasMany(Post::class,'category_id','id');
}
public function setTagsAttribute($value){
$this->attributes['tags'] = implode(',', $value);
}
}
Post Model :
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
class Post extends Model
{
use HasFactory;
use SoftDeletes;
protected $fillable=['title','slug','description','content','category_id','thumbnail','video','type','status','postrelate'];
public function category()
{
return $this->belongsTo(Category::class,'category_id','id');
}
public function getPostrelateAttribute($value)
{
return explode(',', $value);
}
public function setPostrelateAttribute($value){
$this->attributes['postrelate'] = implode(',', $value);
}
}
Vậy tiếp theo chúng ta sẽ code trong mục form như sau :
if($form->isCreating()){
$form->hasMany('post','Bài viết',function (Form\NestedForm $form){
$form->text('title','Tiêu đề');
$form->text('slug','Đường dẫn');
$form->text('content');
$form->image('thumbnail');
});
}
Ở đây khi tạo thì mới có chức năng tạo các bài viết còn khi update thì không
Vậy là xong phần hasmany
4 Danh mục cha
Khi tạo danh mục chúng ta thường xác định danh mục cha của 1 danh mục .Dưới đây là 1 cách cơ bản ,tuy hơi sơ sài và fix lỗi chưa kĩ nhưng các bạn có thể xem qua
$form->select('parent_id', __('Danh mục cha'))->options(function (){
//$a = Category::all()->pluck('title','id');
$a = Category::where([['id','!=',$this->id],['parent_id','!=',$this->id]])->pluck('title','id');
$a->prepend('ROOT',0);
// $a->forget($this->id);
return $a;
});