Patwan's avatar

Pulling more than one value in Query Builder

I am building an e-commerce project using Laravel 5.4, I have got 2 tables which have the following columns:

categories: id, name , timestamps

Products: id , name, price, description, size , image , category_id , timestamps

The two tables are interlinked via one to many relationship(One Category has many products). I am uploading images and storing inside public/images folder in my Laravel app while the image name is stored in the database. When I upload everything is working fine and am pulling images of a particular category and displaying in a view called Front.blade.php which is controlled by frontcontroller, But I want to pull the images together with their corresponding price in the products table and display to view..Please assist?

Category model

class Category extends Model
{
    protected $fillable = ['name'];

    public function products(){
        return $this->hasMany('App\Product');   
    }

}

Product Model


class Product extends Model
{
    protected $fillable = ['name', 'desctiption' , 'size', 'category_id', 'image'];

    public function category()
    {
        return $this->belongsTo(Category::class);
    }
}

Products Controller

public function store(Request $request)
    {

        $formInput[]=$request->image;

       //validation
        $this->validate($request,[
            'name'=>'required',
            'size'=>'required',
            'description' => 'required|min:12',
            'price'=>'required',
            'category_id' => 'required',
            'image'=>'image|mimes:png,jpg,jpeg,gif|max:100'
        ]);

        $image=$request->image;
        if($image){
            $imageName=$image->getClientOriginalName();
            $image->move('images',$imageName);
            $formInput['image']=$imageName;
        }

        //Instantiate a new Project called Product
        $product = new Product;

        //Add the products to the Object
        $product->description = $request->description;
        $product->name = $request->name;
        $product->price = $request->price;.
        $product->image = $imageName;
        $product->category_id = $request->category_id;
        $product->size = $request->size;

        //Save all the items to the database
        $product->save();

FrontController

 public function index(){
        $items = Category::find(6)->products()->whereNotNull("image")->where("price", true)->get();

        return view('front.index')->withItems($items);
    }

Front.blade.php

@foreach($items as $item)
            <img src="{{ asset('images/'.$item->image) }}">
@endforeach

0 likes
3 replies
neilherbertuk's avatar

Hi Patwan,

I'm not sure if I'm understanding what you want to achieve. The price field should be available in blade under {{ $item->price }}? Can you explain in more detail what you've tried and what isn't working rather than what you want to do?

Neil

1 like
josephmtinangi's avatar
Level 1

Writing only

$items = Category::find(6)->products;

should give you products in that category

If you want to load products which has images you should write

$items = Category::find(6)->products()->whereNotNull("image")->get();

And if you want to load products which has price then write

$items = Category::find(6)->products()->whereNotNull("image")->whereNotNull("price")->get();

I think that the price column is integer or double, therefore you cannot say where("price", true)

@neilherbertuk is right, the price field is available in blade under {{ $item->price }}.

Your query

$items = Category::find(6)->products()->whereNotNull("image")->where("price", true)->get();

returns an empty collection

Try writing

$items = Category::find(6)->products()->whereNotNull("image")->whereNotNull("price")->get();
2 likes
Patwan's avatar

@joseph,,, your code has been helpful,,

$items = Category::find(6)->products()->whereNotNull("image")->whereNotNull("price")->get();

I wanted to pull both prices and image name and store in one variable before displaying to view,,, Thanks alot

Please or to participate in this conversation.