display the users order history (laravel) - laravel

I want to create a page that shows users what they bought but I really have no idea how can I do this!
and this is cart view and I don't have cart model:
public function addtocart(Request $request){
$data = $request->all();
if (empty($data['user_email'])){
$data['user_email'] = ' ';
}
$user_id = Auth::id();
$session_id = Session::get('session_id');
if (empty($session_id)){
$session_id = Str::random(40);
Session::put('session_id' , $session_id);
}
DB::table('cart')->insert(['product_id' => $data['product_id'] , 'product_name' => $data['product_name'], 'user_id'=>$user_id,
'product_price' => $data['product_price'], 'qty' => $data['qty'], 'user_email' => $data['user_email'] , 'session_id' => $session_id ]);
return redirect('cart');
}
public function cart(){
$user_id = Auth::user()->id;
$session_id = Session::get('session_id');
$userCart = DB::table('cart')->where(['session_id'=>$session_id])->get();
foreach ( $userCart as $key=>$product){
$productDetail = Singleproduct::where('id' , $product->product_id)->first();
$userCart[$key]->image = $productDetail->image;
}
return view('UI.store.cart' , compact('userCart'));
}
can anyone help me in it plz?

To get a list of orders for a user you can do:
$history = DB::table('cart')
->where('user_id', auth()->id())
->join('products', 'products.id', '=', 'cart.product_id')
->select('products.name', 'cart.quantity', 'cart.product_price')
->get();
Then it's up to you to output it how you want

Related

Image Update Problem Using Laravel Query Builder

My All Data has been updated successfully but Image not update and old image not remove. I am using Query Builder.
Can anyone tell me what to do?
My Controller are given below
I want to know something new.
public function update(Request $request,$id)
{
$validatedData = $request->validate([
'name' => 'required|max:255',
'title' => 'required',
'details' => 'required',
]);
$data = array();
$data['name'] = $request->name;
$data['title'] = $request->title;
$data['details'] = $request->details;
$data['status'] = $request->status;
$image=$request->photo;
if ($image) {
$image_name = str_random(20);
$ext = strtolower($image->getClientOriginalExtension());
$image_full_name = $image_name.'.'.$ext;
$upload_path = 'public/upload/event';
$image_url = $upload_path.$image_full_name;
$success = $image->move($upload_path,$image_full_name);
if ($success) {
$data['photo'] = $image_url;
$img = DB::table('events')->where('id',$id)->first();
$image_path = $img->photo;
$done = unlink($image_path);
$user = DB::table('events')->where('id',$id)->update($data);
if ($user) {
$notification = array(
'messege' => 'Data Updated Successfully',
'alert-type' => 'success'
);
return redirect('admin/event')->with($notification);
}else{
return redirect()->back();
}
}
}else{
return redirect()->back();
}
//return redirect('admin/event')->with($notification);
}

uploading image and got error in laravel "Undefined variable: data"

I am trying to update the product and for that I am updating the pictures. I meant if user want to add more pictures to it but when I do this I get below error
Undefined variable: data
Also I want to restrict the total uploading pictures to max 3 pictures or 2 pictures and 1 video
When I add a video it does not show like it does not play
Any help would be great. Thank for the help in advance
public function editProduct($id, Request $request) {
if(Auth::check()) {
$pn = Input::get('pn');
$desc = Input::get('desc');
$price = Input::get('price');
// $products = Product::all();
$products = Product::where('id', $id)->first();
// foreach($products as $p) {
if(($products->user_id == Auth::user()->id) && ($products->id == $id)) {
$this->validate($request, [
'filename.*' => 'required|max:5000',
// 'filename.*' => 'image|mimes:jpeg,png,jpg,mp4|max:5000'
]);
if($request->hasfile('filename'))
{
foreach($request->file('filename') as $image)
{
$name = $image->getClientOriginalName();
$filename = time(). '-' . $image;
$file_path = $image->move(public_path().'/assets/images/', $name);
$data[] = $name;
}
}
$new_name = json_encode($data);
$products = Product::where('user_id', Auth::user()->id)
->where('id', $id)->update([
'product_name' => $pn,
'description' => $desc,
'price' => $price,
'filename' => $new_name,
]);
}
} else {
Session::flash("message", "OOPS! You dont have permission to edit the items. Please login first.");
return redirect("/register-user");
}
I was missing enctype="multipart/form-data" files=true in my form in the view so it was not getting a file from the blade so got it fixed now

How to use Laravel with 3 where clause in function?

i have a function to show data with 3 where clause condition . but this output is invalid . i have 4 data and just show 1 . this function like this :
public function alkes_user()
{
$user_id = Auth::user()->id;
$unit = User::where('roles_id' , 1)->pluck('nama_unit', 'id');
$alat = Alat::with('users')
->where('jenis' ,'Alkes')
->where('user_id', $user_id)
->where('is_active', 'true')
->orderBy('created_at', 'desc')->paginate(10);
return view('users.alkes_user',['alat' => $alat , 'unit' => $unit ,'count' => $count ]);
}
this data only showing 1 , but in database i have so many data . what wrong and how to solve this ? thanks you
Rewrite your query like this if you want multiple where condition:
public function alkes_user(){
$user_id = Auth::user()->id;
$unit = User::where('roles_id' , 1)->pluck('nama_unit', 'id');
$alat = Alat::with('users')
->where([['jenis' ,'Alkes'],['user_id', $user_id],['is_active', 'true']])
->orderBy('created_at', 'desc')->paginate(10);
return view('users.alkes_user',['alat' => $alat , 'unit' => $unit ,'count' => $count ]);
}
Simple Solution
$alat= Alat::with(array('users' => function($query) {
$query->where([['jenis' ,'Alkes'],['user_id', $user_id],[is_active', 'true']]);
}))->orderBy('created_at', 'desc')->paginate(10);
$user_id = Auth::user()->id;
$unit = User::where('roles_id' , 1)->pluck('nama_unit', 'id');
$alat = Alat::with('users')->where([
['jenis' ,'=','Alkes'],
['user_id','=',$user_id],
['is_active','=','true']
])->orderBy('created_at', 'desc')->paginate(10);
return view('users.alkes_user',['alat' => $alat , 'unit' => $unit ,'count' => $count ]);

How to use the same insert_id() in CodeIgniter from one function to another function [duplicate]

I have a problem with insert_id. I'm using CodeIgniter. I have a method in which I insert some values and in it I return
$insert_id=$this->db->insert_id();
return $insert_id;`
I want to use this value in another method in the same model. I tried that: $insert_id=$this->add_teacher_subject(); but in this way first method runs again and I doesn't receive last_insert_id , but this value +1. Please, tell me how could I solve this problem?
My model is:
public function add_teacher_subject() {
$date = new DateTime("now");
$data=array(
'teacher_id'=>$this->input->post('teacher'),
'user_id'=>$this->session->userdata['user_id'],
'created_at'=>$date->format('Y-m-d H:i:s')
);
$this->db->insert('student_surveys',$data);
$insert_id=$this->db->insert_id();
return $insert_id;
}
public function survey_fill()
{
$insert_id=$this->add_teacher_subject();
if ($this->input->post('answer')) {
$answers= $this->input->post('answer');
if (null !==($this->input->post('submit'))) {
$date = new DateTime("now");
foreach($answers as $question_id=>$answer)
{
$data = array(
'user_id'=>$this->session->userdata['user_id'],
'question_id'=>$question_id,
'answer'=>$answer,
'student_survey_id' => $insert_id
);
$this->db->insert('survey_answers', $data);
}
}
You can use SESSION[] for keeping last insert id:
public function add_teacher_subject() {
$date = new DateTime("now");
$data = array(
'teacher_id' => $this->input->post('teacher'),
'user_id' => $this->session->userdata['user_id'],
'created_at' => $date->format('Y-m-d H:i:s')
);
$this->db->insert('student_surveys', $data);
$insert_id = $this->db->insert_id();
$newdata = array('insert_id' => $insert_id);
$this->session->set_userdata($newdata);
return $insert_id;
}
public function survey_fill() {
$insert_id = $this->session->userdata('insert_id');
if ($this->input->post('answer')) {
$answers = $this->input->post('answer');
if (null !== ($this->input->post('submit'))) {
$date = new DateTime("now");
foreach ($answers as $question_id => $answer) {
$data = array(
'user_id' => $this->session->userdata['user_id'],
'question_id' => $question_id,
'answer' => $answer,
'student_survey_id' => $insert_id
);
$this->db->insert('survey_answers', $data);
}
}
}
}

How to set the logged in user ID on a session (CodeIgniter)?

I am having trouble, getting the logged in user ID from the database...
This is my controller code :
function validation(){
$this->load->model('users_model');
$query = $this->users_model->validate();
if ($query){
$this->users_model->get_userID($this->input->post('email'));
$data = array(
'email' => $this->input->post('email'),
'is_logged_in' => true,
'user_id' => $user_id
);
$this->session->set_userdata($data);
redirect('home');
}
else {
$this->index();
}
}
This is my model function where I return the user id from the database:
function get_userID($email){
$this->db->where('email',$email);
$query = $this->db->get('users');
foreach ($query->result() as $row)
{
$user_id = $row->id;
}
return $user_id;
}
Everything works perfect, except that $user_id returns empty... What am I doing wrong?
Try:
$user_id= $this->users_model->get_userID($this->input->post('email'));
try modifying the model function to this -
function get_userID($email){
$this->db->where('email',$email);
$query = $this->db->get('users')->row_array(); // retrieves only first-row.
return $query['id'];
}
In controller the function needs just a bit of modification as follows -
function validation(){
$this->load->model('users_model');
$query = $this->users_model->validate();
if($query){
//$user_id was not assigned before :)
$user_id = $this->users_model->get_userID($this->input->post('email'));
$data = array(
'email' => $this->input->post('email'),
'is_logged_in' => true,
'user_id' => $user_id
);
$this->session->set_userdata($data);
redirect('home');
}
else {
$this->index();
}
}
hope it helps,
faisal ahmed
blog / site

Resources