简体   繁体   中英

how to retrieve 2 specific rows from db in laravel

I'm new to laravel. i have already searched the laravel doc site for this question. in one of my laravel view page i need to display the value of two rows

1) mtitle
2) mdescription

The table contains some more rows. the columns are name,value,key,status . i wrote the query builder in my blade.php using the model file like this.

$default = Specific::where('status',1)->where('name','mtitle')->first();

This obviously gets only one row. i want to know is there any way to get the other row 'mdescription' also without writing another one more query?

You need to change your query like this

$default = Specific::where('status',1)->where('name','mtitle')->get();

first() method will return only single first record with matching where conditions.

use 'get()' method to get all records.

you can also use 'paginate()' for pagination if you have lots of records.

You can orWhere()

$default = Specific::where('status',1)->where('name','mtitle')->orWhere('name', 'mdescription')->get();

Assuming you want to get rows that have name column set as mtitle or description , you could use whereIn method this way:

$default = Specific::where('status',1)
           ->whereIn('name',['mtitle','description'])->get();

You can now display value of those rows using loop:

foreach ($default as $item) {
    echo $item->name.' '.$item->value.' '.$item->key.' '.$item->status;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM