I have hundreds of fields in my table and I dont want to write all the field in the$fillablearray. Is there any way tobypass $fillable process` ?
class MyClass extends Eloquent {
protected $fillable = array('firstField', 'secondField',.......);
}
You could do it the opposite way:
Specify the guarded attributes (all fields that should not be fillable).
protected $guarded = array('id', 'created_at', '...');
And remove the $fillable completely.
Related
I have three table in DB, and I have used belongsTo for merging them. My Admins model looks like this :
class Admin extends Authenticatable
{
use HasFactory;
protected $guarded = ['status', 'shop_name', 'vendor_id', 'id'];
protected $guard = 'admin';
public function getVendorDetails(){
return $this->belongsTo(Vendor::class, 'vendor_id','vendor_id');
}
public function getVendorBusinessDetails(){
return $this->belongsTo(VendorBusinessDetails::class, 'vendor_id','vendor_id');
}
}
I need to prevent shop-name field to be saved through form. this field is in the table name : vendor_business_detals (getVendorBusinessDetails).But in this case $guarded only works for Admins table and ignores another relationship tables and fields of them such as shop_name. In short I am struggling to find the way to add $guarded data for VendorBusinessDetails. My controller looks like below:
$business_data = $request->only(['shop_name', 'shop_address', 'shop_phone']);
//dd($business_data);
$myModel = Admin::find($id);
//business table update
$myModel->getVendorBusinessDetails()->update($business_data);
VendorBusinessDetails model:
class VendorBusinessDetails extends Model
{
use HasFactory;
protected $guarded = ['shop_name'];
protected $table = 'vendors_business_details';
}
I don't know why $guraded=['shop_name'] is ignored. Any help would be highly appreciated.
$myModel->getVendorBusinessDetails()->update($business_data);
If you use "getVendorBusinessDetails" as a method, the model isn't initialize. Only the SQL update command runs.
$myModel->getVendorBusinessDetails->update($business_data);
Using "getVendorBusinessDetails" as a field initializes the model. So, it works as you expected.
I have a model that looks like:
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
class Location extends Model{
use SoftDeletes;
// Name of our database table
protected $table = 'locations';
// Column for soft deletes
protected $dates = ['deleted_at'];
// Columns that are mass assignable
protected $fillable = ['name', 'address', 'city'];
}
When I call Location::get(), the deleted_at column is returned by default.
How do I prevent this column from being returned without explicitly specifying it?
You can use the $hidden property like this :
protected $hidden = ['deleted_at'];
From the documentation :
Sometimes you may wish to limit the attributes, such as passwords, that are included in your model's array or JSON representation. To do so, add a $hidden property to your model:
How to apply sorting in eloquent query with relational table with "hasOne" Relation?
Brand Model
class Brand extends Eloquent
{
protected $table = 'brand';
protected $primaryKey = 'id';
protected $fillable = ['brand_name'];
public $sortable = ['brand_name'];
public function brandModel() {
return $this->hasOne('App\Brandmodel', 'brand_id')
->select('id', 'brand_id', 'brand_model_name');
}
}
---------------------------------------
class Brandmodel extends Eloquent
{
protected $table = 'brandmodel';
protected $primaryKey = 'id';
protected $fillable = ['brand_id'];
public $sortable = ['brand_model_name'];
}
In this case we can sort using "brand_name". But I am not able to sort using "brand_model_name".
If you want to do this you'll need to step outside of Eloquent a little bit and join on the table you need.
Brand::join('brandmodel', 'brandmodel.brand_id', '=', 'brand.id')
->orderBy('brandmodel.brand_model_name')
->get();
I'd also recommend you re-work your table names to match Eloquent's default naming conventions as it would make the code a little easier to parse.
You should try this:
Brand::leftJoin('brandmodel', 'brandmodel.brand_id', '=', 'brand.id')
->orderBy('brandmodel.brand_model_name','desc')
->get();
My EloquentUserRepository (concrete implementation) has some methods like getCompanies($userId) and getProfile($userId), for example.
In both cases, before returning stuff, they fetch the user, like so:
$user = User::find($userId);
So, when I need to call those two methods in the same request, the data is fetched twice.
Am I missing something or am I using repositories wrongly?
What if you approached it from the relationship side instead? I'm assuming that Companies live in a different table. So could you acquire the companies with something like this?
<?php
class User extends Model {
protected $table = 'users';
protected $fillable = ['name', 'email', 'avatar'];
protected $hidden = ['password', 'remember_token'];
public function companies()
{
return $this->hasMany('App\Company', 'user_id');
}
}
Then you could find the companies with:
$user = User::find($userId)->load('companies')->get();
and you'd already have the companies loaded.
If the profile is in a different table you could do a similar relationship, like this:
<?php
class User extends Model {
protected $table = 'users';
protected $fillable = ['name', 'email', 'avatar'];
protected $hidden = ['password', 'remember_token'];
public function profile()
{
return $this->hasOne('App\Profile', 'user_id');
}
}
I'm not certain if this is exactly what you are looking for, but it's the approach I like to use when finding items that are related to a particular model.
We can use:
protected $hidden = array('attribute');
to hide the attributes we don't want to send to our views.
And i found out writing:
$this->table = 'table';
In a specific function resulted in another that table being used.
But what would we do if we need to hide some attributes in a specific function only?
Kinda like this:
$this->hidden = array('attribute1', 'attribute2');
That didn't work though.
You should really understand how to use models.
In your model you can set several properties, for example, what table the model should use:
protected $table = 'my_users';
Or which attribtues of the model will be hidden:
protected $hidden = array('password');
Or which attributes are fillable (whitelisted), and so protecting from mass assignment vulnerabilities:
protected $fillable = array('first_name', 'last_name', 'email');
Or which attributes should be blacklisted:
protected $guarded = array('id', 'password');
So, you're not dealing with these properties/attributes by function, but by class (model).
If you set certain attributes as hidden in a model, they will be hidden from Array or JSON conversions regardless.