Инструменты пользователя

Инструменты сайта


works:programmer:php:eloquent-for-dummies

Eloquent для чайников

use Illuminate\Database\Eloquent\Model;
 
class Post extends Model
{
      /**
      * Get the publisher for this model.
      */
      public function publisher()
      {
          # ( $modelClass , $foreign_key , $object_id )
          return $this->belongsTo(User::class, 'user_id', 'id');
      }
}

Расширяем возможности builder'ов

namespace App\QueryBuilders;
 
use App\Models\User;
use Carbon\CarbonImmutable;
use Illuminate\Database\Eloquent\Builder;
 
/**
 * @template TModelClass
 * @extends Builder<TModelClass>
 */
class ChatLineQueryBuilder extends Builder
{
    public function forToday(): self
    {
        return $this->whereDay('day', CarbonImmutable::now());
    }
 
    public function forUser(User $user): self
    {
        return $this->where('user_id', $user->id);
    }
}
works/programmer/php/eloquent-for-dummies.txt · Последнее изменение: 2023/07/06 06:52 — 127.0.0.1

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki