store foreign key as zero instead of it's value


store foreign key as zero instead of it's value



I have a table that all the values are foreign keys ,when I store these values it save it as zero not the value that I chose ,


public function create()
{
$type=type::query()->pluck('type');
$color=color::query()->pluck('colore');
$region=region::query()->pluck('country');
$size=size::query()->pluck('size');
$brand=brand::query()->pluck('company');
//$price=new_product::query()->pluck('price');
return view('sale',compact('type','color','region','size','brand'));
}

public function store(Request $request)
{

new_product::create($request->all());
return redirect()->route('sale.index');
}



the model:


class new_product extends Model
{
protected $table = 'enter_new_product';
protected $fillable = ['type_id', 'color_id', 'region_id', 'size_id', 'brand_id','price'];

public function type()
{
return $this->hasMany(type::class);
}

public function size()
{
return $this->hasMany(size::class);
}

public function color()
{
return $this->hasMany(color::class);
}

public function region()
{
return $this->hasMany(region::class);
}

public function brand()
{
return $this->hasMany(brand::class);
}
}



the sale view


{!! Form::open(['route' => 'sale.store', 'method' => 'post','files'=>true]) !!}

<div class="form-group">
{!! Form::label('type_id', 'اسم الصنف') !!}
{!!Form::select('type_id',$type ,null, ['class'=>'form-control','placeholder'=>'ادخل اسم الصنف '])!!}
</div>

<div class="form-group">
{!! Form::label('color_id', 'اللون') !!}
{!!Form::select('color_id', $color,null,['class'=>'form-control','placeholder'=>'ادخل اللون '])!!}
</div>
{!! Form::submit('اضافة',array('class'=>'btn btn-primary btn-lg'))!!}
{!!Form::close()!!}



it's a part of the view not all but the region,size and brand are the same as the color and type>





Please post the result of dd($request->all());.
– Jonas Staudenmeir
Jul 1 at 0:16


dd($request->all());





@JonasStaudenmeir array:7 [▼ "_token" => "m0ew5LonZDGdvDGBt1Vsrbk6cHXCz2nR7gpueqle" "type_id" => "1" "color_id" => "1" "region_id" => "4" "size_id" => "0" "brand_id" => "0" "price" => "120" ] this is the json result . i added a size and brand but it count them as zero , and if i changed the request with other values the zero mostly the result of any other columns .i don't know why .?
– hala
Jul 1 at 18:55





What's the result of dd((new new_product($request->all()))->getAttributes());?
– Jonas Staudenmeir
Jul 1 at 19:53


dd((new new_product($request->all()))->getAttributes());





@JonasStaudenmeir array:6 [▼ "type_id" => "0" "color_id" => "1" "region_id" => "6" "size_id" => "0" "brand_id" => "2" "price" => "760" ]
– hala
Jul 1 at 20:02





Which columns are 0? All of them?
– Jonas Staudenmeir
Jul 1 at 20:04


0




1 Answer
1



It would be nice to know how your request is looking (dd($request->all()) as a comment mentioned before, however I see one problem already, which probably would not help your main problem: The product has the foreign key so it does not have many of the regions and brands and the other stuff. The product hasOne of these relations.


dd($request->all()


hasOne





array:7 [▼ "_token" => "m0ew5LonZDGdvDGBt1Vsrbk6cHXCz2nR7gpueqle" "type_id" => "1" "color_id" => "1" "region_id" => "4" "size_id" => "0" "brand_id" => "0" "price" => "120" ] this is the json result . i added a size and brand but it count them as zero , and if i changed the request with other values the zero mostly the result of any other columns .i don't know why .?
– hala
Jul 1 at 15:34






I would try to convert the types to INT and would not use the $request->all() rather try to write out $product = new Product::create(['type_id' = $request['type_id'], ...]); $product->save(); which is longer, but you can see what happening better. Yes, maybe you do not need save() but I use it always, just in case. And name the model Product or NewProduct, models are always camel case and mostly one word. For the zeroes: how does the view looks like? And if posting the view (you can update the question up here) please include the content of the sale variable.
– Levente Berky
Jul 2 at 18:53


$request->all()


$product = new Product::create(['type_id' = $request['type_id'], ...]); $product->save();


save()


Product


NewProduct


sale





You can always try to create the NewProduct and then create the associations like described here Link.
– Levente Berky
Jul 2 at 18:53


NewProduct





i did what you asked me to do ,but i got an error , SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (my_queen.enter_new_product, CONSTRAINT enter_new_product_ibfk_1 FOREIGN KEY (type_id) REFERENCES type (id) ON DELETE CASCADE ON UPDATE CASCADE) (SQL: insert into enter_new_product (type_id, color_id, region_id, brand_id, price, updated_at, created_at) values (0, 1, 0, 0, 670, 2018-07-02 21:13:47, 2018-07-02 21:13:47)) i uploaded a part of the view not all ,stack overflow refused too much code
– hala
Jul 2 at 21:17


my_queen


enter_new_product


enter_new_product_ibfk_1


type_id


type


id


enter_new_product


type_id


color_id


region_id


brand_id


price


updated_at


created_at





Which method did you used? How did you get the values? You cannot upload 0 to foreign key because it has to be NULL or should be existing value in the other table.
– Levente Berky
yesterday


0


NULL






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

List of Kim Possible characters

Audio Livestreaming with Python & Flask

NSwag: Generate C# Client from multiple Versions of an API