Great bit of work with LaravelShoppingcart! I'm interested in implementing the Buyable interface on my product Model. Can you please provide some pointers on how to do this?
Thanks
class Product implements Buyable
See above
Err. Thanks, but anyone asking this question is unlikely to know how to deal with half an answer.
I've added class Product implements Buyable to my model, and now I get an error saying:
Class App\Product contains 3 abstract methods and must therefore be declared abstract or implement the remaining methods (Gloudemans\Shoppingcart\Contracts\Buyable::getBuyableIdentifier, Gloudemans\Shoppingcart\Contracts\Buyable::getBuyableDescription, Gloudemans\Shoppingcart\Contracts\Buyable::getBuyablePrice)
I suspect it's something simple, but I'm pretty new to Laravel and haven't come across anything like this before.
EDIT: It's okay. I figured it out. Anyone else wanting to know how it's done, just add the following to your product model (using whatever fields are appropriate for your model):
public function getBuyableIdentifier(){
return $this->id;
}
public function getBuyableDescription(){
return $this->name;
}
public function getBuyablePrice(){
return $this->price;
}
@matbeard Sorry if it came across as a harsh answer. Just thought that would be enough information. In the end, if you're using Laravel and doing OO programming, it seems to me it's pretty trivial how to "implement an interface".
But again, didn't mean to sound harsh, and glad you figured it out! Good luck with your website!
To add on to what @matbeard mentioned, It now throws an error saying:
Declaration of App\Book::getBuyableDescription() must be compatible with Gloudemans\Shoppingcart\Contracts\Buyable::getBuyableDescription($options = NULL)
to fix it, add $options = null inside the method parenthesis:
public function getBuyableIdentifier($options = null){
return $this->id;
}
Also the full declaration in your Model is :
use Gloudemans\Shoppingcart\Contracts\Buyable;
class Product extends Model implements Buyable
{ ...
use Illuminate\Support\Facades\Redirect;
class CartController extends Controller
{
public function add_to_cart_function(Request $request){
$qty=$request->qty;
$product_id=$request->product_id;
// print_r($product_id);
// print_r($qty);
// exit();
$product_detail_info=DB::table('tbl_product')
->where('id',$product_id)
->first();
// echo '<pre>';
// print_r($product_info);
// exit();
Cart::add(['id' => $product_detail_info->product_id, 'name' => $product_detail_info->product_name, 'qty' => $qty, 'price' => $product_detail_info->product_price, 'options' => ['image' => $product_detail_info->image_name]]);
}
}
//////--------"Trying to get property of non-object"
i am using laravel version 5.5
Most helpful comment
To add on to what @matbeard mentioned, It now throws an error saying:
Declaration of App\Book::getBuyableDescription() must be compatible with Gloudemans\Shoppingcart\Contracts\Buyable::getBuyableDescription($options = NULL)to fix it, add
$options = nullinside the method parenthesis:public function getBuyableIdentifier($options = null){ return $this->id; }