I want to know how get this working in laravel:
I have two tables: "orders" and "delivery_boys".
When an order is created I want to get "id" in table "delivery_boys" that correspond to the "store_id" in the "order" table.
which looks like that in mysql
SELECT `id` FROM `delivery_boys` WHERE store_id = '3'
Thanks for your help.
I use this function:
protected $table = 'orders';
public function addNew($data)
{
$user = AppUser::find($data['user_id']);
if(isset($data['address']) && $data['address'] > 0)
{
$address = Address::find($data['address']);
}
else
{
$address = new Address;
}
$add = new Order;
$add->user_id = $data['user_id'];
$add->store_id = $this->getStore($data['cart_no']);
$add->d_boy = $add->?????; <---- here i want to add from "delivery_boys" table the "id" that correspond to the "store_id" added just above from the "order" table ---->
$add->name = $user->name;
$add->email = $user->email;
$add->phone = $user->phone;
$add->address = $address->address;
$add->lat = $address->lat;
$add->lng = $address->lng;
$add->address = $address->address;
$add->d_charges = $this->getTotal($data['cart_no'])['d_charges'];
$add->discount = $this->getTotal($data['cart_no'])['discount'];
$add->total = $this->getTotal($data['cart_no'])['total'];
$add->payment_method = $data['payment'];
$add->payment_id = isset($data['payment_id']) ? $data['payment_id'] : 0;
$add->type = isset($data['otype']) ? $data['otype'] : 1;
$add->notes = isset($data['notes']) ? $data['notes'] : null;
$add->save();
run this before $add = new Order;
$d_boy = DB::table('delivery_boys')
->select('id')
->where('store_id', $this->getStore($data['cart_no']))
->first();
and then add it where you need it:
$add->d_boy = $d_boy->id;
$id= DB::table('delivery_boys')->select('id')->where('store_id', '=', $this->getStore($data['cart_no']))->get();
Related
I'm working on a system that has several server-side datatables but i facing issues with 2 joins when i try to order de columns.
I receive the following message when try to sort the columns:
Query error: Column 'notes' in order clause is ambiguous - Invalid query: SELECT *
FROM `tbl_project`
LEFT JOIN `tbl_client` ON `tbl_project`.`client_id`=`tbl_client`.`client_id`
LEFT JOIN `tbl_account_details` ON `tbl_project`.`created_by` = `tbl_account_details`.`user_id`
LEFT JOIN `tbl_notes` ON `tbl_project`.`notes` = `tbl_notes`.`notes_id`
WHERE `tbl_project`.`client_id` = '100'
ORDER BY `notes` DESC
LIMIT 10
This is the code with my query:
$id = $this->input->post("client_id");
$client_details = get_row('tbl_client', array('client_id' => $id));
$draw = intval($this->input->post("draw"));
$start = intval($this->input->post("start"));
$length = intval($this->input->post("length"));
$order = $this->input->post("order");
$search= $this->input->post("search");
$search = $search['value'];
$col = 0;
$dir = "";
if(!empty($order))
{
foreach($order as $o)
{
$col = $o['column'];
$dir= $o['dir'];
}
}
if($dir != "desc" && $dir != "desc")
{
$dir = "desc";
}
$valid_columns = array(
0=>'project_id',
1=>'client',
2=>'fullname',
3=>'notes',
4=>'origen',
5=>'end_date',
6=>'project_status',
7=>'action',
);
if(!isset($valid_columns[$col]))
{
$order = null;
}
else
{
$order = $valid_columns[$col];
}
if($order !=null)
{
$this->db->order_by($order, $dir);
}
$searchQuery = "";
if($search != ''){
$searchQuery = " (tbl_project.project_id like'%".$search."%' OR tbl_project.end_date like'%".$search."%' OR tbl_project.project_status like'%".$search."%' OR tbl_notes.notes like'%".$search."%' OR tbl_notes.eco like'%".$search."%' OR tbl_account_details.origen like'%".$search."%' OR tbl_client.name like'%".$search."%') ";
}
$this->db->select('*');
$this->db->from('tbl_project');
$this->db->join('tbl_client', 'tbl_project.client_id=tbl_client.client_id','left');
$this->db->join('tbl_account_details', 'tbl_project.created_by = tbl_account_details.user_id','left');
$this->db->join('tbl_notes', 'tbl_project.notes = tbl_notes.notes_id','left');
$this->db->where('tbl_project.client_id', $client_details->client_id);
if($searchQuery != '')
$this->db->where($searchQuery);
$this->db->limit($length,$start);
$cita = $this->db->get()->result();
For some reason the ORDER BY is not set as tbl_notes.notes
Any suggestion on how to fix this?
Thanks in advance
EDIT: i have added more code so there is more visibility of the process
The error occurs, because your column name is not unique, it exists in more than one table.
append the table name of the searched column to your query to make it unique:
for example in this line:
$this->db->order_by('my_table_name.'.$order, $dir);
that would generate something like
ORDER BY `my_table_name.notes` DESC
edit: or in case you have to address columns from several different tables you could change your $valid_columns array:
$valid_columns = array(
0=>'my_table_name1.project_id',
1=>'my_table_name2.client',
2=>'my_table_name2.fullname',
3=>'my_table_name3.notes',
// etc.
);
and maintain the remaining original code.
I'm trying to edit and save some articles, but I receive this error : Creating default object from empty value. What's wrong in my code?Because, at edit I have my subject, but and submit I get this error.
My error : https://imgur.com/a/eWGqc5B
Controller
public function update($type, $id)
{
/* print_r(Input::all()); die; */
if($type == "News")
{
$article = \App\News::find($id);
$article->subject = Request::input('subject');
$article->public = Request::input('public');
$article->category_id = Request::input('category_id');
$article->information = Request::input('information');
$article->update();
}
if($type == "Event")
{
$article = \App\Event::find($id);
$article->subject = Request::input('subject');
$comm->comments = Request::input('comments');
$article->public = Request::input('public');
$article->category_id = Request::input('category_id');
$article->event_type_id = Request::input('event_type_id');
$article->country = Request::input('country');
$article->starts = Request::input('starts');
$article->ends = Request::input('ends');
$article->organizer = Request::input('organizer');
$article->address = Request::input('address');
$article->city = Request::input('city');
$article->website = Request::input('website');
$article->email = Request::input('email');
$article->telephone = Request::input('telephone');
$article->information = Request::input('information');
$article->update();
}
return redirect(URL::previous());
Line 669 in ArticleController.php
$article->subject = Request::input('subject');
Here is my editEvent code : https://codepen.io/anon/pen/YodwKO
my articles.blade code : https://codepen.io/anon/pen/jjXWOb
My route:
`
Route::post('admin/article/update/{type?}/{id?}', [ 'as' => 'update.article', 'uses' => 'ArticleController#update']);
`
The variable $comm is not defined. It looks like a typo.
here is some of the code where I declared the values for $list.
$list['first_name'] = Request::input("first_name");
$list['last_name'] = Request::input("last_name");
when I use dd($list), it shows the value like in my snipped image
but when it enters this if else statement,
if($existing_customer)
{
$customer_id = $existing_customer->customer_id;
}
else
{
dd($list);
$insert_customer["first_name"] = ($list['first_name'] == "undefined" ? "John" : ucfirst($list['first_name']));
$insert_customer["last_name"] = ($list['last_name'] == "undefined" ? "Doe" : ucfirst($list['last_name']));
}
this is the result.
here is the photo of my function, if it could help.
first
second
public function import_submit()
{
$data = Self::get_initial_settings();
/* INITIALIZE AND CAPTURE DATA */
$shop_id = $this->user_info->shop_id;
$sponsor = Tbl_mlm_slot::where("slot_no", Request::input("sponsor"))->where("shop_id", $shop_id)->value("slot_id");
$placement = Tbl_mlm_slot::where("slot_no", Request::input("placement"))->where("shop_id", $shop_id)->value("slot_id");
/* POSITIONING DATA */
$slot_sponsor = $sponsor;
$slot_placement = $placement;
$slot_position = strtolower(Request::input("position"));
/* SLOT GENERATION */
$membership_package_id = Request::input("package_number");
/* JUST ADD TO SLOT IF EXISTING CUSTOMER */
if(Request::input("date_created") == "undefined")
{
$slot_date_created = date("Y-m-d h:i");
}
else
{
$slot_date_created = date("Y-m-d", strtotime(Request::input("date_created")));
}
$existing_customer = Tbl_customer::where("shop_id", $shop_id)->where("email", Request::input("email"))->first();
$list['first_name'] = Request::input("first_name");
$list['last_name'] = Request::input("last_name");
$list['email'] = Request::input("email");
$list['slot_no'] = Request::input("slot_no");
$list['password'] = Request::input("password");
$list['birthday'] = Request::input("birthday");
$list['contact'] = Request::input("contact_number");
$list['gender'] = Request::input("gender");
dd($list); <---- first run of the function, I put one here
if($existing_customer)
{
$customer_id = $existing_customer->customer_id;
}
else
{
dd($list); <--- for the second run of the function, to check what happens to my $list
$insert_customer["shop_id"] = $shop_id;
$insert_customer["first_name"] = ($list['first_name'] == "undefined" ? "John" : ucfirst($list['first_name']));
$insert_customer["last_name"] = ($list['last_name'] == "undefined" ? "Doe" : ucfirst($list['last_name']));
$insert_customer["email"] = ($list['email'] == "undefined" ? "dummy#gmail.com" : $list['email']);
$insert_customer["ismlm"] = 1;
$insert_customer["mlm_username"] = $list['slot_no'];
$insert_customer["password"] = ($list['password'] == "undefined" ? Crypt::encrypt(randomPassword()) : Crypt::encrypt($list['password']));
$insert_customer["created_date"] = $slot_date_created;
$insert_customer["b_day"] = date("Y-m-d", strtotime($list['birthday']));
$insert_customer["birthday"] = date("Y-m-d", strtotime($list['birthday']));
$insert_customer["contact"] = $list['contact'];
$insert_customer["gender"] = strtolower($list['gender']);
$customer_id = Tbl_customer::insertGetId($insert_customer);
/* Insert Customer Address */
$address_purpose[0] = "permanent";
$address_purpose[1] = "billing";
$address_purpose[2] = "shipping";
foreach ($address_purpose as $key => $value)
{
$insert_customer_address["customer_id"] = $customer_id;
$insert_customer_address["country_id"] = 420;
$insert_customer_address["customer_state"] = "";
$insert_customer_address["customer_city"] = "";
$insert_customer_address["customer_zipcode"] = "";
$insert_customer_address["customer_street"] = Request::input("address");
$insert_customer_address["purpose"] = $value;
$insert_customer_address["archived"] = 0;
$insert_customer_address["created_at"] = Carbon::now();
$insert_customer_address["updated_at"] = Carbon::now();
Tbl_customer_address::insert($insert_customer_address);
}
}
}
I can't really see where I went wrong as the values went null just because they entered that if/else statement.
I have a table that I have recently edited to add another index using two columns (which combined are required to be unique).
The problem is now I get an error when I go to create a new model record:
Integrity constraint violation: 1062 Duplicate entry ... for key ....
The two columns are titled:
shipment_origin
pro_number
So as an example, the end result would look like: 1-230185
And I have checked, the record is created technically, it just doesn't follow through and the error is returned. So to make it simple, there is no record before hand (so there's no duplicate entry), a record is created in the database and the error is returned (no matter what).
Or is there a way to go about doing this directly through Laravel rather than MySQL?
Update
Here is the public function in my controller:
public function store(Request $request)
{
$this->validate(request(), [
'pro_number' => 'required',
'shipment_origin' => 'required'
/*'piecesNumber' => 'required' (had to remove for now, but must review)*/
]);
$user_id = Auth::id();
$input = $request->all();
//Save Initial Shipment Data
$shipment = new Shipment();
$shipment->pro_number = request('pro_number');
$shipment->shipment_origin = request('shipment_origin');
$shipment->date = request('date');
$shipment->due_date = request('due_date');
$shipment->tractor_id = request('tractor_id');
$shipment->trailer_id = request('trailer_id');
$shipment->driver_id = request('driver_id');
$shipment->notes = request('notes');
$shipment->shipper_no = request('shipper_no');
$shipment->ship_to = request('ship_to');
$shipment->ship_from = request('ship_from');
$shipment->bill_to = request('bill_to');
$shipment->bill_type = request('bill_type');
$shipment->load_date = request('load_date');
$shipment->shipment_status = 1;
$shipment->shipment_billing_status = (isset($request->shipment_billing_status) && !empty($request->shipment_billing_status)) ? $request->shipment_billing_status : 1;
$shipment->created_by = $user_id;
$shipment->cn_billtoName = request('cn_billtoName');
$shipment->cn_billtoAddress1 = request('cn_billtoAddress1');
$shipment->cn_billtoAddress2 = request('cn_billtoAddress2');
$shipment->cn_billtoCity = request('cn_billtoCity');
$shipment->cn_billtoState = request('cn_billtoState');
$shipment->cn_billtoZip = request('cn_billtoZip');
$shipment->cn_billtoEmail = request('cn_billtoEmail');
$shipment->cn_billtoPhone = request('cn_billtoPhone');
$shipment->cn_shiptoName = request('cn_shiptoName');
$shipment->cn_shiptoAddress1 = request('cn_shiptoAddress1');
$shipment->cn_shiptoAddress2 = request('cn_shiptoAddress2');
$shipment->cn_shiptoCity = request('cn_shiptoCity');
$shipment->cn_shiptoState = request('cn_shiptoState');
$shipment->cn_shiptoZip = request('cn_shiptoZip');
$shipment->cn_shiptoEmail = request('cn_shiptoEmail');
$shipment->cn_shiptoPhone = request('cn_shiptoPhone');
$shipment->cn_shipfromName = request('cn_shipfromName');
$shipment->cn_shipfromAddress1 = request('cn_shipfromAddress1');
$shipment->cn_shipfromAddress2 = request('cn_shipfromAddress2');
$shipment->cn_shipfromCity = request('cn_shipfromCity');
$shipment->cn_shipfromState = request('cn_shipfromState');
$shipment->cn_shipfromZip = request('cn_shipfromZip');
$shipment->cn_shipfromEmail = request('cn_shipfromEmail');
$shipment->cn_shipfromPhone = request('cn_shipfromPhone');
$shipment->fuelChargeDesc = request('fuelChargeDesc');
$shipment->fuelChargeAmt = request('fuelChargeAmt');
$shipment->fuelChargeTotal = request('fuelChargeTotal');
$shipment->permitChargeDesc = request('permitChargeDesc');
$shipment->permitChargeAmt = request('permitChargeAmt');
$shipment->permitChargeTotal = request('permitChargeTotal');
$shipment->otherChargeDesc = request('otherChargeDesc');
$shipment->otherChargeAmt = request('otherChargeAmt');
$shipment->otherChargeTotal = request('otherChargeTotal');
$shipment->noCharge = request('noCharge');
$shipment->noSettle = request('noSettle');
$shipment->Total = request('Total');
if ((request('shipment_billing_status') == 2) || (request('shipment_billing_status') == 3)){
$balance = 0.00;
}else{
$balance = request('Total');
}
$shipment->Balance = $balance;
$shipment->freightBillSubtotal = request('freightBillSubtotal');
$shipment->save();
//Save Shipment Details
for ($i = 0; $i < count($request->shipment_details['piecesNumber']); $i++) {
//the first line used to be 'shipment_id' => $shipment->pro_number,
Shipment_Detail::create([
'shipmentID' => $shipment->id,
'pieces_number' => $request->shipment_details['piecesNumber'][$i],
'pieces_type' => $request->shipment_details['piecesType'][$i],
'rate_type' => $request->shipment_details['rateType'][$i],
'charge' => $request->shipment_details['charge'][$i],
'weight' => $request->shipment_details['weight'][$i],
'hazmat' => $request->shipment_details['hazmat'][$i],
'description' => $request->shipment_details['description'][$i] ]);
}
$carrier = \App\Customer::where('carrier','=',1)->get();
foreach($carrier as $car){
$carrierUsers = $car->users;
if ($carrierUsers->count() > 0){
foreach($carrierUsers as $carrierUser){
$carrierUser->notify(new FreightBillNew($shipment));
}
}
}
Session::flash('success_message','Freight Bill Successfully Created'); //<--FLASH MESSAGE
//Return to Register//
return redirect('/shipments/accounts');
}
I am trying to dynamic populate the report based the values select in dropdown.
For the same, I need to retrieve one or more columns from table using laravel 5.2. I am getting error "Trying to get property of non-object"
class ReportController extends Controller
{
public function index()
{
$apartmentNumber = Report:: lists('apartment_number','id')->all();
$commonArea = Report:: lists('common_area_name','id')->all();
$createdDate = Report:: lists('created_date_time','id')->all();
$status = Report:: lists('status','id')->all();
$priority = Report:: lists('priority','id')->all();
$assign = Report:: lists('assign_to','id')->all();
$reportDatas=Report::all();
return view('Report.index', compact('apartmentNumber','commonArea','createdDate','status','priority','assign','reportDatas','report'));
}
public function storeData(Request $request)
{
$report = new Report();
$report1 = $request->input();
$report->id = trim($report1['apartment_number']);
$aptNumber = Report::find($report->id)->apartment_number;
$report->id = trim($report1['common_area_name']);
$commonAreaName = Report::find($report->id)->common_area_name;
$report->id = trim($report1['created_date_time']);
$createdDateTime = Report::find($report->id)->created_date_time;
$report->id = trim($report1['status']);
$status1 = Report::find($report->id)->status;
$report->id = trim($report1['priority']);
$priority1 = Report::find($report->id)->priority;
$report->id = trim($report1['assign']);
$assign1 = Report::find($report->id)->assign_to;
$apartmentNumber = Report:: lists('apartment_number','id')->all();
$commonArea = Report:: lists('common_area_name','id')->all();
$createdDate = Report:: lists('created_date_time','id')->all();
$status = Report:: lists('status','id')->all();
$priority = Report:: lists('priority','id')->all();
$assign = Report:: lists('assign_to','id')->all();
$reportDatas=Report::where('apartment_number', '=',$aptNumber)
->where('common_area_name', 'like' , $commonAreaName)
->where('created_date_time', '=' , $createdDateTime)
->where('status', 'like' , $status1)
->where('priority', 'like' , $priority1)
->where('assign_to', 'like' , $assign1)
->get();
return view('Report.index', compact('apartmentNumber','commonArea','createdDate','status','priority','assign','reportDatas','report'));
}
}