Laravel 4 eloquent Mass assignment error in fillable - php

I'm currently studying eloquent of L4 and I encountered this mass assignment. I'd follow the instructions and I know you need to setup your $fillable to use the create method but I am still receiving a blank row in the database. here's my code:
MODEL:
class User extends Eloquent
{
protected $fillable = array('email','pwd','active','name');
}
CONTROLLER:
$user = new User;
$add = array(
'name'=>Input::get('custname'),
'active'=>1,
'pwd'=>Hash::make(Input::get('pwd')),
'email'=>Input::get('email')
);
return var_dump($user->create($add));
I also did:
CONTROLLER
$add = array(
'name'=>Input::get('custname'),
'active'=>1,
'pwd'=>Hash::make(Input::get('pwd')),
'email'=>Input::get('email')
);
return var_dump(User::create($add));
But still the same result.

There was a bug causing this, see https://github.com/laravel/framework/issues/1548
Should be fixed now, run composer update to get the newest version of laravel/framework

Yes with new version you can use public or protected keywords.
Simple use this :
protected $fillable = [‘email’, ‘pwd’,’active’,’name’];
You can also specify table name if you are working with other Model like this :
public $table = ‘users’
This is working fine after run composer update on the root of project directory.

Related

Laravel Activity Log won't work on Update

I'm using SPATIE laravel-activitylog I followed all the instructions but still it only log the Create function not update while using it on a Modal
My Modal
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Spatie\Activitylog\Traits\LogsActivity;
class Contact extends Model
{
use HasFactory, LogsActivity;
protected $fillable = [
'comp_name',
'cont_name',
'cont_email',
'cont_number',
'created_by',
'updated_by',
];
// spatie activitylog
protected static $logFillable = true;
protected static $logOnlyDirty = true;
protected static $logName='users'; // default
}
My Controller
Contact::where('id',$input['id'])->update($data);
$retrnArray = array('status'=>1,'msg'=>'Updated Successfully');
I have changed my query. we should use Eloquent query.
$contact = Contact::find($input['id']);
$contact->cont_email = $input['cont_email'];
$contact->cont_number = $input['cont_number'];
$contact->save();
$retrnArray = array('status'=>1,'msg'=>'Updated Successfully');
The Update with writing in DB but NOT writing in ActivityLogs will look like this:
User::where("id", 1)->update($data);
The Update with writing in DB and ALSO writing in ActivityLogs will look like this:
User::where("id", 1)->first()?->update($data); //if you are using php >= 8
User::where("id", 1)->firstOrFail()->update($data); // Good using php >= 5.6
User::find(1)?->update($data); //This also works as find("your id") also returns the model that it was able to found, it also returns null so check it on null or use nullsafe operator.
It's important to load model to make ActivityLogs correctly.
It seems the default log options does not include all Model`s field. You can describe fields that needs logging or use the wildcard to fire logging for every field changes. According to documentation example (in your Model Class):
public function getActivitylogOptions(): LogOptions
{
return LogOptions::defaults()->logOnly(['*']);
// To avoid hardcoding you could use logAll() method
// return LogOptions::defaults()->logAll();
}

Extending the Spatie Role Model but use a different database table

What I need to do is extend all of the functionality of the Spatie permissions package Role model, but use a different table for the derived model.
Right now I have a model SubscriptionPackage that I want to emulate the behavior of a Role such that it can be assigned permissions and in turn this model can be assigned to users. But I wanna keep the Role model intact too.
I have tried extending Yes, but when I create a new SubscriptionPackage, the new record is created in the roles tables instead of subscription_packages table despite specifying the table in my derived Model. As shown below
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use App\Models\Permission; // This extends from Spatie\Permission\Models\Permission
use Spatie\Permission\Models\Role as SpatieRole;
class SubscriptionPackage extends SpatieRole
{
//
protected $guarded = ['id'];
protected $table = 'subscription_packages';
/**
* The permissions that belong to the package.
*/
public function packagePermissions()
{
return $this->belongsToMany(Permission::class);
}
}
With the code above I expect when I create a new SubscriptionPackage, the record should be inserted into the subscription_packages table but in this case it goes to the roles table.
Any pointers on how to go about this will be highly appreciated.
If you have a look at the Role source code you will this inside the __construct method:
public function __construct(array $attributes = [])
{
$attributes['guard_name'] = $attributes['guard_name'] ?? config('auth.defaults.guard');
parent::__construct($attributes);
$this->setTable(config('permission.table_names.roles')); // <-- HERE IS THE PROBLEM!
}
So, if you want that your SubscriptionPackage to write its records in the right table you have to override this behaviour like this:
public function __construct(array $attributes = [])
{
parent::__construct($attributes)
$this->setTable('your_table_name'); // <-- HERE THE SOLUTION!
}
I don't think you can. Spatie already have 5 tables and fetched data from those only. But still if you want to make the change you have make the changes with table and column name in the model

Laravel Model save() & update() Not Saving

Laravel 5.7
Hello I have been looking through stack overflow and have tried many possible answers and have come to no conclusions.
I am updating a simple integer value in a patron table through simple checks and it is not updating the database. I have tried using save() and update() methods on the model.
There are no errors or exceptions that show up and the save() and update() methods return true.
code:
Controller Class Using the model and updating data:
$patron_coupon = PatronCoupons::where('owner',$patron_id)->where('coupon_id',$active_coupon['coupon_id'])->first();
if($patron_coupon->current_uses > $active_coupon['max_uses'])
$patron_coupon->current_uses = $active_coupon['max_uses'];
// if i echo $patron_coupon->current_uses here, it will show that this value has changed ( good )
$patron_coupon->current_uses = $active_coupon['max_uses'] - $patron_coupon->times_used;
if($patron_coupon->current_uses < 0)
$patron_coupon->current_uses = 0;
// this doesnt work
$patron_coupon->save();
// this doesnt work either
$patron_coupon->update($patron_coupon->toArray());
// if i echo current_uses here, the value goes back to what it was before being set.
Model Class:
class PatronCoupons extends Model
{
protected $table = 'patron_coupons';
protected $primaryKey = 'owner';
public $timestamps = false;
protected $fillable = ['times_used','current_uses','settings_version'];
}
Migration File:
Schema::create('patron_coupons', function (Blueprint $table) {
$table->string('owner'); // patron id that 'owns' this coupon
$table->unsignedInteger('coupon_id'); // id of this coupon
$table->unsignedInteger('current_uses')->default(0); // the amount of uses this patron has for this coupon
$table->unsignedInteger('settings_version')->nullable();// last settings version fetch
$table->unsignedInteger('times_used')->default(0); // amount of times this coupon has been used
});
Please help, I've been banging my head on my desk for hours !!!!!
You can try any of the following:
Try using static update function instead
PatronCoupons::update([
// updates here
]);
Remove your vendor folder and composer.lock file. Then run composer install to refresh your vendor file
get record
$patronCoupons = PatronCoupons::find($id);
$patronCoupons->update([
'times_used' => 'value',
'current_uses' => 'value',
'settings_version' => 'value'
]);
if your post data with the same input name then directly use
$post = $request->all();
$patronCoupons = PatronCoupons::find($id);
$patronCoupons->update($post);
Make sure that you put this code in the form because of which the error occurs
'enctype' => 'multipart/form-data'

Laravel 5.1 (Eloquent) in using relationship attaching or detaching not working

I'm having problems in using Laravel 5.1's Eloquent. I have 2 databases, and 2 model classes are using a different database (that is not default database).
It is working well for simple CRUD, but when I use relationship, it causes an error.
$list->users()->attach($nListUser->id, [
'entered' => $user->createdDate,
'modified' => date('Y-m-d H:m:s'),
]);
Or
$list->users()->detach($nListUser->id);
Error code is
SQLSTATE[42S02]: Base table or view not found: 1146 Table
'sampledb.listuser' doesn't exist (SQL: insert
into listuser (entered, listid, modified, userid)
values (2012-06-17 18:34:58, 52275, 2016-01-18 02:01:46, 6))
This is my model class file.
class ListUser extends Model
{
protected $connection = 'listdbconnection';
protected $table = 'listuser';
public $timestamps = false;
}
class PList extends Model
{
protected $connection = 'listdbconnection';
protected $table = 'list';
public $timestamps = false;
public function users(){
return $this->belongsToMany('App\Model\User', 'listuser', 'userid', 'listid');
}
}
Eventhough I set connection name above, it is still finding the table in default database. It is clear that the Eloquent is working on default database for relationship.
Has anyone solution for this? Am I wrong or Is this really Laravel 5.1 Eloquent's fault?
Finally found the reason.
Eloquent was wonderful. The model class I specified in the belongsToMany parameter was wrong.
It was actually, as belows.
return $this->belongsToMany('App\Model\LUser', 'listuser', 'userid', 'listid');
I recommend Eloquent because you can use it outside Laravel. Maybe, good choice for web service development.

Laravel Eloquent::Find() returning NULL with an existing ID

It's pretty straightforward as it's the most basic thing but I don't know what I'm missing:
Having a model called Site
I'm using Eloquent ORM, so when I call (in a controller)
$oSite = Site::find(1)
and then
var_dump($oSite);
It returns a value of NULL.
But when I check the database, the table 'sites' actually contains the following item:
id: 1
user_id: 1
name: test
In my Site model I have the following code:
use Illuminate\Database\Eloquent\ModelNotFoundException;
Class Site extends Eloquent {
protected $table = 'sites';
protected $fillable = ['user_id', 'name'];
}
Instead, if I gather the item with the following:
$oSite = DB::table('sites')
->where('id', 1)
->first();
It works and I get the correct register.
What I'm doing wrong? Which part of the documentation I didn't get?
EDIT:
Model code can be checked above.
Controller:
use Illuminate\Support\Facades\Redirect;
class SiteManagementController extends BaseController {
...
public function deleteSite()
{
if (Request::ajax())
{
$iSiteToDelete = Input::get('siteId');
$oSite = Site::find($iSiteToDelete);
return var_dump($oSite);
}
else
{
return false;
}
}
}
EDIT 2: (SOLVED)
Real reason why wasn't working:
I had originally in my model code the following:
use Illuminate\Database\Eloquent\SoftDeletingTrait;
use Illuminate\Database\Eloquent\ModelNotFoundException;
Class Site extends Eloquent {
protected $table = 'sites';
use SoftDeletingTrait;
protected $dates = ['deleted_at'];
protected $fillable = ['user_id', 'name'];
}
Problem was I added a 'deleted_at' column after I started the project and when I applied migrations, I didn't have softdeleting enabled.
Obviously, I did a second error, forgetting to enable 'deleted_at' to be nullable, hence all inserts went had a wrong timestamp (0000-00-00 ...).
Fix:
Made nullable 'deleted_at' column.
Set all wrong 'deleted_at' timestamps to NULL.
Check you are getting Input::get('siteId') correctly. if you are getting it then try to convert it into integer i.e
$iSiteToDelete = intval(Input::get('siteId'));
You're not returning your model.
var_dump prints output and returns nothing.
do this instead:
dd($oSite); // stands for var_dump and die - a helper method
and even better, simply return the model:
return $oSite; // will be cast to JSON string
In my case I was using a custom query with the DB facade. I neglected to skip records that have a deleted_at in my DB query. When showing all the records, it worked with IDs that had already been deleted, so methods like find that if they were considering the deleted_at, did not find the record.
Layer eight.
For the future if you encounter a similar problem you can check what SQL queries laravel is sending to the database.
Todo so just enable query logging by using DB facade:
\DB::enableQueryLog();
Before sending request to database.
Then after using find() or get() you can get all requests by:
\DB::getQueryLog();
You can getQueryLog into dd() function and see what database queries were made.

Categories