User::with(['withdrawals' => function($query) {
$query->select('id', 'name')->orderBy('created_at')->with(['withdrawalitems' => function($query) {
$query->select('id', 'name')->orderByDesc('created_at')
}])
}])->get();
// вместо этого можешь замутить model binding
public function order_index(Request $request){
$order = Order::find($request->order_id);
if(isset($order)) abort(404);
// и писать так
public function order_index(Order $order, Request $request){
Project::whereHas(“log”, function($q){
$q->where(“type”, “message”)->orderBy('id', 'desc')->limit(1);
})->get();
public function index()
{
$users = DB::table('users')->paginate(15);
return view('user.index', ['users' => $users]);
}
{{ $users->links() }}
public function boot()
{
parent::boot();
Route::model('user', App\User::class);
Route::model('post', App\Post::class);
}
Route::get('profile/{user}', 'UserController@show')
Route::get('posts/{post}', 'PostController@show')
public function getRouteKeyName()
{
return 'login';
}
public function posts_feed()
{
return $this->belongsToMany('App\Post');
}
public function posts()
{
return $this->hasMany('App\Post', 'author_id');
}
public function getRouteKeyName()
{
return 'slug';
}
public function author()
{
return $this->belongsTo('App\User');
}
class ProfileController extends Controller
{
public function show(User $user)
{
$user->load('posts_feed.author')
return view('profile', compact('user'))
}
}
{{ $user->name }}
@foreach(user->posts_feed as $post)
{{ $post->title }}
{{ $post->author->name }}
@endforeach
// контроллер
$consultations = Consultation::with('patient.passports')->get()
// blade
@foreach($consultations as $consultation)
{{ $consultation->created_at }}
{{ $consultation->patient->created_at}}
@foreach($consultation->patient->passports as $client_passport)
{{ $client_passport->surname }}
@endforeach
@endforeach
// модель
class Patient extends Model
{
public function consultations()
{
return $this->hasMany(Consultation::class());
}
}
// контроллер
public function index(Request $request)
{
if ($request->surname != null || $request->first_name != null || $request->second_name != null || $request->birthday != null || $request->day_consultation != null) {
$patients = Patient::with('consultations')->orWhere('surname', $request->surname)
->orWhere('first_name', $request->first_name)
->orWhere('second_name', $request->second_name)
->orWhere('birthday', $request->birthday)
->orderBy('surname')
->orderBy('first_name')
->orderBy('second_name')
->orderBy('birthday')
->get();
return view('administration.protocol.index', compact('patients'));
} else {
$consultations = Consultation::orderBy('day_consultation')
->get();
return view('administration.protocol.index', compact('consultations'));
}
}
// вывод в blade.php
<ul>
@forelse($patients as $patient)
<li>Пациенту {{ $patient->name }}
<ul>
@forelse($patient->consultations as $consultation)
<li>назначено на {{ $consultation->created_at }}</li>
@empty
<li>Нет назначенных консультаций</li>
@endforelse
</ul>
</li>
@empty
<li>Нет записей</li>
@endforelse
</ul>
public function getRouteKeyName()
{
return 'slug';
}