Home > Mobile >  How to loop database model in laravel
How to loop database model in laravel

Time:10-15

I specified the connection and the table name in my Model

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class object_mapping extends Model
{
    use HasFactory;
    public $table = 'object_mapping';
    public $connection = 'mysql1';
}

Added the model to my controller file use App\Models\object_mapping;

And now I need to loop through it extracting userFK != 0

What should my loop look like?

CodePudding user response:

According to the comment you made:

use Illuminate\Support\Facades\DB;

$objectMappings = DB::table('object_mapping')->get();

foreach ($objectMappings as $objectMapping)
{
    dump($objectMapping->id);
}

Also I would suggest naming your model according the the convention. In your case your Model Class name would be : ObjectMapping

  • Related