Category : laravel-models

i need to make the post accepts as many language as i need so i have tow models post and language in post model: public function languages(){ return $this->belongsToMany(AppModelsLanguage::class); } in my language model : public function posts() { return $this->belongsToMany(AppModelsPost::class); } post migration : Schema::create(‘posts’, function (Blueprint $table) { $table->id(); $table->string(‘title’); $table->text(‘body’); $table->boolean(‘puplished’)->default(false); $table->bigInteger(‘user_id’)->unsigned(); ..

Read more

trying to define a model mutators <?php namespace App; use IlluminateDatabaseEloquentModel; class Custom_fields extends Model { protected $table = ‘custom_fields’; public function set_contentAttribute($value){ return unserialize($value); } } accessing the field _content and unserialize the value of the field so I can get an array automatically on retrieve but seems not working. Any help, ideas? Source: ..

Read more

I have two models (let’s say TestModel_1 and TestModel_2), between which there is a relationship "hasMany" TestModel_1.php: protected $fillable = [‘par_id’, ‘par_attr_1’, ‘par_attr_2’]; public function parentAttr () { return $this->hasMany(‘AppModelsTestModel_1’, ‘par_id’, ‘child_id’); } TestModel_2.php: protected $fillable = [‘child_id’, ‘child_attr_1’, ‘child_attr_2’]; public function childAttr() { return $this->belongsTo(‘AppModelsTestModel_2’, ‘par_id’, ‘par_id’); } After determining the relationship between the ..

Read more

I have two models (let’s say TestModel_1 and TestModel_2), between which there is a relationship "hasMany" TestModel_1.php: protected $fillable = [‘par_id’, ‘par_attr_1’, ‘par_attr_2’]; public function parentAttr () { return $this->hasMany(‘AppModelsTestModel_1’, ‘par_id’, ‘child_id’); } TestModel_2.php: protected $fillable = [‘child_id’, ‘child_attr_1’, ‘child_attr_2’]; public function childAttr() { return $this->belongsTo(‘AppModelsTestModel_2’, ‘par_id’, ‘par_id’); } After determining the relationship between the ..

Read more

Good morning/afternoon/evening! I have 2 model, User and post User.php public function getRouteKeyName() { return ‘user_name’; } public function posts() { return $this->hasMany(Post::class, ‘by_id’, ‘id’); } I have two records in users table |——————————————–| | id | user_name | … | … | |——————————————–| | 1 | megamanx | … | … | | 1 ..

Read more

I have 2 models: Invoice and InvoiceItems and these models are connected with invoice_id: Schema::create(‘invoice_items’, function (Blueprint $table) { $table->bigInteger(‘invoice_id’)->unsigned(); $table->foreign(“invoice_id”)->references(‘id’)->on(‘invoices’)->onDelete(‘cascade’); }); Invoice : class Invoice extends Model { protected $fillable = [ ‘profile_id’, ‘invoice_number’, …. ]; public function invoiceItems() { return $this->hasMany(‘AppInvoiceItem’); } } And InvoiceItems class InvoiceItem extends Model { protected $guarded = ..

Read more