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.
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();
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();
I was trying to make a search in laravel but in laravel when I used
User::where('first_name', 'LIKE %', $name);
It Didn't work.
This is my user model.
<?php
namespace App;
use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;
class User extends Authenticatable {
use Notifiable;
protected $table = 'users';
protected $fillable = [
'first_name',
'last_name',
'email',
'bio',
'pic',
'location',
'password',
'is_e'
];
protected $hidden = [
'password', 'remember_token',
];
public function project() {
return $this->hasMany('App\Project');
}
}
I also tried not using like but then it was working properly. But not working at all with like. please help
thanx
You have to append % with value not with LIKE string. Try following code:
User::where('first_name', 'LIKE', $name."%"); // add % with $name
For match string from both side
User::where('first_name', 'LIKE', "%".$name."%");
User::where('first_name', 'LIKE', $name."%")->get();
I have a project about quizzes and their questions,
I don't know why but when i'm trying to get the quiz's questions i get
this error
This are my files and scripts:
Route web.php file:
Route::get('/admin/quizzes/{id}/questions', 'AdminQuizzesController#questions')->name('admin.quizzes.questions')
Model Quiz.php file:
class Quiz extends Model
{
protected $dates = [ 'start_at', 'end_at', 'created_at', 'updated_at', 'deleted_at'];
protected $fillable = [
'title', 'category_id', 'level_id', 'is_active', 'start_at', 'end_at'
];
public function questions(){
return $this->belongsToMany('App\Question');
}
}
Model Question.php file:
class Question extends Model
{
protected $dates = ['created_at', 'updated_at', 'deleted_at'];
protected $fillable = [
'title', 'correct_answer', 'category_id', 'level_id'
];
public function quiz(){
return $this->belongsTo('App\Quiz');
}
}
Controller AdminQuizzesController.php file:
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Quiz;
class AdminQuizzesController extends Controller
{
public function questions($id)
{
$quiz = Quiz::findOrFail($id);
$questions = $quiz->questions;
return view('admin.quizzes.questions.index', compact('quiz', 'questions'));
}
}
the answer of #MazinoSUkah solved my problem.
just did 'composer dump-autoload' in my cmd
AFAIK composer dump-autoload regenerates the list of all classes that need to be included in the project (autoload_classmap.php). Ideal for when you have a new class inside your project or changed/renamed a class file. Hope it helps.
I wan't to get the name of the user who created is own thread. Like Michael did a thread about food. So at the bottom of the food-thread should be the name of Michael.
I've wrote the code for this but it doesn't really works. Maybe someone of you can find the mistake.
I have two models. A thread Model and a users model.
thread model:
<?php
namespace App\Models\Thread;
use Illuminate\Database\Eloquent\Model;
use App\User;
class Thread extends Model {
public $table = 'thread';
public $fillable = [
'thread',
'content',
'user_id'
];
public function userthread() {
return $this->belongsTo('User','user_id', 'id');
user model:
<?php
namespace App;
use ...
protected $table = 'users';
protected $fillable = ['name', 'email', 'password'];
protected $hidden = ['password', 'remember_token'];
public function threaduser() {
return $this->hasMany('App\Models\Thread\Thread','user_id', 'id');
}
}
and now the controller method, where I'm trying to get the name:
public function show($id)
{
$thread = Thread::query()->findOrFail($id);
$threaduser = Thread::where('user_id', Auth::user()->id)->with('userthread')->get();
return view('test.show', [
'thread' => $thread,
'threaduser' => $threaduser
]);
}
in my html:
{{$threaduser->name}}
The error message I get is :
Undefined property: Illuminate\Database\Eloquent\Collection::$name (View: /var/www/laravel/logs/resources/views/test/show.blade.php)
I hope someone can help me there.
change it to
{{$threaduser->userthread->name}}
change userthread() function in your Thread Class to
public function userthread() {
return $this->belongsTo('App\User','user_id', 'id');
}
get() gives you a Collection not a Model you either have to do a foreach on it like
#foreach ($threadusers as $threaduser)
{{ $threaduser->userthread->name }}
#endforeach
Or use first instead of get if there is only one Thread per User.
Depending on what you want to do, of course.