I have a model with softdeletes, if I add A appends field my sofdeletes implementation doesnt work, somebody knows what is happend?
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
class Users extends Model
{
use SoftDeletes;
protected $dates = ['deleted_at'];
protected $table = 'users';
protected $appends = ['assigned'];
protected $fillable = ['category_id'];
public function __construct()
{
$this->assigned = false;
}
public function setAssignedAttribute($value)
{
$this->attributes['assigned'] = $value;
}
public function getAssignedAttribute($value)
{
return $value;
}
}
It's not your appends field. It's your constructor not calling the parent constructor which is what boots your soft deletes trait. Add a parent::__construct() to your constructor. It should match the signature of the parent as well so it should be:
public function __construct(array $attributes = [])
{
$this->assigned = false;
parent::__construct($attributes);
}
You misread the documentation, your accessor doesn't have the right name:
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
class Users extends Model
{
use SoftDeletes;
protected $dates = ['deleted_at'];
protected $table = 'users';
protected $appends = ['assigned'];
protected $fillable = ['category_id'];
public function __construct()
{
$this->assigned = false;
}
public function setIsAssignedAttribute($value)
{
$this->attributes['assigned'] = $value;
}
public function getIsAssignedAttribute($value)
{
return $value;
}
}
You need the Is even thought it is not part of the variable name.
Related
i want to get nama from Supplier table using PembelianDetail table through Pembelian table using this code :
$detail = PembelianDetail::with('supplier')->whereBetween('tanggal',[$awal, $akhir])->where('id_produk', $produk->id_produk)->get();
with this model :
class Supplier extends Model
{
use HasFactory;
protected $table = 'supplier';
protected $primaryKey = 'id_supplier';
protected $guarded = [];
public function pembelian()
{
return $this->hasMany(Pembelian::class, 'id_pembelian', 'id_pembelian');
}
public function pembelian_detail(){
return $this->hasManyThrough(PembelianDetail::class, Pembelian::class );
}
}
class Pembelian extends Model
{
use HasFactory;
protected $table = 'pembelian';
protected $primaryKey = 'id_pembelian';
protected $guarded = [];
public function supplier()
{
return $this->belongsTo(Supplier::class, 'id_supplier', 'id_supplier');
}
public function pembelian_detail()
{
return $this->hasMany(PembelianDetail::class, 'id_pembelian_detail', 'id_pembelian_detail');
}
}
class PembelianDetail extends Model
{
use HasFactory;
protected $table = 'pembelian_detail';
protected $primaryKey = 'id_pembelian_detail';
protected $guarded = [];
public function pembelian()
{
return $this->belongsTo(PembelianDetail::class, 'id_pembelian_detail', 'id_pembelian_detail');
}
}
and keep getting this error :
Call to undefined relationship [supplier] on model [App\Models\PembelianDetail].
with the same way i dont understand how that works in this tutorial https://www.youtube.com/watch?v=5s-_SnVl-1g and i just followed the same steps but keep getting that error.
i also tried nested eager loading according laravel documentation by using :
$detail = PembelianDetail::with('pembelian.supplier')->whereBetween('tanggal',[$awal, $akhir])->where('id_produk', $produk->id_produk)->get();
but get the same error and other queries works fine
Am i missing something here?
I'm quite new to Laravel and now I'm trying to move parts of a former application from a small self written framework into Laravel. The address book is multilingual therefor the table structure is a little more complicated.
db-structure
And this is my source code:
AddressBookController.php
namespace App\Http\Controllers;
use App\AddressBook as AB;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;
class AddressBookController extends Controller
{
/**
* Display a listing of the resource.
*
* #return \Illuminate\Http\Response
*/
public function index()
{
$entries = AB::all();
return view('addressBook')->with([
'class' => __CLASS__,
'function' => __FUNCTION__,
'line' => __LINE__,
'entries' => $entries,
]);
}
}
Model AddressBook.php
namespace App;
use Illuminate\Database\Eloquent\Model;
class AddressBook extends Model
{
protected $table = 'address';
protected $primaryKey = 'address_id';
protected $keyType = 'int';
public $incrementing = true;
public $timestamps = false;
protected $searchable = [
'columns' => [
'address.address_surname' => 10,
'address.address_company' => 5,
'address.address_vatid' => 2,
],
];
public function country() {
return $this->hasOne('country', 'country_id', 'country_id');
}
public function addresstype() {
return $this->hasOne('addresstype', 'addresstype_id', 'addresstype_id');
}
}
Model Country.php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Country extends Model
{
protected $table = 'country';
protected $primaryKey = 'country_id';
protected $keyType = 'int';
public $incrementing = true;
public $timestamps = false;
public function translation() {
return $this->hasOne('translations', 'translations_id', 'translations_id');
}
public function addressbook() {
return $this->belongsTo('address', 'country_id', 'country_id');
}
}
Model AddressType
namespace App;
use Illuminate\Database\Eloquent\Model;
class AddressType extends Model
{
protected $table = 'addresstype';
protected $primaryKey = 'addresstype_id';
protected $keyType = 'int';
public $incrementing = true;
public $timestamps = false;
public function translation() {
return $this->hasOne('translations', 'translations_id', 'translations_id');
}
public function addressbook() {
return $this->belongsTo('address', 'addresstype_id', 'addresstype_id');
}
}
Model Translation.php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Translation extends Model
{
protected $table = 'translations';
protected $primaryKey = 'translations_id';
protected $keyType = 'int';
public $incrementing = true;
public $timestamps = false;
public function country() {
return $this->belongsTo('country', 'translations_id', 'translations_id');
}
public function addresstype() {
return $this->belongsTo('addresstype', 'translations_id', 'translations_id');
}
}
The request "$entries = AB::all();" works in general but I get the id's and maybe I'm completely wrong here but I thought that data from foreign keys will be replaced with the respective models (if configured correctly). so my question is:
a. did I make a mistake during configuration and if yes, where exactly is the error?
or
b. is my assumption of replacing id's with objects is completly wrong?
Thanks in advance!
Steve
Laravel Eloquent models do not replace the foreign keys of the active records with the relational data, it only appends a new property with the same name as the method relating the classes and in that property it puts all the Model instances of the resulted query, this ONLY if you access the property, its called Eager Loading.
It is explained here (Ofiicial Documentation)
$addressBook = App\AddressBook::find(1); //this only will return the active record with the id 1 and nothig more.
$addressBook->country; // The property "country" does not exist in the AddressBook Classs but eloquent models will return a "fake" property with the value of the result of the query by the method with the same name (only if the method returns an Eloquent Relation).
This Eloquent behavior is natural, and is a very clever way to minimize the number of queries, Eloquent will never load a relation if there is no need to.
If you want to load a relation in a set the models at the same time that those models are retriving from the db you need to explicitly especify wich relations you want to load.
$addressBook = App\AddressBook::with(['country', 'addresstype', 'anotherRelation'])->get(); // this will retrive all the addressBook models and in each one will attach the relations specified.
[EDITED]
Also you have to place the entire namespace of the related model class in the relation methods so you need to replaced like:
class Translation extends Model
{
protected $table = 'translations';
protected $primaryKey = 'translations_id';
protected $keyType = 'int';
public $incrementing = true;
public $timestamps = false;
// ****** You need to put the entire namespace of the Model class
public function country() {
return $this->belongsTo('App\Country', 'translations_id', 'translations_id');
}
I have a model for custom fields,I want to bind the configuration detail for all model extended by Base model!
my CoctomFormBulder Model
class CustomFormBuilder extends \Eloquent
{
protected $fillable = [];
protected $table = 'custom_fields_configure';
}
Here is My base model and what I have try up to now.
class BaseModel extends \Eloquent {
protected $guarded = array('q');
protected $table = 'custom_fields_configure';
protected $appends = ['configure'];
public function getConfigerAttribute() {
if (isset($this->attributes['configure'])) {
//return URL::asset($this->attributes['image']);
return $this->attributes['configure'];
}
return '';
}
public function setConfigerAttribute($configure) {
$this->attributes['configure'] = $configure;
}
}
How can Bind Data from CustomFormBuilder to all of my models in the app
Just let the models that need that logic extends the 'CustomFormBuilder' class
I have two models:
TRUCK
DRIVER
TRUCK has two fields which are FKs. Driver (FK) and Driver2 (FK).
When I try to get the truck with driver and driver2, I get two same records.
$truck = $this->instance->truck()->where('id', $id)
->with(['driver', 'driver2',])
->firstOrFail();
My Truck Model:
class Truck extends Model
{
use SoftDeletes;
protected $table = 'trucks';
protected $guarded = ['id'];
protected $dates = ['deleted_at'];
public function driver()
{
return $this->hasOne('App\Models\Driver');
}
public function driver2()
{
return $this->hasOne('App\Models\Driver');
}
My Driver Model:
class Driver extends Model
{
use SoftDeletes;
protected $table = 'drivers';
protected $guarded = ['id'];
protected $dates = ['deleted_at'];
public function truck()
{
return $this->belongsTo('App\Models\Truck');
}
I am still new to laravel and something I get stuck. Should I create another model instead maybe?
By default laravel will use default foreign key,
Eloquent assumes the foreign key of the relationship based on the
model name #Further reading
So both relation are pointing to the same FK, So you need to specify the foreign key as below
return $this->hasOne('App\Models\Driver', 'Driver');
return $this->hasOne('App\Models\Driver', 'Driver2');
Full code
class Truck extends Model
{
use SoftDeletes;
protected $table = 'trucks';
protected $guarded = ['id'];
protected $dates = ['deleted_at'];
public function driver()
{
return $this->hasOne('App\Models\Driver', 'Driver');
}
public function driver2()
{
return $this->hasOne('App\Models\Driver', 'Driver2');
}
Is there anyone can suggest from the eloquent relationship that I have based on the screenshot and model setup?
Model setup:
class Leaves extends Model
{
protected $table = 'leaves';
protected $fillable = [
'leave_type',
'user_id'
];
public function user()
{
return $this->belongsTo('App\User');
}
}
class LeaveType extends Model
{
protected $table = 'leave_type';
protected $fillable = ['type_name'];
}
class User extends Model implements AuthenticatableContract,
AuthorizableContract,
CanResetPasswordContract
{
use Authenticatable, Authorizable, CanResetPassword;
protected $table = 'users';
protected $fillable = ['name', 'email', 'password'];
protected $hidden = ['password', 'remember_token'];
public function leave()
{
return $this->hasMany('App\Leaves');
}
}
Currently I only able to get the leaves detail but need to retrieve the leave_type's type_name based on
$user = User::oldest('name')->get();
foreach ($users as $user) {
$user->leave()-get();
}
in your Leave model you would have
function type() {
return $this->hasOne(App\LeaveType);
}
in your LeaveType you should reciprocate
function leave() {
return $this->belongsToMany(App\LeaveType);
}
and in your controller:
$user = User::oldest('name')->with('leave', 'leave.type')->get();
dd($user->leave->type);
Leaves model:
class Leaves extends Model {
protected $table = 'leaves';
protected $fillable = [
'leave_type',
'user_id'
];
public function User()
{
return $this->belongsTo('App\User');
}
public function LeaveType()
{
return $this->belongsTo('App\LeaveType');
}
}
LeavesType model:
class LeaveType extends Model {
protected $table = 'leave_type';
protected $fillable = ['type_name'];
public function Leaves()
{
return $this->hasMany('App\Leaves');
}
}
User model:
class User extends Model implements AuthenticatableContract,
AuthorizableContract,
CanResetPasswordContract
{
use Authenticatable, Authorizable, CanResetPassword;
protected $table = 'users';
protected $fillable = ['name', 'email', 'password'];
protected $hidden = ['password', 'remember_token'];
public function Leaves()
{
return $this->hasMany('App\Leaves');
}
}