extends('layouts.main', ['title' => 'Тайтл'])
@section('content')
//контент
@endsection<title>
@isset($title)
{{ $title }} |
@endisset
{{ config('app.name') }}
</title> public function profile()
{
return $this->hasOne('App\Models\Profile','user_id', 'id');
}if (!Auth::user()->profile) {
return redirect()->route('profile');
}
return $next($request);Route::group('middleware' => ['verified','hasProfile'], function () {
}); class Filter
{
protected $builder;
protected $request;
public function __construct($builder, $request)
{
$this->builder=$builder;
$this->request=$request;
}
public function run()
{
foreach ($this->request->all() as $filter => $value)
{
if (method_exists($this, $filter) and !empty($value))
{
$this->$filter($value);
}
}
return $this->builder;
}
//------------------Фильтры-----------------------------------
private function name($value)
{
$this->builder->where('name', 'like', "%$value%");
}
private function type_id($value)
{
$this->builder->where('type_id', $value);
}
}->orderBy($field, $metod);return ProductResource::collection($products); $user->update($request->all())$user->update('[country_id'=>$country_id]);return [
// Отношения
'country' => new CountryResource($this->country)
];User::with('country')->get();public $lists;
public function __construct()
{
$this->lists = List::all();
}$this->listspublic function getLists()
{
return List::all();
}$lists=$this->getLists();public function catalog_page($id)
{
$menus = Menu::all();
$category = Category::with('children')->find($id);
return view('catalog_page', compact('menus','category'));
}<h1>
{{$category->title}}
</h1> //в модели Post
public function user()
{
return $this->belongsTo('App\User','user_id', 'id');
}
// в контроллере
Post::with('user')->find($id);
// в шаблоне
{{$post->user->name}}public function getProductById($id)
{
return $this->products()->find($id);
}
public function hasProduct($id)
{
return $this->getProductById($id)->count()>0;
}if ($user->hasProduct($product->id)) {
// не продавать
}@if ($user->hasProduct($product->id))
<div class="danger">
Вы уже купили этот товар
</div>
@endifphp artisan make:resource PlayerResourceuse App\Http\Resources\CityResource as CityResource;public function toArray($request)
{
return [
// поля модели, которые нужно отдалть в json
'id' => $this->id,
'name' => $this->name,
// Отношения
'cities' => CityResource::collection($this->cities),
'city' => new CityResource($this->city)
];
}use App\Http\Resources\PlayerResource as PlayerResource;public function getPlayers()
{
$players = Player::with('cities')->get();
return PlayerResource::collection($players);
}public function friends(){
return $this->hasMany('App\Models\Friend, 'user_id', 'id');
}$user->friends()->get()
$user->friends()->find($id) public function getUsers()
{
return User::all();
}$this->getUsers();$book->authors()->attach($author_id);$book=Book::with('authors')
->withCount('authors')
->find($id);$books=Book::with('authors')
->withCount('authors')
->get();dd($book);@foreach ($books as $book)
//количество авторов у книги
{{$book->authors_count}}
//имена авторов
@foreach ($book->authors as $author)
{{ $author->name }}
@endforeach
@endforeachnamespace App\Http\Controllers\Admin; в верхней части контроллера Registered::class => [
SendEmailVerificationNotification::class,
'App\Listeners\Auth\LogRegisteredUser'
],
// 'Illuminate\Auth\Events\Registered' => [
// 'App\Listeners\Auth\LogRegisteredUser',
// ],php artisan config:cache return $this->hasManyThrough(
Catalog_generation::class,
Catalog_generation_body::class,
'current_model_id', //Внеший ключ текущей модели в Catalog_generation_body
'catalog_generation_body_id', // Внешний в Catalog_generation
'id' // Локальнй в текущей модели
)