eloquent

SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row (Laravel 6)

纵饮孤独 提交于 2021-01-28 08:02:20
问题 What am I doing wrong? Environment: Laravel 6, Homestead (Local), Windows 10 Create External Table (Migration): Schema::create('external', function (Blueprint $table) { $table->increments('id')->unsigned(); $table->foreign('id')->references('order_id')->on('order'); }); Create Order Table (Migration): Schema::create('order', function (Blueprint $table) { $table->increments('id'); $table->unsignedInteger('order_id')->index(); External.php (model): class External extends Model public function

Mass assignment won't handle Null input even when default is set on migration.Any solution to this?

﹥>﹥吖頭↗ 提交于 2021-01-28 07:34:26
问题 I have been using mass assignment a lot. I recently came across this issue, where I create fillable, and also defaults for null values, but on using mass assignment, if my inputs are empty, it returns a "Cannot Be Null" Error. My Model protected $fillable = ['name','status']; My Controller $this->model->create($request->all()); My Migration $table->boolean('status')->default(0); Shouldn't the above mean that when I provide nothing on the input field status , it should default to 0? But column

Mass assignment won't handle Null input even when default is set on migration.Any solution to this?

拜拜、爱过 提交于 2021-01-28 07:25:24
问题 I have been using mass assignment a lot. I recently came across this issue, where I create fillable, and also defaults for null values, but on using mass assignment, if my inputs are empty, it returns a "Cannot Be Null" Error. My Model protected $fillable = ['name','status']; My Controller $this->model->create($request->all()); My Migration $table->boolean('status')->default(0); Shouldn't the above mean that when I provide nothing on the input field status , it should default to 0? But column

Get all records of relationship when parent doesn't exist

試著忘記壹切 提交于 2021-01-28 07:13:04
问题 I have two models. Resource: public function account() { return $this->belongsTo('App\Account'); } Account: public function resources() { return $this->hasMany('App\Resource'); } I want to receive all resources of an account knowing user_id from account Model. Account::where('user_id', Auth::id())->first()->resources()->get(); This works only when account exists. It throws error otherwise: Call to a member function resources() on null . What's the smart way to get all resources in Eloquent?

Wrong timezone with Eloquent Model timestamps on $model->get(), but correct with print_r()

不羁岁月 提交于 2021-01-28 05:44:06
问题 I am experiencing something quite weird for any Eloquent Model with Laravel 7! P.S.: I've run on every test I did: php artisan optimize:clear I don't know what I am missing here! I won't post any code because that's a simple CRUD with Model Bindings. When saving the created_at and updated_at fields, it is correctly saved in the MySQL with my timezome "America/Sao_Paulo". But if I do this in any controler: return $model->get() or return $model->paginate() or Model::all() I get the response: {

Collection Relationship Select columns not working Laravel

拈花ヽ惹草 提交于 2021-01-28 05:24:02
问题 I am trying to select columns (id, title) of the relationship database (product_detail) but it's not working at all. My Query: RoomProduct::select('product_id', DB::raw('SUM(value) as total')) ->whereHas('room.offer', function($sql) use ($offer_id) { $sql->where('id', $offer_id); })->whereHas('product_detail', function($sql) use ($category_id) { $sql->select("id", "title")->with(['category' => function ($query) { $query->where('parent_id', $category_id); }])->orWhere('id', $category_id); })-

Laravel error in creating a table with two timestamp columns

耗尽温柔 提交于 2021-01-28 04:40:23
问题 I created a table in Laravel 6.6 with the following definition. public function up() { Schema::create('quarters', function (Blueprint $table) { $table->integer('quarter_id')->unsigned(); $table->integer('year')->unsigned(); $table->integer('quarter_num')->unsigned(); $table->timestamp('valid_from'); $table->timestamp('valid_to'); // <------ error on this line $table->string('description')->nullable(); $table->timestamps(); $table->primary('quarter_id'); }); } When I run the migration command

Filter categories on AND instead of OR basis in Laravel

与世无争的帅哥 提交于 2021-01-28 00:53:33
问题 I would like to filter journalists on the basis of the categories they write about (e.g. Sports, Music). When the user selects multiple categories, e.g. sports and music, it should show only the journalist which are writing about Sports AND Music. Currently, I have only managed to filter on journalists who are writing about Sports AND/OR Music. The categories selected by the user are represented in an array: And the combination journalists and category combination are defined in a linking

Delete related models in Laravel 6/7

时光总嘲笑我的痴心妄想 提交于 2021-01-27 21:00:17
问题 There are many related questions but unfortunately I can't find working solution I have Laravel model and when this model is deleted I want to delete some related models run custom SQL query when deleting model My Laravel's model class looks like (as you can see models can have different relation types) class ModelA extends Model { public functions modelsB() { return $this->hasMany(ModelB:class); } public functions modelsC() { return $this->belongsToMany(ModelC:class); } // other related

Laravel get data based on foreign key

让人想犯罪 __ 提交于 2021-01-27 17:24:40
问题 So I have 2 tables, TABLE1 and TABLE2. They are linked throug Eloquent: TABLE1 has many TABLE2's, and TABLE2 has one TABLE 1. They are linked with foreign keys as such: in TABLE2 table1_id -> links to -> TABLE1 id. How do I retrieve the entire table data or TABLE 2, + replace table1_id with a column of table1 (let's say by booktitle for example)? Sorry for the abstraction, but this is the best way I can explain it? :D 回答1: The easiest way is to make use of Eloquent's eager loading and fetch