Call to undefined method App\Product::getProductStock() - laravel

BadMethodCallException
Call to undefined method App\Product::getProductStock()
This error occurred while accessing the model function in controller
My Controller
$user_id = Auth::user()->id;
$user_email = Auth::user()->email;
// To prevent out of stock material from ordering
$userCart=DB::table('cart')->where('user_email',$user_email)->get();
// echo "<pre>"; print_r($userCart); die;
foreach ($userCart as $cart) {
$product_stock= Product:: getProductStock($cart->product_id,$cart->size);
echo $product_stock;
if ($product_stock==0) {
return redirect('/cart')->with('flash_message_error','Product is Stock sold out. Buy another product');
}
if ($Cart->quantity>$product_stock) {
return redirect('/cart')->with('flash_message_error','Reduced product stock & Try Again');
}
}
Model
public static function getProductStock($product_id,$product_size)
{
$getProductStock=ProductsAttribute::select('stock')->where(['product_id'=>$product_id,'size'=>$product_size])->first();
return $getProductStock->stock;
}

I think you product retrieving approach is not the best.
To get the product you should use this approach:
$product = Product::find($cart->product_id);
And to retrieve the stock should be:
$product_stock = $product->getProductStock($product->id, $cart->size);

Related

ERROR - Trying to get property of non-object - in laravel

I am getting the problem when i fetch categorey name from categorey table ..
code here ..
Thanks.
public function viewProducts(){
$products = Product::get();
$products = json_decode(json_encode($products));
foreach($products as $key => $val){
$category_name = Category::where(['id'=>$val->category_id])->first();
$products[$key]->category_name= $category_name->name;
}
echo "<pre>";print_r($products);die;
return view('admin.products.view_products')->with(compact('products'));
}
ErrorException (E_NOTICE)
Trying to get property of non-objectenter image description here
The error is straight forward. You haven't gotten any objects of $category_name variable and you're trying to access to the value of the name property. Which is not defined because there's no object.
Add a simple condition to fix it like this
public function viewProducts(){
$products = Product::get();
$products = json_decode(json_encode($products));
foreach($products as $key => $val){
$category_name = Category::where(['id'=>$val->category_id])->first();
if($category_name != null){
$products[$key]->category_name= $category_name->name;
}
}
echo "<pre>";print_r($products);die;
return view('admin.products.view_products')->with(compact('products'));
}
To send data to your blade:
# Controller:
$data['category_name'] = $category_name;
$data['products'] = $products;
->withKeyName($data);
# Blade:
{{ $KeyName['category_name'] }}
{{ $KeyName['products'] }}

Call to a member function setPath() on a non-object in laravel

In my Laravel web application, getting error like "Call to a member function setPath() on a non-object" while using pagination.
My code is
In model,
public function scopegetApartments()
{
$list1 = DB::table('properties')->orderBy('id','DESC')->paginate(1);
$list = array();
foreach($list1 as $listeach){
$imgs = DB::table('property_images')->where('property_id',$listeach->id)->get();;
$images = array();
foreach($imgs as $img){
array_push($images, $img->image);
}
$lists = array(
'id'=>$listeach->id,
'name'=>$listeach->name,
'rent'=>$listeach->rent,
'shortdescription'=>$listeach->shortdescription,
'images'=>$images,
);
array_push($list, $lists);
}
if(count($list)>0){
return $list;
}else{
return '[]';
}
}
In view,
$apartmentlist->setPath('serviceapartments');
echo $apartmentlist->render();
How to resolve this problem?

How to send value as parameter to model in codeigniter to get data?

I am new in codeigniter.
Currently working in e-commerce site I want to send the category_id as parameter to bring product data.
I want to know the steps to detail and Thanks
This is my model:
( function get_category(){ $query = $this->db->query("SELECT a.category_id,a.category_name from ci_intro.categories a order by a.category_id"); return $query->result(); } function get_product($id){ $q = $this->db->query('select * from products where category_id = $id'); if($q->num_rows() > 0){ foreach ($q->result() as $row) { $data[] = $row; } return $data; } } )
and this is my controller
( public function services() { $this->load->model("get_db"); $data['results'] = $this->get_db->get_category(); $id=$this->input->post('id'); $data['cat_id'] = $this->get_db->get_product($id); $this->load->view("view_header"); $this->load->view("view_nav"); $this->load->view("content_portfolio",$data); $this->load->view("site_footer"); } )
You get category_id in model as $_GET['category_id'] or $_REQUEST['category_id'] in model also .
class Pages extends CI_Controller {
public function view($page = 'home')
{
$this->load->model('services_model');
$id=$this->input->post('id');
$data['records']= $this->services_model->getData($id);
$data['title'] = ucfirst($page); // Capitalize the first letter
$this->load->view('templates/header', $data);
$this->load->view('pages/'.$page, $data);
$this->load->view('templates/footer', $data);
}
}
this is your controller example
and this is mlodel for you
class Services_model extends CI_Model {
function getUser($id) {
$q = $this->db->query('YOUR QUERY HERE');
if($q->num_rows() > 0){
foreach ($q->result() as $row)
{
$data[] = $row;
}
return $data;
}
}
}
you can see various crud example and their website for further details
Your category should be the drop downlist. on change event the id will be pass through ajax like below
$(document).ready(function() {
$("#category").change(function(){
/*dropdown post */
$.ajax({
url:"<?php echo base_url(); ?>index.php/services/getproducts",
data: {catid: $(this).val()},
type: "POST",
success: function(data){
$("#product").html(data);
}
});
});
}
get prducts will be the function in services controller class you can get the category id using the
$this->input->post('catid');

Debug Codeigniter Shoppincart Functions:

Can I have some help with this, please. This is a Codeigniter script MVC. There is a Controller "Function add to cart" with a Model: "Function get_products" in Models. I cannot see what is wrong here and why the function get_products doesn't execute. Could someone help me please.
This is the Model: get_product which connects to database:
function get_product()
{
$product_id = $this->input->post(‘product_id’);
$query = $this->db->select(‘product_id, product_name, description, price, photopath’);
$query = $this->db->from(‘product’);
$query = $this->db->where(‘product_id’, $product_id);
$query = $this->db->get(’‘);
return $query->result_array();
}
This is the Controller called Function add_cart, which add products to the "shopping cart view":
public function add_cart()
{
$thisProduct = $this->Cart_model->add_product();
if($thisProduct->num_rows() > 0)
{
$data = array(‘id’ => $thisProduct[‘product_id’],
‘qty’ => 1,
‘price’ => $thisProduct[‘price’],
‘name’ => $thisProduct[‘product_name’],
‘description’ => $thisProduct[‘description’]
);
$this->cart->insert($data);
}
$this->load->view(“site_header”);
$this->load->view(“site_nav”);
$this->load->view(“shoppingcart”, $data);
$this->load->view(“site_footer”);
}
you make many mistakes.
read here: http://codeigniter.com/forums/viewthread/128969/

codeigniter view, add, update and delete

I'm newbie in codeigniter and still learning. Anyone can help for sample in basic view, add, update, delete operation and queries in codeigniter will gladly appreciated.
Just a simple one like creating addressbook for newbie.
thanks,
best regards
Some sample queries in Codeigniter
class Names extends Model {
function addRecord($yourname) {
$this->db->set("name", $yourname);
$this->db->insert("names");
return $this->db->_error_number(); // return the error occurred in last query
}
function updateRecord($yourname) {
$this->db->set("name", $yourname);
$this->db->update("names");
}
function deleteRecord($yourname) {
$this->db->where("name", $yourname);
$this->db->delete("names");
}
function selectRecord($yourname) {
$this->db->select("name, name_id");
$this->db->from("names");
$this->db->where("name", $yourname);
$query = $this->db->get();
return $this->db->result();
}
function selectAll() {
$this->db->select("name");
$this->db->from("names");
return $this->db->get();
}
}
More information and more ways for CRUD in codeigniter active record documentation
More about error number over here
A sample controller
class names_controller extends Controller {
function addPerson() {
$this->load->Model("Names");
$name = $this->input->post("name"); // get the data from a form submit
$name = $this->xss->clean();
$error = $this->Names->addRecord($name);
if(!$error) {
$results = $this->Names->selectAll();
$data['names'] = $results->result();
$this->load->view("show_names", $data);
} else {
$this->load->view("error");
}
}
}
More about controllers over here
A sample view - show_names.php
<table>
<tr>
<td>Name</td>
</tr>
<?php foreach($names as $row): ?>
<tr><td><?ph echo $row->name; ?></td></tr>
<?php endforeach; ?>
</table>
More about codeigniter views over here
You can use this as an example
class Crud extends Model {
// selecting records by specifying the column field
function select()
{
// use $this->db->select('*') if you want to select all the records
$this->db->select('title, content, date');
// use $this->db->where('id', 1) if you want to specify what row to be fetched
$q = $this->db->get('mytable');
// to get the result
$data = array();
// for me its better to check if there are records that are fetched
if($q->num_rows() > 0) {
// by doing this it means you are returning array of records
foreach($q->result_array() as $row) {
$data[] = $row;
}
// if your expecting only one record will be fetched from the table
// use $row = $q->row();
// then return $row;
}
return $data;
}
// to add record
function add()
{
$data = array(
'title' => 'My title' ,
'name' => 'My Name' ,
'date' => 'My date'
);
$this->db->insert('mytable', $data);
}
// to update record
function update()
{
$data = array(
'title' => $title,
'name' => $name,
'date' => $date
);
$this->db->where('id', 1);
$this->db->update('mytable', $data);
}
// to delete a record
function delete()
{
$this->db->where('id', 1);
$this->db->delete('mytable');
}
}
Some of this are from codeigniter userguide.
To view the records,
If return data is array of records,
foreach($data as $row)
{
echo $row['title'] . "<br />";
}
If the return data is an object (by using $q->row),
echo $data->title;
This is just a few examples or CRUD in Codeigniter. Visit the codeigniter userguide.

Resources