Amfani Eloquent ORM don hulɗar Database da Ayyukan CRUD

Eloquent mai ƙarfi ne Object-Relational Mapping(ORM) haɗa cikin Laravel. Yana ba da hanya mai sauƙi da dacewa don yin hulɗa tare da bayanan bayanai da aiwatar da ayyukan CRUD(Ƙirƙiri, Karanta, Sabuntawa, Share). Anan akwai jagora akan amfani a: Eloquent ORM Laravel

 

Ƙayyade da Model

Da farko, kuna buƙatar ayyana model taswirorin zuwa tebur a cikin bayanan. Misali, idan kuna da tebur "masu amfani", zaku iya ƙirƙirar "User" model ta amfani da umarnin Artisan:

php artisan make:model User

 

Yi hulɗa da Bayanai

Kuna iya amfani da hanyoyi a cikin model don yin hulɗa tare da bayanan.

  • Ƙirƙiri sabon rikodin:
    $user = new User;  
    $user->name = 'John Doe';  
    $user->email = '[email protected]';  
    $user->save();  
    ​
  • Mai da duk bayanan:
    $users = User::all();
  • Maido rikodin bisa maɓalli na farko:
    $user = User::find($id);​
  • Sabunta rikodin:
    $user = User::find($id);  
    $user->name = 'Jane Doe';  
    $user->save();
  • Share rikodin:
    $user = User::find($id);  
    $user->delete();  
    

 

Model Dangantaka

Eloquent yana ba ku damar ayyana alaƙa tsakanin model s. Kuna iya ayyana alaƙa kamar "na", "yana da yawa", "hasOne", da sauransu don yin hulɗa tare da bayanai ta hanyar ƙungiyoyi. Wannan yana ba ku damar yin tambaya cikin sauƙi da sarrafa alaƙa tsakanin teburi a cikin bayanan.

 

Keɓance Tambaya

Eloquent yana ba da hanyoyi da yawa don keɓance tambayoyi da tace bayanai. Kuna iya amfani da hanyoyi kamar where, orderBy, groupBy, da sauransu don yin hadaddun tambayoyi da kuma dawo da bayanai dangane da bukatunku.

 

Yin amfani da ciki yana ba ku damar yin hulɗa tare da bayanan bayanai cikin sauƙi da inganci. Yana rage buƙatar rubuta ƙananan tambayoyin SQL kuma yana ba da hanyoyi masu dacewa don aiki tare da bayanai. Eloquent ORM Laravel