Laravel Eloquent get data where comparison is true

Related searches

I am creating a mini inventory stock for a fashion store. I am using Laravel/Voyager with BREAD and everything fine. I have 2 tables Sizes and Products with a column in common product_code.

I would like to get the results from Sizes where column product_code = Product 'product_code'. I have this query in the controller:

$product_code = Product::all();
$allSizes = Size::where('product_code', ($product_code->product_code));

and in browse.blade.php I have:

@foreach ($allSizes as $size)
    <tr>
        <td align="right">{{$size->size_name}}</td>
        <td align="right">{{$size->stock}}</td>
    </tr>
@endforeach 

I guess the where statement is not working as supposed to. I want to get the corresponding stock based on product_code for each size from table Sizes

Try This

$product_code = Product::pluck('product_code')->toArray();
$allSizes = Size::whereIn('product_code', $product_code)->get();

and in browse.blade.php:

@foreach ($allSizes as $size)
    <tr>
        <td align="right">{{$size->size_name}}</td>
        <td align="right">{{$size->stock}}</td>
    </tr>
@endforeach 

Eloquent: Getting Started - Laravel, Hi, I'm trying to check if a boolean is true or false, I get true when it is set to true http://laravel.com/docs/5.1/eloquent-mutators#attribute-casting. @fsdolphin The database usually stores the boolean value as an integer, e.g. TINYINT(1) . It should only make a difference when you use === comparisons. For more examples, see first message of #3946 and this Laravel Daily article. Update: Though the above method is convenient, as noted by Arth it is inefficient on large datasets, because the DATE() SQL function has to be applied on each record, thus discarding the possible index. Here are some ways to make the comparison (but please read notes

I think you're going about this the wrong way. Your tables are related in some way and you need to define a relationship to access the data Eloquently.

If I were building such a database, I think that the relationship between Product and Size is a many to many. I.e. a Product can have many Sizes, and you can also shop for many Products in a certain Size. Thus, your models should have a belongsToMany() relationship to each other.

// Product.php
protected $with = ['sizes'];     // eager load product sizes
public function sizes() {
    return $this->belongsToMany(Size::class);
}

// Size.php
public function products() {
    return $this->belongsToMany(Product::class);
}

Then you can do

// ProductsController.php
public function show(Product $product) {
    return $product;
}

Database: Query Builder - Laravel, Finding & comparing array values via Eloquent - all help appreciated! instructors which should be returned as selectable are those with the correct qualification. When I show my 'Health and Safety' course (ID 3), it should return both Bob and Mary Proudly hosted with Laravel Forge and DigitalOcean. For string data, value corresponds to the number of characters. For numeric data, value corresponds to a given integer value (the attribute must also have the numeric or integer rule). For an array, size corresponds to the count of the array. For files, size corresponds to the file size in kilobytes. Let's look at some examples:

You missed to run the query

$product_code = Product::all();
# ::all() will return collection, and you can't access a property of it directly

$allSizes = Size::where('product_code', ($product_code->first()->product_code))->get();

Notice the ->get();

What is the proper way to check a boolean from a DB, When using the comparison operator ( == ), object variables are compared in a Two instances of the same class o1 == o2 : TRUE o1 != o2 : FALSE o1 === o2 return true; //Code-flow not intended to arrive here. PHP and MySQL both provide ways to sort your data already, and it is a good idea to use that if possible . Sometimes you may wish to only include certain meta data with a resource response if the resource is the outermost resource being returned. Typically, this includes meta information about the response as a whole. To define this meta data, add a with method to your resource class. This method should return an array of meta data to be included

Finding & comparing array values via Eloquent - all help , Browse other questions tagged php laravel eloquent laravel-query-builder or ask your own question. The Overflow Blog The Overflow #36: Community-a-thon

Comparing Objects - Manual, I'm trying to fetch relational data from a model where the date column is higher or equal to the current time. The date column is formated as this: Y-m-d H:i:s What I'm trying to do is to grab all rows where the Y-m-d H:i:s is in the future.

{note} Methods that mutate the collection (such as shift, pop, prepend etc.) are not available on the LazyCollection class.. Lazy Collection Methods. In addition to the methods defined in the Enumerable contract, the LazyCollection class contains the following methods:

Comments
  • Or you can ask for join also
  • is throwing bac error Trying to get property 'product_code' of non-object
  • Check It now @fallcoshkoder
  • Ye noticed that. but now I get an error Property [product_code] does not exist on this collection instance.
  • check my updated answer, youre returning Product::all(); which will return a collection, so you can't do this $product_code->product_code, betcause $product_code is a collection, you need to pick a single value
  • Understood, but as you say "can't do this $product_code->product_code, betcause $product_code is a collection" I have the product_code as a variable. the SQL statement I am trying to get is: SELECT * FROM sizes WHERE product_code=$product_code; then elaborate the data to get the sizes of each product and stock of each size.