laravel two $fillable array for addition(create) and modification(update)
$fillable1 for create
and
$fillable2 for update
for example
//in model
protected $fillable1 = [
'id', 'email','name', 'phone',
];//for create
protected $fillable2 = [
'name', 'phone'
];//for update
//in controler
public function store
User::create($request->all());
public function update
User::findOrFail($user_id)->fill($request->all())->save();
so in this example column email will be creatable but not editable
User::create($request->all());
User::create($request->$fillable1());
and
User::findOrFail($user_id)->fill($request->all())->save();
User::findOrFail($user_id)->fill($request->$fillable2())->save();
$fillable used by Model, So, you will need one variable $fillable.
Try to use this:
protected $hidden = [
'email', 'password',
];
or Just modify your Controller to update only what you want :)
User::findOrFail($user_id)->fill(['values_here'])->save();
Related
Part of data can not be acquired when Eloquent object is set to Json.
I use command artisan make:Auth, and customized users table.
Specifically, I thought that I did not need to delete the email column once,but since it became necessary, I added it again.
These operations use a migration file.
I got all the data of users table and returned it with Json.
But email is not output.
class UserController extends Controller
{
public function index() {
return User::all();
}
}
Even using json_encode did the same thing.
It exists when use this
$user = User::all();
$userArray = array();
foreach ($user as $u){
$userArray[] = [
'name' => $u->name,
'email' => $u->email
];
}
return $userArray;
Why is not email being outputted?
(I rely on google translation)
Additional notes
Thanks to everyone for comment.
Comparing the output of json with dd(), password and remember_token were not output in addition to email.
Perhaps the email is private property, and private property is a mechanism that does not output JSON?
In that case, how can I change to public?
The User model looks like this
class User extends Authenticatable
{
use Notifiable;
use SoftDeletes;
protected $table = 'users';
protected $dates = ['deleted_at'];
protected $primaryKey = 'uuid';
protected $keyType = 'string';
public $incrementing = false;
protected $fillable = [
'name', 'email', 'password',
];
protected $hidden = [
'password', 'email','remember_token',
];
~~~
}
I solved it myself.
I found that the column set here is not output to JSON.
protected $hidden = [
'password', 'email','remember_token',
];
It was a very simple thing....
try this:
return User::select('name', 'email')->get()->toArray();
I have a user model, which can have many reports, and a report model obviously belonging to a user, whenever one is created.
However when I use return $this->belongsTo('App\User') on the report model No user is returned even when I have the correct user_id on the report, and correct id on the user table.
User
protected $fillable = [
'name', 'email', 'password',
];
public function reports()
{
return $this->hasMany('App\Report');
}
Report
protected $fillable = [
'user_id', 'title', 'detail',
];
public function user()
{
return $this->belongsTo('App\User', 'user_id');
}
I've solved it simply by using $report->user, instead of calling it like a function via $report->user()
I have three tables: events - admins - dependencies
The relationship between them is the following:
a dependency has many admins, an admin has many events
like that:
I'm trying to show the name of the dependency to which the admin that created the event belongs.
How could I bring data with Laravel from this three tables?
controller:
class WelcomeController extends Controller
{
public function evento($slug){
$event = Event::where('slug', $slug)->first();
return view('evento', compact('event'));
}
Event model:
class Event extends Model
{
protected $fillable = [
'admin_id', 'place_id', 'name', 'slug', 'excerpt', 'body', 'status', 'file'
];
public function admins(){
return $this->belongsTo('App\Admin', 'admin_id');
}
Admin model:
protected $fillable = [
'name', 'email', 'password', 'cargo', 'dependence_id'
];
public function dependencyAdmin(){
return $this->belongsTo(Dependence::class);
}
Dependence Model:
class Dependence extends Model
{
protected $fillable = [
'name', 'slug'
];
public function adminsDependence(){
return $this->hasMany(Admin::class);
}
So I try to show it in the view, but it does not work:
<div class="panel-heading">
Dependencia:
{{$event->admins->dependencyAdmin->name}}
</div>
The problem is that you're only retrieving the event model without loading the relationship. See here for more explanation
$event = Event::where('slug', $slug)->first();
$eventAdmins = $event->admins()->get();
$eventAdminDependency = $eventAdmins->dependencyAdmin()->get();
I am trying to do the mass assignment with laravel like. But I have a field called 'hidden' that in the database is a TINYINT. From my front-end I get a boolean back. When I mass-assign with 'hidden' => TRUE the field in DB still is 0. When i convert it back to a integer ('hidden' => 1) then the field is saved as 1.
I did added 'hidden' to my $fillable.
P.S. When I try inserting it into DB directy with mysql with boolean value, it works.
Anyone know what is wrong?
EDIT: this is my code,
public function store(Request $request) {
class Group extends Model
{
use Notifiable;
const CREATED_AT = 'created';
const UPDATED_AT = 'updated';
protected $table = 'groups';
protected $casts = [
'hidden' => 'boolean',
];
protected $fillable = [
'hidden',
// etc
];
}
public function store(Request $request) {
$post = $request->all();
$group_id = Group::create($post);
}
Front-end is Vue project. So laravel is my API. And I do get a TRUE out of $post['hidden'].
You need to cast the boolean in the model:
class YourModel extends Model
{
protected $casts = [
'hidden' => 'boolean',
];
}
This will tell Laravel the you want the hidden column to be treated as boolean and values like 0 and 1 will be returned as true/false and true/false saved as 0/1.
You can read more in Laravel doc mutators.
Change the database type to Bool. If you want to do that with a migration you can do: $table->boolean(‘hidden’);
I am getting this error:
MassAssignmentException in Model.php line 448: _token
When I am using create method. Please review code below:
Contacts.php (Model):
class Contacts extends Model
{
protected $table = ['name', 'mobile', 'email', 'address', 'created_at', 'updated_at'];
}
ContactsController.php (Controller):
public function store(Request $request)
{
$inputs = $request->all();
$contacts = Contacts::Create($inputs);
return redirect()->route('contacts.index');
}
For the Mass Assignment Exception: you should specify all the fields of the model that you want to be mass-assignable through create or update operations on the property $fillable:
protected $fillable = ['name', 'mobile', 'email', 'address', 'created_at', 'updated_at'];
Besides, the field $table should contain only the model's table name:
protected $table = 'your_table_name';
This might happen in case if you have used the wrongly imported the class. if you are using the User Model.
Wrong Import
// mostly IDE suggestion
use Illuminate\Foundation\Auth\User;
Correct Model Import
use App\User;
i have gone through this. might help someone.
You can all column fillable:
protected $guarded = array();
Add your model.
You need only to add the following to your Model (Contact):
protected $fillable = ['name', 'mobile', 'email', 'address', 'created_at', 'updated_at'];
For example:
class Contacts extends Model {
protected $table = ['name', 'mobile', 'email', 'address', 'created_at', 'updated_at'];
protected $fillable = [ 'name', 'mobile', 'email', 'address', 'created_at', 'updated_at' ];
}
If all of the above fails, you can try following.
Put following after namespace.
use Eloquent;
Put following at the start of your store method.
Eloquent::unguard();
like:
public function store(Request $request)
{
Eloquent::unguard();
$inputs = $request->all();
$contacts = Contacts::Create($inputs);
return redirect()->route('contacts.index');
}
This is not recommended though, as this makes things vulnerable to attacks. But if you need a quick fix this might help.
Check Model you have Imported or Not. If not then use this.
<?php
namespace App\Http\Controllers\Auth;
use App\Http\Controllers\Controller;
use App\User;
Make sure you are putting the $fillable or $guarded in the app\Contacts.php file and not the app\Http\Controllers\ContactsController.php file. It should be obvious, but it can be overlooked.