public function AddWishlist($id)
{
$userid = Auth::id();
$check = DB::table('whishlists')
->where('user_id', $userid)
->where('whishlists', $id)
->first();
$data = ['user_id' => $userid, 'product_id' => $id];
if (Auth::check())
{
if ($check)
{
$notification = ['messege' => 'already added whisahlist', 'alert-type' => 'error'];
return redirect()->back()->with($notification);
}
else
{
DB::table('whishlists')->insert($data);
$notification = [
'messege' => 'Suucessafully added whisahlist',
'alert-type' => 'success'
];
return redirect()->back()->with($notification);
}
}
else
{
$notification = array(
'messege' => 'At first login your account',
'alert-type' => 'error'
);
return redirect()->back()->with($notification);
}
}
Problem
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'whishlists' in 'where clause' (SQL: select * from `whishlists` where `user_id` = 1 and `whishlists` = 37 limit 1)
the logic will be to change
->where('whishlists', $id)
with
->where('whishlist_id', $id)
if its not working, Please show us the table whishlists
Note:
if $id is the id of the wishlist, i think this line should be reviewed :
$data = ['user_id' => $userid, 'product_id' => $id];
because it set 'product_id' with the whishlist id
Related
public function store(OrderCreateRequest $request)
{
$formField = $request->validated();
$totalAmount = 0;
// get the price of products of product_id using whereIn
$products = DB::table('products')
->select('price')
->whereIn('id', $formField['product_id'])
->get();
foreach ($products as $product) {
$totalAmount += $product->price;
}
$order = Order::create([
'product_id' => $formField['product_id'],
'user_id' => Auth::id(),
'total' => $totalAmount,
'status' => false,
]);
$response = [
'message' => 'order placed',
'created' => new OrderResource($order),
];
return response($response, 201);
}
what is wrong with this piece of code?
error => Illuminate\Database\Grammar::parameterize(): Argument #1 ($values) must be of type array, int given,
You are giving an int to whereIn(), whereIn is a function to check a column value against multiple values.
->whereIn('status', ['pending', 'completed'])
In your case either product_id should be an array, or you should just use a simple where().
->where('id', $formField['product_id'])
In my Laravel-8, I have this code:
public function add(Request $request){
if( $request->ajax() ){
$rules = array(
'first_name.*' => 'required',
'country.*' => 'required'
);
$error = Validator::make($request->all(),$rules);
if($error->fails()){
return response()->json([
'error' => $error->errors()->all(),
]);
}
$first_name = $request->first_name;
$country = $request->country;
for( $count = 0; $count < count($first_name); $count++ ){
$data = array(
'first_name' => $first_name[$count],
'country' => $country[$count],
);
$insert_data[] = $data;
}
DynamicField::insert($insert_data);
return response()->json([
'success' => 'Data added Successfully',
]);
}
}
This successfully Inserts record.
How do I do the update to this same code?
I mean public function update ...
I'm creating a pivot tables for 3 columns
my pivot table name is : category_post_pad
category_id| post_id | pad_id
-----------|----------|--------
1 | 1 | 3
1 | 4 | 1
2 | 2 | 1
Each post sent by the user includes a category and an pad
Post Model
public function categories()
{
return $this->belongsToMany(Category::class,'category_post_pad','post_id','category_id');
}
public function pads()
{
return $this->belongsToMany(Pad::class,'category_post_pad','post_id','pad_id');
}
category model:
public function posts()
{
return $this->belongsToMany(Post::class,'category_post_pad','category_id','post_id');
}
pad model:
public function posts()
{
return $this->belongsToMany(Post::class,'category_post_pad','pad_id','post_id');
}
PostsController
public function store(Request $request)
{
$data = $request->all();
$post = Post::create($data);
if ($post && $post instanceof Post) {
$category = $request->input('categories');
$pad = $request->input('pads');
$post->categories()->attach([$category],[$pad]);
return redirect()->back();
}
}
but show me this error
SQLSTATE[42S22]: Column not found: 1054 Unknown column '0' in 'field list' (SQL: insert into category_post_pad (category_id, post_id, 0) values (3, 104, 2))
how to fixit?
Try using the following code as the store function in your PostsController:
$request->validate([
'title' => 'required',
'body' => 'required',
'category_post_pad.*.category_id' => 'required|array|integer',
'category_post_pad.*.post_id' => 'required|array|integer',
'category_post_pad.*.pad_id' => 'required|array|integer',
]);
$date = [
'title' => $request->input('title'),
'body' => $request->input('body'),
];
$post = Post::create($date);
if ($post){
if (count($request->input('category_id')) > 0){
$date2 = array();
foreach ( $request->input('category_id') as $key => $value ){
$postCategory = array(
'post_id' => $post->id,
'category_id' => (int)$request->category_id[$key],
'pad_id' => (int)$request->pad_id[$key],
);
array_push($date2, $postCategory);
}
$post->categories()->attach($date2);
return redirect()->back();
}
}
Then, inside of your Post model change your catagory relation:
public function categories()
{
return $this->belongsToMany(Category::class,'category_post_pad','post_id','category_id')->withPivot('pad_id');
}
Here is my code in the controller.
I want multiple data to insert into a database but I have a problem with Array:
public function postCreate(Request $request)
{
$data = $request->all();
$lastid = Tr_header::create($data)->id;
if (count($request->id_product) > 0)
{
foreach($request->id_product as $item => $value)
$datax = array(
'id_tr_header' => $lastid,
'id_product' => $request->id_product[$item],
'qty' => $request->qty[$item],
'date_kembali' => $request->date_kembali[$item],
'information' => $request->information[$item],
);
Tr_detail::insert($datax);
}
return redirect()->back();
If you want to insert multiple row at a time try this:
{
$data = $request->all();
$lastid = Tr_header::create($data)->id;
if (count($request->id_product) > 0)
{
$datax = [];
foreach($request->id_product as $item => $value)
array_push($datax ,[
'id_tr_header' => $lastid,
'id_product' => $request->id_product[$item],
'qty' => $request->qty[$item],
'date_kembali' => $request->date_kembali[$item],
'information' => $request->information[$item],
]);
Tr_detail::insert($datax);
}
return redirect()->back();
You are overwritting your $datax variable, you need to create an array of arrays to pass on to your insert() function:
public function postCreate(Request $request)
{
$data = $request->all();
$lastid = Tr_header::create($data)->id;
if (count($request->id_product) > 0) {
$datax = [];
foreach ($request->id_product as $item => $value)
$datax[] = array(
'id_tr_header' => $lastid,
'id_product' => $request->id_product[$item],
'qty' => $request->qty[$item],
'date_kembali' => $request->date_kembali[$item],
'information' => $request->information[$item],
);
Tr_detail::insert($datax);
}
return redirect()->back();
}
I’m working on laravel excel import. The data can be loaded using
$data = Excel::load($path)->get(); command. But, when i try to loop through $data object and put it in $insert[], some fields remaining empty.
my import function look like
public function import(request $request) {
$path = $request->file('select_file')->getRealPath();
$data = Excel::load($path)->get();
if(!empty($data) && $data->count()){
foreach ($data as $key => $value) {
$insert[] = [
'Item_name' => $value->Item_name,
'Manufacturer' => $value->Manufacturer,
'Serial_no' => $value->Serial_no,
'Model_no' => $value->Model_no,
'status' => $value->status,
'Price' => $value->Price,
'photo' => $value->photo,
'user_id' => $value->user_id,
'deletedBy' => $value->deletedBy,
'created_at' => $value->created_at,
'updated_at' => $value->updated_at,
];
}
if(!empty($insert)){
$insertData = DB::table('inventories')->insert($insert);
if ($insertData) {
Session::flash('success', 'Your Data has successfully imported');
}else {
Session::flash('error', 'Error inserting the data..');
return redirect()->back();
}
}
}
return redirect()->back();
}
when I dd($data); the result looks as
and the result of dd($insert); looks as
if any friend can help me that why some fields like Item_name, Manufacturer, and Serial_no remain null, would be appreciated.
solved!
all value->item_names must write in lowercase; I thought that it should match with the database table column names.