public function scopeGetCategories($query){
$categories = clone $query;
return $categories->joinRelations('categories')
->selectRaw('*, count(`product_category_values`.`product_id`) as products_count')
->whereNotNull('category_id')->groupBy('category_id')
->getCache(['product_categories.*']);
}
$users = User::with(['posts' => function($query)
{
$query->where('title', 'like', '%первое%');
},
'foo',
'bar' => function($query){
// bar query
}])->get();
Category::with('params.values')->whereHas('params', function($query){
$query->where('something', 'foobar');
})->get();
public function get(Request $request)
php artisan make:model Localization
protected $table = 'localization';
public function lozalizable()
{
return $this->morphTo();
}
public function lozalization(){
return $this->morphOne('App\Localization', 'lozalizable');
}
Schema::create('localization', function (Blueprint $table) {
$table->increments('id');
$table->string('field');
$table->string('language');
$table->string('value');
$table->string('lozalizable_type');
$table->integer('lozalizable_id');
$table->timestamps();
});
$article = Article::create($Atricle);
$localization = new Localization;
$localization->language = 'en';
$localization->field = 'content';
$localization->value = 'Znachenye na english yazike';
$article->localization()->save($localization); //привязываем к свежесозданному объекту Article новую локализацию
public function scopeGetLocalize($language, $field){
return $this->localization()->where(['language' => $language, 'field' => $field])-> firstOrFail()->value;
}
$article->getLocalize('en', 'title')
class Image extends Model
{
public function imageable()
{
return $this->morphTo();
}
}
class Post extends Model
{
public function images()
{
return $this->morphMany('App\Image', 'imageable');
}
}
class Post extends Model
{
public function images()
{
return $this->morphMany('App\Image', 'imageable')->withPivot('imageable_type');
}
}
public function getNameAttribute() {
switch($this->pivot->imageable_type){
case 'App\Post':
// что вы хотите сделать с $this->name если тип - App\Post
break;
}
}
function listen($event, $fire){
Event::listen($event, $fire);
}
event('entity.news.add');
listen('entity.news.add', function(){
//тут код
});
Route::group(['middleware' => ['web']], function () {
// тут храните все ваши роуты, которым нужны формы (GET, POST)
});
if(Auth::attempt([
'username' => $request->login,
'password' => $request->password
], $request->remember)){
return ['message' => 'success'];
}
return ['message' => 'error'];
public function postLogin(Request $request)
{
$this->validate($request, [
$this->loginUsername() => 'required', 'password' => 'required',
]);
// If the class is using the ThrottlesLogins trait, we can automatically throttle
// the login attempts for this application. We'll key this by the username and
// the IP address of the client making these requests into this application.
$throttles = $this->isUsingThrottlesLoginsTrait();
if ($throttles && $this->hasTooManyLoginAttempts($request)) {
return $this->sendLockoutResponse($request);
}
$credentials = $this->getCredentials($request);
$login = $request->input($this->loginUsername());
$field = filter_var($login, FILTER_VALIDATE_EMAIL) ? 'email':'username';
$request->merge([$field => $login]);
$credentials = $request->only($field, 'password');
if (Auth::attempt($credentials, $request->has('remember'))) {
return $this->handleUserWasAuthenticated($request, $throttles);
}
// If the login attempt was unsuccessful we will increment the number of attempts
// to login and redirect the user back to the login form. Of course, when this
// user surpasses their maximum number of attempts they will get locked out.
if ($throttles) {
$this->incrementLoginAttempts($request);
}
return redirect($this->loginPath())
->withInput($request->only($this->loginUsername(), 'remember'))
->withErrors([
$this->loginUsername() => $this->getFailedLoginMessage(),
]);
}