vendredi 11 octobre 2019

Is there an easier way to code laravel eloquent models besides what I have here?

I created this code to work with laravel to access a database with many foreign keys, seems like I am subverting the foreign keys, am I missing something, was hoping someone could point me in the right direction

namespace App\Models\Entities;
use Illuminate\Database\Eloquent\Model;
class AbstractModel extends Model
{
    public function isDuplicate(Model $model) {
        return  $model::where($model->getAttributes())->first();
    }
}
///
namespace App\Models\Entities;
abstract class AbstractForeignModel extends AbstractModel {

    public $timestamps = false;
    public $fillable = ['value'];

    public function store($value){
        $foreign = $this->newInstance();
        $foreign->value = $value;
        if(!$this->isDuplicate($foreign)){
            $foreign->save();
        }
        return $foreign->getId($value);
    }

    public function setValueAttribute($value){
        $this->attributes['value'] = $value;
    }

    public function getId($value){
        $result =  self::where('value', $value)->first();
        if($result){
            return $result->id;
        }
    }
    public function getValue($id){
        $result = self::where('id', $id)->first();
        if($result){
           return $result->value; 
        }
    }
}
///
namespace App\Models\Entities\Video;
use App\Models\Entities\AbstractForeignModel;
class ForeignModel extends AbstractForeignModel {
    public function video() {
        return $this->belongsTo('App\Models\Entities\Video');
    }
}

Author, Description, Source, Title extend the above as empty classes


use App\Models\Entities\Video\Author;
use App\Models\Entities\Video\Description;
use App\Models\Entities\Video\Source;
use App\Models\Entities\Video\Title;
use Carbon\Carbon;
class Video extends AbstractModel {

    protected $fillable = ['author_id', 'title_id', 'description_id',
        'source_id', 'published_at'];

    public function store($data) {
        $video = new Video;
        $video->author_id = $data->author;
        $video->title_id = $data->title;
        $video->description_id = $data->description;
        $video->source_id = $data->source;
        $video->published_at = $data->published_at;
        if (!$this->isDuplicate($video)) {
            $video->save();
        }
    }

    public function setPublishedAtAttribute($value){
        $this->attributes['published_at'] = Carbon::parse($value)->toDateTimeString();
    }

    public function setTitleIdAttribute($value) {
        $this->attributes['title_id'] = (new Title)->store($value); 
    }

    public function setDescriptionIdAttribute($value) {
        $description = (new Description)->store($value);
        $this->attributes['description_id'] = $description;
    }

    public function setSourceIdAttribute($value) {
        $this->attributes['source_id'] =(new Source)->store($value);
    }

    public function setAuthorIdAttribute($value) {
        $this->attributes['author_id'] = (new Author)->store($value);
    }

    public function getAuthorIdAttribute($value) {
        $this->attributes['author_id'] = (new Author)->getValue($value);
    }

    public function getTitleIdAttribute($value) {
        $this->attributes['title_id'] = (new Title)->getValue($value);
    }

    public function getDescriptionAttribute($value) {
        $this->attributes['description_id'] = (new Description)->getValue($value);
    }

    public function getSourceIdAttribute($value) {
        $this->attributes['source_id'] = (new Source)->getValue($value);
    }

    public function author() {
        return $this->belongsTo('App\Models\Entities\Video\Author', 'author_id');
    }

    public function description() {
        return $this->belongsTo('App\Models\Entities\Video\Description', 'description_id');
    }

    public function title() {
        return $this->belongsTo('App\Models\Entities\Video\Title', 'title_id');
    }

    public function source() {
        return $this->belongsTo('App\Models\Entities\Video\Source', 'source_id');
    }
}

video migration file

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateVideosTable extends Migration {
   public function up() {
        Schema::create('videos', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->unsignedBigInteger('source_id');
            $table->unsignedBigInteger('title_id');
            $table->unsignedBigInteger('description_id');
            $table->unsignedBigInteger('author_id');
            $table->dateTimeTz('published_at');
            $table->timestamps();
            $table->foreign('source_id')->references('id')->on('sources');
            $table->foreign('title_id')->references('id')->on('titles');
            $table->foreign('description_id')->references('id')->on('descriptions');
            $table->foreign('author_id')->references('id')->on('authors');
        });
    }

    public function down() {
        Schema::dropIfExists('videos');
    }
}

The foreign key migration files follow this layout for Author, Description, Source, Title

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateTitlesTable extends Migration
{
    public $timestamps = false;
    public function up()
    {
        Schema::create('titles', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->string('value');
        });
    }
    public function down()
    {
        Schema::dropIfExists('titles');
    }
}

could probably create a foreign migration class and just set the variables that I need

It works, but I think I am overdoing it and missing some eloquent shortcuts.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire