I have some trouble getting the foreign key.
My Migrations looks like this (shortened them):
<?php
class CreateProductsTable extends Migration
{
public function up()
{
Schema::create('products', function (Blueprint $table) {
$table->increments('id');
$table->string('email');
$table->string('title');
$table->string('filename');
$table->integer('number_of_chapters');
$table->text('input_mpg');
$table->timestamps();
});
}
public function down()
{
Schema::drop('products');
}
}
<?php
class CreateChaptersTable extends Migration
{
public function up()
{
Schema::create('chapters', function (Blueprint $table) {
$table->increments('id');
$table->integer('product_id')->unsigned();
$table->time('input-chapter-start1');
$table->time('input-chapter-end1');
$table->timestamps();
});
Schema::table('chapters', function($table) {
$table->foreign('product_id')->references('id')->on('products');
});
}
public function down()
{
Schema::drop('chapters');
}
}
And my 2 Model like this:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Chapters extends Model
{
protected $table = 'chapters';
protected $fillable = ['input-chapter-start1', 'input-chapter-end1'];
public function product()
{
return $this->belongsTo('App\Product');
}
}
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Product extends Model
{
protected $table = 'products';
protected $fillable = ['email', 'title', 'filename', 'inputMpg', 'number_of_chapters'];
public static $rules = [
'email' => 'required|email|max:50',
'title' => 'required|max:50',
'filename' => 'required|max:50',
'input_mpg' => 'required'
];
public function Chapters()
{
return $this->hasMany('App\Chapters');
}
}
And just save it like this in my Controller
$product->save();
$Chapters->save();
And get following error:
SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or
update a child row: a foreign key constraint fails
(generator.chapters, CONSTRAINT chapters_product_id_foreign
FOREIGN KEY (product_id) REFERENCES products (id)) (SQL: insert
into chapters (input-chapter-start1, input-chapter-end1,
updated_at, created_at) values (12:12:12, 12:12:12, 2016-04-25
11:41:31, 2016-04-25 11:41:31))
EDIT
Controller looks like this:
namespace App\Http\Controllers;
class ProductController extends Controller
{
protected $request;
public function request(Request $request)
{
$this->request = $request;
}
public function createProduct(Request $request)
{
$product = new Product;
$Chapters = new Chapters($request->all());
$data = $request->all();
$projectEmail = $request->input('email');
$projectTitle = $request->input('title');
$projectFile = $request->input('filename');
$projectChapters = $request->input('number_of_chapters');
$validator = Validator::make($request->all(), Product::$rules);
if($validator->fails())
{
return Redirect::back()->withInput()->withErrors($validator);
}
$product->fill($data);
if($product->save())
{
$Chapters->product()->associate($product);
$Chapters->save();
return redirect()->route('root')->with('message', 'success')->withInput();
}
else
{
return redirect()->route('newProduct')->with('message', 'Error')->withInput();
}
}
}
Edit I tried Samsquanch suggestion:
And added this in my controller:
$product->save();
$Chapters->product()->associate($product);
$Chapters->save();
but still get this error message:
BadMethodCallException in Builder.php line 2093: Call to undefined
method Illuminate\Database\Query\Builder::products()
The problem is that you're not telling Laravel or MySQL what the foreign key should be.
You have two options here (both from the documentation: https://laravel.com/docs/5.1/eloquent-relationships#inserting-related-models)
The first option would be to save chapters through product:
$chapters = $product->chapters()->saveMany($Chapters); // or just->save() if it's only one
The second (and how I generally do it) would be to use associate() which relies on the belongsTo relationship in your Chapters model:
$product->save();
$Chapters->product()->associate($product);
$Chapters->save();
There's also a third, but not recommended, option of just setting the foreign key yourself manually.
Edit:
$product->chapters()->associate($Chapters);
$product->save();
Related
hope you are safe and doing well. I am facing an issue with laravel relational table. I have three table, USER, CLIENT ,ORDER and CAR_PARKING. Now Client is related to user and ORDER is related to both Client and USER while CAR_PARKING related to ORDER only. What i am facing like issue is that when I am trying to update table ORDER it says #General error: 1364 Field 'start_date' doesn't have a default value#
Below are my different table models and controller
public function updateOrderCarParking(Request $request, $id)
{
if (Auth::check()) {
$carParkingData = $request->only('removed', 'removed_date');
$validateCarParking = Validator::make($carParkingData, [
'removed' => 'required|boolean',
'removed_date' => 'nullable'
]);
if ($validateCarParking->fails()) {
return response()->json($validateCarParking->errors(), 422);
}
$orderData = $request->only('paid', 'amount_paid', 'overdue', 'currency', 'user_id');
$validateOrder = Validator::make($orderData, [
'amount_paid' => 'required|regex:/^\d*+(\.\d{1,2})?$/',
'currency' => [
'required',
Rule::in(['USD', 'CAD'])
],
"paid" => "required|boolean",
'overdue' => 'regex:/^\d*+(\.\d{1,2})?$/'
]);
if ($validateOrder->fails()) {
return response()->json($validateOrder->errors(), 422);
}
$updateCarParking = CarParking::updateOrCreate([
'removed' => $request->removed,
'removed_date' => $request->removed_date,
]);
$order = Order::find($id);
$order->carParkings()->save($updateCarParking);
$updateOrder = Order::find($id);
$updateOrder->amount_paid = $request->amount_paid;
$updateOrder->paid = $request->paid;
$updateOrder->currency = $request->currency;
$updateOrder->overdue = $request->overdue;
$updateOrder->user_id = Auth::user()->id;
$updateOrder->save();
if ($order && $updateOrder) {
return response()->json([
'success' => true,
'message' => 'Order updated successfully',
'data' => $order
], Response::HTTP_OK);
}
} else {
return response()->json([
'success' => false,
'message' => 'Can not update',
], Response::HTTP_UNAUTHORIZED);
}
}
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Order extends Model
{
use HasFactory;
protected $fillable = ['order_type', 'amount_paid','client_id', 'user_id', 'price', 'currency', 'paid', 'overdue'];
public function user()
{
return $this->belongsTo(User::class);
}
public function client()
{
return $this->belongsTo(Client::class);
}
public function carParkings(){
return $this->hasMany(CarScrap::class);
}
}
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class CarParking extends Model
{
use HasFactory;
protected $fillable = ['start_date', 'end_of_free_charge', 'order_id', 'removed', 'removed_date'];
public function order()
{
return $this->belongsTo(Order::class);
}
}
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateOrdersTable extends Migration
{
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
Schema::create('orders', function (Blueprint $table) {
$table->id();
$table->decimal('price', 8, 2);
// $table->date('start_date');
$table->enum('order_type', ['Storage rent', 'Forklift', 'Ramp', 'Car Parking', 'Car Scrap', 'Shipping']);
$table->enum('currency', ['USD', 'CAD']);
$table->boolean('paid')->default('0');
$table->decimal('amount_paid')->default(0);
$table->decimal('overdue')->nullable();
$table->foreignId('client_id')->constrained('clients')->onDelete('cascade');
$table->foreignId('user_id')->constrained('users')->onDelete('cascade');
$table->timestamps();
});
Schema::enableForeignKeyConstraints();
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::table('orders', function (Blueprint $table) {
$table->dropConstrainedForeignId("client_id");
$table->dropConstrainedForeignId("user_id");
});
Schema::dropIfExists('orders');
}
}
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateCarParkingsTable extends Migration
{
public function up()
{
Schema::create('car_parkings', function (Blueprint $table) {
$table->id();
$table->date('start_date');
$table->date('end_of_free_charge');
$table->boolean('removed')->default("0");
$table->date('removed_date')->nullable();
$table->timestamps();
$table->foreignId('order_id')->constrained('orders')->onDelete('cascade');
});
Schema::enableForeignKeyConstraints();
}
public function down()
{
Schema::table('car_parkings', function (Blueprint $table) {
$table->dropConstrainedForeignId("order_id");
});
Schema::dropIfExists('car_parkings');
}
}
What am I doing wrong?
Replace your existing method with this:
class CreateCarParkingsTable extends Migration
{
public function up()
{
Schema::create('car_parkings', function (Blueprint $table) {
$table->id();
$table->date('start_date')->nullable();
$table->date('end_of_free_charge');
$table->boolean('removed')->default("0");
$table->date('removed_date')->nullable();
$table->timestamps();
$table->foreignId('order_id')->constrained('orders')->onDelete('cascade');
});
Schema::enableForeignKeyConstraints();
}
public function down()
{
Schema::table('car_parkings', function (Blueprint $table) {
$table->dropConstrainedForeignId("order_id");
});
Schema::dropIfExists('car_parkings');
}
}
If you get error for any other field then make it nullable as I had done in start_date
Set start_date to Nullable in Database Migration.
Example:
$table->date('start_date')->nullable();
How can we imagine that only two lines of code have solved the errors. No need of setting nullable on any column. I got the solution by doing this code below:
$client = Client::findOrFail($client->id);
$client->update($request->all());
But i put in the model the relationships function. I meant in the CLIENT model i put
public function orders(){
return $this->hasMany(Order::class);
}
same thing for order model
I'm trying to delete a user but there is ForeignKey constraints that I also need to delete.
In my User model, I have
class User extends Authenticatable
{
use HasApiTokens, Notifiable;
public $incrementing = false;
protected $fillable = [
'name',
'email',
'password',
'timezone',
'profile_picture',
'notification_key'
];
protected $hidden = [
'password',
'pivot',
'admin'
];
public static function boot()
{
parent::boot();
static::creating(function ($instance) {
$instance->id = Uuid::uuid4();
});
}
public function groups()
{
return $this->belongsToMany(Group::class)
->withPivot('user_role')
->withTimestamps();
}
public function events()
{
return $this->belongsToMany(Event::class);
}
}
migrations for relation are
class CreateEventUserTable extends Migration
{
public function up()
{
Schema::create('event_user', function (Blueprint $table) {
$table->uuid('event_id');
$table->uuid('user_id');
$table->primary(['event_id', 'user_id']);
$table->foreign('event_id')->references('id')->on('events');
$table->foreign('user_id')->references('id')->on('users');
});
}
public function down()
{
Schema::dropIfExists('event_user');
}
}
and
class CreateGroupUserTable extends Migration
{
{
Schema::create('group_user', function (Blueprint $table) {
$table->uuid('group_id');
$table->uuid('user_id');
$table->string('user_role')->nullable();
$table->timestamps();
$table->boolean('owner');
$table->primary(['group_id', 'user_id']);
$table->foreign('group_id')->references('id')->on('groups');
$table->foreign('user_id')->references('id')->on('users');
});
}
public function down()
{
Schema::dropIfExists('group_user');
}
}
so I was trying to delete user like this
public function delete($user)
{
$user = User::findOrfail('id', $user->id);
$res = $user->groups()->events()->delete();
if ($res) {
return response('Success, user was deleted', 204);
} else {
return response()->json(error);
}
}
but still, I'm receiving
Illuminate\Database\QueryException: SQLSTATE[23000]: Integrity constraint violation: 1451 Cannot delete or update a parent row: a foreign key constraint fails (`event_activities`, CONSTRAINT `event_activities_user_id_foreign` FOREIGN KEY (`user_id`) REFERENCES `users` (`id`)) (SQL: delete from `users` where `id` = someID) in file /home/server/vendor/laravel/framework/src/Illuminate/Database/Connection.php on line 664
I was hoping to do deletion in the manner by relationship in the User model, but I'm still receiving an Integrity error, so what is the proper way of doing this?
There are 3 ways to achieve this:
Using detach
$user->groups()->events()->detach();
$user->groups()->detach();
Utilise the deleting event on related models like in this answer
Migrations.
// in user_roles
$table->integer('group_id');
$table->foreign('group_id')->references("id")->on("groups")->onDelete("cascade");
This translates into:
When group 'id' is deleted on 'groups', delete this row.
Apply same thing to groups for events.
Edit:
Looking at your migrations, you implemented 3rd solution, but you seem to forget
->onDelete('cascade');
I'm working on my first Laravel project, and I want to create a REST Api for android application. In my system, I have two tables: categories and images. Table images has column category_id which is a foreign key that references column id on category table.
The categories table
//users table migration
class CreateCategoriessTable extends Migration
{
public function up()
{
Schema::create('categories', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->timestamps();
});
}
...
}
The images table
class CreateImagesTable extends Migration
{
public function up()
{
Schema::create('images', function(Blueprint $table){
$table->increments('id');
$table->string('name')
$table->integer('category_id')->unsigned();
$table->foreign('category_id')
->references('id')
->on('categories')
->onDelete('cascade');
$table->timestamps();
});
}
...
}
In the Images model class I did it:
class Images extends Model
{
protected $fillable = ['name'];
protected $hidden = array('created_at', 'updated_at');
public function category(){
$this->belongsTo('App\Category');
}
}
I also created CategoryResource() class as:
class CategoryResource extends JsonResource
{
public function toArray($request)
{
return [
'id'=> $this->id,
'name' => $this->name,
];
}
}
So, I created a CategoryController to with the API methods, and configured the routes to access the corresponding function. The api/category/ url via GET is redirect to the index function of my controller, and the function is like that:
public function index()
{
$categories = Category::get();
return CategoryResource::collection($categories);
}
With this, I can get the categories table data, but I would like merge the users and images table, and get something like this as response:
[
{
'id': 1,
'name': 'category_name',
'image': 'image_name'
}
]
How I can do this?
First add a hasOne relation in Category model for image like this
Category Model
public function image(){
return $this->hasOne('App\Image');
}
Now specify the relation in your CategoryResource
class CategoryResource extends JsonResource
{
public function toArray($request)
{
return [
'id'=> $this->id,
'name' => $this->name,
'image' => new ImageResource($this->whenLoaded('image'))
];
}
}
Create ImageResource for loading images
class ImageResource extends JsonResource
{
public function toArray($request)
{
return [
'id'=> $this->id,
'name' => $this->image_name,
];
}
}
Finally load images relations with eager load in your controller like this
public function index()
{
$categories = Category::with('image')->get();
return CategoryResource::collection($categories);
}
Model 1:
namespace App;
use Illuminate\Database\Eloquent\Model;
class productDescription extends Model
{
protected $table="ProductDescription";
protected $connection="mysql";
public function productPricing()
{
return $this->belongsTo(priceInfo::class);
}
public function salesPackage()
{
return $this->hasMany(packageModel::class);
}
}
Model2:
class packageModel extends Model
{
//
protected $table="subSalesPackage";
protected $connection="mysql";
public function product_description(){
return $this->belongsTo(productDescription::class);
}
}
Controller:
public function addProductDetails(Request $formdescription,$dataId)
{
$description=new productDescription;
$description->deviceCategoryId=$dataId;
$description->productdescriptionid=$this->getproductDescriptionId();
$description->modelName=$formdescription->input('mname');
$description->batteryType=$formdescription->input('batteryType');
//$description->salesPackage =$formdescription->input('package');
$description->skillSet =$formdescription->input('skillSet');
$description->Colour=$formdescription->input('colour');
$description->Material =$formdescription->input('material');
$description->maxAge=$formdescription->input('maxage');
$description->minAge =$formdescription->input('minage');
//$product->productPricing()-save($priceInfo);
//$product->productDetails()->save($description);
$description->save();
$salesPackage=new packageModel;
$salesPackage->salesPackage=$formdescription->input('package');
**$salesPackage->product_description()->associate($description);**
$salesPackage->save();
//echo("success");
return response()->json([
'modelName' => $formdescription->mname,
'colour' => $formdescription->colour,
'rechargable' => $formdescription->rechargable,
'batteryType' => $formdescription->batteryType
]);
//$description->product()->associate($priceInfo);
}
Migration->productdescription:
public function up()
{
//
Schema::create('ProductDescription', function (Blueprint $table) {
$table->engine='InnoDB';
$table->string('productdescriptionid')->primary();
$table->string('product_id');
$table->string('salesPackage');
$table->timestamps();
$table->index(['productDescriptionId']);
});
}
This is my migration for 1st table(model).It has the primary key as'productdescriptionid'.
Migration->subSalespackage
public function up()
{
//
Schema::create('subSalesPackage', function (Blueprint $table) {
$table->increments('id');
$table->string('product_description_id');
$table->string('salesPackage');
$table->foreign('product_description_id')-
>references('productdescriptionid')->on('ProductDescription');
$table->timestamps();
$table->index(['id']);
});
}
Here I have referred the productdescriptionid as foreign key.And when I add this salespackage table,the values should get added with the value of productdescriptionid(productDescription).
But the error i'm getting is can't able to add or update a child row.
You should try this:
return response()->json([
'SKUID' => $priceInfo->SKUID,
'listingStatus' => $priceInfo->listingStatus,
'MRP' => $priceInfo->MRP,
'sellingPrice' => $priceInfo->sellingPrice,
'id' =>$this->getproductId()
]);
It's a table migrated from https://github.com/lucadegasperi/oauth2-server-laravel
In the table oauth_clients, the field data type of id is varchar(40), not int.
$name = Input::get('name');
$id = str_random(40);
$secret = str_random(40);
$client = new oauthClient;
$client->name = $name;
$client->id = $id;
$client->secret = $secret;
$client->save();
After save(); the $client->id become '0', not the string I assigned.
That makes the following relation table save fail.
$endpoint = new OauthClientEndpoint(array('redirect_uri' => Input::get('redirect_uri));
$client->OauthClientEndpoint()->save($endpoint);
I checked the $client->id: after save, it becomes 0 and I get an error including this one:
(SQL: insert into `oauth_client_endpoints` (`redirect_uri`, `client_id`, `updated_at`, `created_at`) values (http://www.xxxxx.com, 0, 2014-09-01 11:10:16, 2014-09-01 11:10:16))
I manually saved an endpoint to prevent this error for now. But how do I resolve this issue?
Here's my model:
class OauthClient extends Eloquent {
protected $table = 'oauth_clients';
public function OauthClientEndpoint(){
return $this->hasOne('OauthClientEndpoint', 'client_id', 'id');
}
}
class OauthClientEndpoint extends Eloquent {
protected $table = 'oauth_client_endpoints';
protected $fillable = array('redirect_uri');
public function OauthClient(){
return $this->belongsTo('OauthClient', 'client_id', 'id');
}
}
class CreateOauthClientsTable extends Migration {
public function up() {
Schema::create('oauth_clients', function (Blueprint $table) {
$table->string('id', 40);
$table->string('secret', 40);
$table->string('name');
$table->timestamps();
$table->unique('id');
$table->unique(array('id', 'secret'));
});
}
public function down() {
Schema::drop('oauth_clients');
}
}
class CreateOauthClientEndpointsTable extends Migration {
public function up() {
Schema::create('oauth_client_endpoints', function (Blueprint $table) {
$table->increments('id');
$table->string('client_id', 40);
$table->string('redirect_uri');
$table->timestamps();
$table->foreign('client_id')
->references('id')->on('oauth_clients')
->onDelete('cascade')
->onUpdate('cascade');
});
}
public function down() {
Schema::table('oauth_client_endpoints', function ($table) {
$table->dropForeign('oauth_client_endpoints_client_id_foreign');
});
Schema::drop('oauth_client_endpoints');
}
}
When you are setting your own ID and not using auto_increment be sure to add public $incrementing = false; to that model. In your case you want:
class OauthClient extends Eloquent {
public $incrementing = false;
protected $table = 'oauth_clients';
public function OauthClientEndpoint(){
return $this->hasOne('OauthClientEndpoint', 'client_id', 'id');
}
}
This is a tiny red block in the huge Laravel documentation:
Note: Typically, your Eloquent models will have auto-incrementing keys. However, if you wish to specify your own keys, set the incrementing property on your model to false.