Laravel / Eloquent - Foreign key always NULL - php

I have to classes of type Model, WptModel and CacheModel.
use Model;
class CacheModel extends Model {
use \October\Rain\Database\Traits\Validation;
public $timestamps = false;
public $rules = [];
public $table = 'cache';
public $belongsTo = [
'WptModel' => [ 'Models\WptModel' ]
];
public $incrementing = FALSE;
}
use Model;
class WptModel extends Model {
use \October\Rain\Database\Traits\Validation;
public $timestamps = false;
public $rules = [];
public $table = 'wpt';
public $hasOne = [
'CacheModel' => [ 'Models\CacheModel' ]
];
public $belongsTo = [
'GpxModel' => [ 'Models\GpxModel' ]
];
public $incrementing = FALSE;
}
For both models I use firstOrNew to build them.
$wptmodel = WptModel::firstOrNew($values);
$cachemodel = CacheModel::firstOrNew($values);
This works like a charm. but when I try to store $cachemodel in $wptmodel and to save $gpxmodel like this:
$wptmodel->CacheModel = $cachemodel;
$gpxmodel->WptModel = $wptmodel;
$gpxmodel->save();
The following error message appears:
"SQLSTATE[23000]: Integrity constraint violation: 1048 Column
'wpt_model_id' cannot be null (SQL: update
cache` set `wpt_model_id` = where cache`.`wpt_model_id`
= 978a1287-3cad-4580-9c95-8ee2ed13b836 and
cache`.`wpt_model_id` is not null)" on line
662 of laravel/framework/src/Illuminate/Database
/Connection.php
Another thing I won't understand is that is process works for the first time (that means I'm able to insert a row into table cache). The error shows up when I execute these statements the second time (update won't work). If I have a look into the corresponding tables than it looks like that every thing is there. All columns have their values. There is definitifly no NULL-value. So I don't understand why Laravel says that _wpt_model_id_ is NULL.
Thanks in advance for your help.
fw.

Related

Eloquent: guard ID when inserting into one database but don't guard it for another database.

I need to guard the ID column when inserting into a database, however I don't want to guard it when inserting into a different database due to needing to manually set the ID, so that the tables are in sync.
However I can't figure out a way to do it, below is what I have got at the moment, however this doesn't work at all as I just get an error:
Field 'id' doesn't have a default value
This is my current model:
<?php
namespace App\Models\Seasonal;
use Illuminate\Database\Eloquent\Model;
class SeasonalBanner extends Model
{
protected $connection = 'dev';
protected $guarded = [ 'id' ];
protected $appends = [ 'period' ];
public static function boot()
{
parent::boot();
self::creating(function($model){
if ($model->connection === 'live') {
$model->guarded = [];
}
});
}
public function dates() {
return $this->hasMany(SeasonalBannerDates::class);
}
public function getPeriodAttribute() {
return [ $this->start, $this->end ];
}
}
The best way in my opinion is not to use $guarded at all in such case. Just set:
protected $guarded = [];
and in your code depending on which database you use, either fill id or not.

Eloquent Many to Many attach() is passing a null model ID

I am developing an application using Laravel and Eloquent ORM, it uses a database filled with event information.
I have successfully implemented attach() in the relevant controllers for both my user and role models.
My Event model can have many Links. A Link can have Many events.
My problem is that the attach() is not supplying the ID of the object it is being called on, instead it supplies null and I receive the following error message:
SQLSTATE[23000]: Integrity constraint violation:
1048 Column 'link_id'
cannot be null (SQL: insert into 'event_link' ('created_at', 'event_id',
'link_id', 'updated_at') values (2018-06-09 11:27:15, 2, , 2018-06-09 11:27:15))
I've triple checked my models and database structure.
I can't even imagine how this error could occur since the id lacking in the SQL query is the id of the object that the attach() method is actually being called on. If I use sync($eventID, false) instead of attach(), the result is the same.
Event table:
Link table:
Event_Link table:
The following is the problematic method in the controller responsible for storing the record and creating an entry in the event_link weak entity.
The $link object is created successfully if the attach() line is commented out, a JSON representation of a link is returned which confirms this (but it lacks the 'id' field).
public function store(StoreLink $request) {
$link = Link::create([
'title' => $request->title,
'url' => $request->url,
]);
if ($request['eventId']) {
// $request->eventId is passed successfully, $link id is not passed.
$link->events()->attach($request->eventId);
}
return response()->json($link, 201);
}
Link Model:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Link extends Model
{
protected $table = 'links';
public $incrementing = false;
public $timestamps = true;
protected $primaryKey = "id";
protected $fillable = ['title', 'url'];
public function events()
{
return $this->belongsToMany('App\Event')->withTimestamps();
}
}
Event Model:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Event extends Model
{
protected $table = 'events';
public $incrementing = false;
public $timestamps = true;
protected $primaryKey = "id";
protected $fillable = ['description', 'date', 'image', 'category_id'];
public function category()
{
return $this->belongsTo('App\Event', 'category_id');
}
public function links()
{
return $this->belongsToMany('App\Link', 'event_link')->withTimestamps();
}
public function history()
{
return $this->belongsToMany('App\User', 'user_history');
}
public function favourites()
{
return $this->belongsToMany('App\User', 'user_favourites');
}
}
The problem is public $incrementing = false;: Remove the line from both your models.
Your id columns are defined as AUTO_INCREMENT, so $incrementing has to be true.

updateOrCreate cause ErrorException in laravel

I wrote a code to login or register users.I used updateOrCreate function to add user if not exist an update a column that is name is verifcode if user exist
controller
users Table:
id(auto increment primary key)|phone|verifcode|timeStmp
---------------------------------
$phone=Input::get('phone');
$code=rand(1001,9999);
$user = new user;
$user = array('phone'=>$phone);
user::updateOrCreate($user,['verifcode' => $code]);
Model:
namespace App;
use Illuminate\Database\Eloquent\Model;
class user extends Model
{
protected $table = 'users';
protected $fillable = array('phone', 'verifcode','timeStmp');
protected $guarded = array('id');
protected $primaryKey = "id";
public $incrementing = false;
const CREATED_AT= false;
const UPDATED_AT = false;
}
with this code i have folowing error:
array_key_exists(): The first argument should be either a string or an integer that point to vendor\laravel\framework\src\Illuminate\Database\Eloquent\Concerns\HasAttributes.php file.
i just know that error point to secound argument of UpdateOrCreate function.
thanks for help
If you see at the Model class of laravel framework code, you can note, that updated_at is stored only when it is not dirty.
if (! $this->isDirty(static::UPDATED_AT)) {
$this->setUpdatedAt($time);
}
But isDirty method checks for nulls only.
public function isDirty($attributes = null)
{
$dirty = $this->getDirty();
if (is_null($attributes)) {
return count($dirty) > 0;
}
if (! is_array($attributes)) {
$attributes = func_get_args();
}
foreach ($attributes as $attribute) {
if (array_key_exists($attribute, $dirty)) {
return true;
}
}
return false;
}
So just substitute false with NULL.
If you take a look at the Laravel documentation, you're just using the updateOrCreate method wrong. Look at the flight example there.
In your case it should look like this:
$phone = \Input::get('phone')
$code = rand(1001, 9999);
$user = User::updateOrCreate(
['phone' => $phone],
['verifcode' => $code, 'timeStmp' => \Carbon::now()->timestamp]
);
finally i solve it. by adding Created_at and Update_at columns to table and adding this two coulmn to $guarded in model like this protected $guarded = array('id','CREATED_AT','UPDATED_AT'); and then problem fixed
thanks for help
$phone=Input::get('phone');
$code=rand(1001,9999);
user::updateOrCreate(
['phone' => $phone]
['verifcode' => $code]
);
See if it is work for you.

Eloquent create says column has no default value using Laravel 5

I have an small API that i want to save into client mysql database.
For this purpose i'm using guzzle.
my controller:
public function index()
{
$http = new \GuzzleHttp\Client;
$res = $http->request('GET', 'http://localhost:8080/api/address');
$addresses = json_decode($res->getBody(),true);
// dd($addresses);
Address::create($addresses);
}
my model:
class Address extends Model
{
protected $primaryKey = 'Adresse';
protected $fillable = ['Adresse', 'Mandant', 'Kategorie', 'Matchcode', 'Name1'];
public $timestamps = false;
}
my migration:
public function up()
{
Schema::create('addresses', function (Blueprint $table) {
$table->integer('Adresse')->primary();
$table->smallInteger('Mandant');
$table->smallInteger('Kategorie')->nullable();
$table->string('Matchcode', 50);
$table->string('Anrede', 50)->nullable();
$table->string('Name1', 50)->nullable();
});
}
my api content:
[
{"Adresse":"1111","Mandant":"0","Kategorie":"0","Matchcode":"fgh8881","Anrede":"Firma","Name1":"Sample name"},{"Adresse":"2399","Mandant":"0","Kategorie":"0","Matchcode":"fgh8882","Anrede":"Firma","Name1":"Sample name 1"}
]
the problem is i get an error
SQLSTATE[HY000]: General error: 1364 Field 'Adresse' doesn't have a
default value (SQL: insert into addresses () values ())
when i limit the api content to one array i can save it without a problem. But if i have more arrays in my api i get this error.
$fillable property on the model is set.
If your primary key is not auto-incrementing the framework needs to know about it.
class Address extends Model
{
protected $primaryKey = 'Adresse';
protected $fillable = ['Adresse', 'Mandant', 'Kategorie', 'Matchcode', 'Name1'];
public $incrementing = false;
public $timestamps = false;
}
Then to add all of the models:
public function index()
{
$http = new \GuzzleHttp\Client;
$res = $http->request('GET', 'http://localhost:8080/api/address');
$addresses = json_decode($res->getBody(),true);
foreach ($addresses as $address) {
Address::create($address);
}
}
Your API content is essentially 2 records. For mass inserting using eloquent you need to use insert not create
So either
1) have your api return 1 result
2) or change Address::create($addresses); to Address::insert($addresses);
Set Adresse be auto-increment as well with primary key and your issue will be solved or If you do not want to set it to auto-increment then assign it to a default value.
check the table structure of addresses whether it have default value NULL need to change default

Laravel save many-to-many relationship in Eloquent mutators

I've got 2 models with a many-to-many relationship. I want to be able to set a specific attribute with an array of ids and make the relationship in the mutator like this:
<?php
class Profile extends Eloquent {
protected $fillable = [ 'name', 'photo', 'tags' ];
protected $appends = [ 'tags' ];
public function getTagsAttribute()
{
$tag_ids = [];
$tags = $this->tags()->get([ 'tag_id' ]);
foreach ($tags as $tag) {
$tag_ids[] = $tag->tag_id;
}
return $tag_ids;
}
public function setTagsAttribute($tag_ids)
{
foreach ($tag_ids as $tag_id) {
$this->tags()->attach($tag_id);
}
}
public function tags()
{
return $this->belongsToMany('Tag');
}
}
<?php
class Tag extends Eloquent {
protected $fillable = [ 'title' ];
protected $appends = [ 'profiles' ];
public function getProfilesAttribute()
{
$profile_ids = [];
$profiles = $this->profiles()->get([ 'profile_id' ]);
foreach ($profiles as $profile) {
$profile_ids[] = $profile->profile_id;
}
return $profile_ids;
}
public function profiles()
{
return $this->belongsToMany('Profile');
}
}
However the setTagsAttribute function isn't working as expected. I'm getting the following error: SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'profile_id' cannot be null (SQL: insert intoprofile_tag(profile_id,tag_id) values (?, ?)) (Bindings: array ( 0 => NULL, 1 => 1, ))
You can't attach many-to-many relations until you've saved the model. Call save() on the model before setting $model->tags and you should be OK. The reason for this is that the model needs to have an ID that Laravel can put in the pivot table, which needs the ID of both models.
It looks like you're calling the function incorrectly or from an uninitialized model. The error says that profile_id is NULL. So if you're calling the function as $profile->setTagsAttribute() you need to make sure that $profile is initialized in the database with an ID.
$profile = new Profile;
//will fail because $profile->id is NULL
//INSERT: profile->save() or Profile::Create();
$profile->setTagsAttribute(array(1,2,3));
Additionally, you can pass an array to the attach function to attach multiple models at once, like so:
$this->tags()->attach($tag_ids);
You can also pass it the model instead of the ID (but pretty sure array of models won't work)
Try using the sync method:
class Profile extends Eloquent {
protected $fillable = [ 'name', 'photo', 'tags' ];
protected $appends = [ 'tags' ];
public function getTagsAttribute()
{
return $this->tags()->lists('tag_id');
}
public function setTagsAttribute($tag_ids)
{
$this->tags()->sync($tagIds, false);
// false tells sync not to remove tags whose id's you don't pass.
// remove it all together if that is desired.
}
public function tags()
{
return $this->belongsToMany('Tag');
}
}
Don't access the tags through the tags() function, rather use the tags property. Use the function name if you want to pop additional parameters onto the relationship query and the property if you just want to grab the tags. tags() works in your getter because you're using get() on the end.
public function setTagsAttribute($tagIds)
{
foreach ($tagIds as $tagId)
{
$this->tags->attach($tagId);
}
}

Categories