WmZilla - Webmaster and Marketplace

The Next Generation Webmaster and Trade Forum

3 Ways to Create Crosses in Laravel

ttvyaboydes

New member

0

0%

Status

Offline

Posts

43

Likes

0

Rep

0

Bits

225

3

Months of Service

0%
In this opportunity we will quickly see how to make joins with Laravel, the logic is similar in most PHP frameworks except for CodeIgniter.

Example 1:

In this example, we perform a join between 3 tables, where it retrieves everything adding the email condition. We use the DB class for this.

```php
$users = DB::table('users as u')
->leftJoin('users_products as up', 'u.id', '=', 'up.user_id')
->leftJoin('products as p', 'p.id', '=', 'up.product_id')
->select('*')
->where('u.email', $email)
->get();
```

Example 2:

In this example, we apply the select using the model.

```php
$media = GalleryModel::select('attachment.type as mime', 'attachment.media_type', 'galleries.price', 'galleries.ownerId as modelId', 'galleries.id as galleryId', 'attachment.mediaMeta', 'attachment.path')
->join('attachment', 'attachment.parent_id', '=', 'galleries.id')
->where('attachment.media_type', 'image')
->where('galleries.type', 'image')
->where('attachment.id', $id)
->first();
```

Example 3 (In the Model):

In this example, we have many quotes. If we wanted to summarize the examples of the 2 previous queries, in our controller, we can access the relationship method.

```php
public function quotes(){
return $this->hasMany(Quote::class);
}
```
 

249

6,622

6,642

Top