Home > Blockchain >  I don’t know how to get data in laravel correctly by linking the data in this database
I don’t know how to get data in laravel correctly by linking the data in this database

Time:12-26

In this figure, the remaining tables are linked to the datainfo table. I need to retrieve the entire table data from the datainfo table. In this picture I have shown the tables themselves

CodePudding user response:

You can create Eloquent Models for each table and define Relationships among them. This is the Correct Laravel Way.

Let's assume Your datainfo table represents your Datainfo model, Your cars table represents Car model. Same as washtypes and boxes.

then depending on your relation type defined the relation in Datainfo model.

class Datainfo extends Model
{

    public function cars()
    {
        return $this->hasMany(Car::class);
    }
}

You also can use hasOne instead of hasMany for one - to one relation

Similarly, create relation defining functions as washtypes() and boxes().

Then To get Datainfo with all related data using something like think in your controller

return Datainfo::with('cars','washtypes','boxes')->get();

Alternatively, you can get the count

return Datainfo::with('cars','washtypes','boxes')->count();

To get a count on a date

return Datainfo::with('cars','washtypes','boxes')->where('created_at',$date_var)->count();

If you only want Datainfo that has relation with cars, washtypes or boxes:

return Datainfo::has('cars','washtypes','boxes')->where('created_at',$date_var)->count();

CodePudding user response:

Use laravel eloquent join clauses, https://laravel.com/docs/8.x/queries#joins For total amount in same day You can use eloquent sum and group by methods based on the date

  • Related