Задача у меня вывести все контакты, отсортированные в алфавитном порядке по фирмам, не понимаю как это сделать.
public function firm()
{
return $this->belongsTo('App\firm')->orderBy('field', 'asc');
}
public function contacts()
{
return $this->hasMany('App\App\Contact')->orderBy('field', 'asc');
}
$firms = Firm::orderBy('field', 'asc')->get();
foreach ($firms as $firm) {
$contact = $firm->contacts()->orderBy('field', 'asc')->get();
}
$firms = Firm::with('contacts' => function ($q)
{
return $q->orderBy('field', 'asc');
})->get();
id | name
id | name
guide_id | category_id
public function categories()
{
return $this->belongsToMany('App\Models\Category');
}
public function guides()
{
return $this->belongsToMany('App\Models\Guide');
}
$ids = [1, 2, 3];
$guides = App\Models\Guide::whereHas('categories', function ($q) use ($ids)
{
return $q->whereIn('id', $ids);
})->get();
$category = Category::find(1);
$count = $category->guides()->count();
// Модель Cloth
public function styles()
{
return $this->belongsToMany(...);
}
$result = Cloth::whereHas('styles', function ($q)
{
return $q->whereIn('id', [1, 2]);
})->get();
<?php
use Illuminate\Foundation\Testing\WithoutMiddleware;
use Illuminate\Foundation\Testing\DatabaseMigrations;
use Illuminate\Foundation\Testing\DatabaseTransactions;
class HomeControllerTest extends TestCase
{
public function testIndexPage()
{
\View::shouldReceive('make')->with('home')->andReturn('test');
View::shouldReceive('share');
$response = $this->action('GET', 'HomeController@getIndex');
$this->assertResponseOk();
$this->assertEquals('test', $response->getContent());
}
}