How to retrieve value from the migrations and display them - php

I used this for my migrations.
<?php
use Anomaly\Streams\Platform\Database\Migration\Migration;
class SnipcartModuleProductsCreateProductsFields extends Migration
{
/**
* The addon fields.
*
* #var array
*/
protected $fields = [
'name' => 'anomaly.field_type.text',
'description' => 'anomaly.field_type.text',
'sku' => [
'type' => 'anomaly.field_type.slug',
'config' => [
'slugify' => 'name',
'type' => '-'
],
],
'price' => [
"type" => "anomaly.field_type.decimal",
"config" => [
"min" => 0
]
],
'image' => 'anomaly.field_type.file',
'tags' => [
'type' => 'anomaly.field_type.tags',
'config' => [
'free_input' => true
]
]
];
}
Routing it from here
web.php
Route::get('/products', function () {
return view('products','ProductsController#getProducts');
});
Now How would I retrieve those values from here
ProductControllers.php
//name,description and price all the values should be pass to the products.blade.php
public function getProducts(){
$products = [];
return view('products', ['products' => $products]);
}
Then display it here
products.blade.php
#if (products)
<div class="row">
#foreach ($products as $product)
<div class="col-sm-4">
<div class="card" style="margin-bottom: 2rem;">
<div class="card-body">
<h4 class="card-title">{{ $product->name }}</h4>
<img />
<p class="card-text">{{ $product->description }}</p>
Buy for {{ $product->price }}
</div>
</div>
</div>
#endforeach
</div>
#endif

Related

Laravel different styles

Hello i have one problem i have Article model its haves 5 types and one of the types must have different style in html
model
public static function scopeAllNews($query)
{
return Article::whereIn('type', ['news', 'interview', 'analytic', 'quote'])->statusOn()->orderBy('created_at', 'DESC');
}
controller
public function news()
{
$newsFirstLine = Article::allNews()->get()->take(8);
$newsSecondLine = Article::allNews()->FilterContent($newsFirstLine->pluck('id'))->get();
$populars = Article::popular();
$articles = Article::all();
return view('pages/news/news-list', compact('newsFirstLine', 'newsSecondLine', 'populars', 'articles'));
}
blade
<div class="container w-100 d-block d-xs-none">
<div class="d-flex w-100">
<span class="fas fa-yellow-bullet text-yellow pr-10px font-size-20"></span>
<div class="font-size-20 font-size-xs-16 font-bold-caps text-blue-100">#tr('web.news')</div>
</div>
<div class="row">
#foreach($newsFirstLine as $news_item)
#include("cards/interviews", [
'image' => $news_item->image,
'slug' => $news_item->getUrl(),
'title' => $news_item->tr('title'),
'created' => $news_item->created_at->format('d.F.Y'),
'createdHi' => $news_item->created_at->format('H:i'),
])
#endforeach
#if($articles->type == 'quote')
#foreach($newsFirstLine as $quote)
#include("cards/quote", [
'slug' => $quote->getUrl(),
'title' => $quote->tr('title'),
'avatar' => $quote->respondent->image,
'publicPerson' => $quote->respondent->tr('name'),
'created' => $quote->created_at->format('d.m.Y'),
'createdHi' => $quote->created_at->format('H:i'),
])
#endforeach
#endif
</div>
</div>
#include("home.popularNews")
<div class="container w-100">
<div class="row">
#foreach($newsSecondLine as $news_item)
#include("cards/interviews", [
'image' => $news_item->image,
'slug' => $news_item->getUrl(),
'title' => $news_item->tr('title'),
'created' => $news_item->created_at->format('d.m.Y'),
'createdHi' => $news_item->created_at->format('H:i'),
])
#endforeach
#if($articles->type == 'quote')
#foreach($newsFirstLine as $quote)
#include("cards/quote", [
'slug' => $quote->getUrl(),
'title' => $quote->tr('title'),
'avatar' => $quote->respondent->image,
'publicPerson' => $quote->respondent->tr('name'),
'created' => $quote->created_at->format('d.m.Y'),
'createdHi' => $quote->created_at->format('H:i'),
])
#endforeach
#endif
</div>
</div>
but it's don't working if type will be quote it must include cards/quote and if its not quote type it must include cards/interviews
what's i'm doing wrong? (

Laravel ORM Eloquent doesn't work properly on shared hosting but wokrs good on local

Here i ve got error Trying to get property 'form' of non-object (View: /home/laravel/web/laravel.swt101.eu/public_html/abonamenty/resources/views/invoices/show.blade.php) in view when i am trying to get value from foreign key, from form model using Laravel's Eloquent. In this line: {{ $invoice->form->form }}. I had several problems with eloquent and have to use DB without Eloquent many times. Same code works fine for another view (proform) it has got same things like invoice, but not gives error on shared hosting. This {{ $invoice->form->form }} in invoices.show view works fine only on local host. I don't know what is going on. Can anybody help me? Please. If there is a problem with server, can anybody help me with how to configurate shared hosting for Laravel Eloquent?
This is link to shared error. I get this error only on shared hosting. on local it works fine
https://flareapp.io/share/x7XVWgma
This is my invoice model
<?php
namespace App;
use App\Form;
use Kyslik\ColumnSortable\Sortable;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
class Invoice extends Model
{
/**
* The attributes that are mass assignable.
*
* #var array
*/
use SoftDeletes;
use Sortable;
protected $table = 'invoices';
protected $fillable = [
'invoicenumber', 'invoicedate', 'id', 'selldate', 'user_id', 'paymentmethod',
'paymentdate', 'status', 'comments', 'city', 'paid', 'autonumber', 'automonth', 'autoyear', 'name',
'PKWIU', 'quantity', 'unit', 'netunit', 'nettotal',
'VATrate', 'grossunit', 'grosstotal', 'form_id', 'currency_id',
];
public $sortable = [ 'invoicenumber', 'invoicedate', 'id', 'selldate', 'user_id', 'paymentmethod',
'paymentdate', 'status', 'comments', 'city', 'paid', 'autonumber', 'automonth', 'autoyear', 'name',
'PKWIU', 'quantity', 'unit', 'netunit', 'nettotal',
'VATrate', 'grossunit', 'grosstotal', 'form_id', 'currency_id',
];
protected $dates = ['deleted_at'];
public $primaryKey = 'id';
public function user()
{
return $this->belongsTo('App\User');
}
public function form()
{
return $this->hasOne('App\Form', 'id', 'form_id');
}
public function currency()
{
return $this->hasOne('App\Currency');
}
public function proform()
{
return $this->belongsTo('App\Proform');
}
}
This is my form model:
<?php
namespace App;
use App\Invoice;
use Kyslik\ColumnSortable\Sortable;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
class Form extends Model
{
//
protected $table= 'forms';
protected $fillable = [
'id', 'form',
];
public function invoice()
{
return $this->belongsTo('App\Invoice', 'id', 'form_id');
}
public function proform()
{
return $this->belongsTo('App\Proform');
}
}
This is show method of controller:
public function show(Invoice $invoice)
{
$users = User::all('showname','id');
$forms = Form::all('id', 'form');
$currencys = Currency::all('id', 'currency', 'course');
$query = DB::table('forms')
->join('invoices', 'forms.id', '=', 'invoices.form_id');
$query1 = DB::table('currencys')
->join('invoices', 'currencys.id', '=', 'invoices.currency_id');
/*$invoice2 = Invoice::with('form')->find($invoice->id);*/
$invoice2 = Invoice::find($invoice->id);
$form = $invoice2->form;
$invoice->id;
/* dd($invoice2->form); */
return view('invoices.show', compact('users', 'forms', 'currencys', 'invoice', 'query', 'query1', 'invoice2'));
}
This is my show view:
#extends('layouts.app')
#section('content')
<div class="row">
<div class="col-lg-12 margin-tb">
<div class="pull-left">
<h2>Szczegóły abonamentu</h2>
</div>
<div class="pull-right">
<a class="btn btn-primary" href="{{ route('invoices.index') }}"> Wstecz</a>
</div>
</div>
</div>
<div class="row">
<div class="col-xs-12 col-sm-12 col-md-12">
<div class="form-group">
<strong>Numer faktury:</strong>
{{ $invoice->autonumber }}
</div>
</div>
<div class="col-xs-12 col-sm-12 col-md-12">
<div class="form-group">
<strong>Kontrahent:</strong>
{{ $invoice->user->showname }}
</div>
</div>
<div class="col-xs-12 col-sm-12 col-md-12">
<div class="form-group">
<strong>Forma płatności:</strong>
{{ $invoice->form->form }}
</div>
</div>
<div class="col-xs-12 col-sm-12 col-md-12">
<div class="form-group">
<strong>Waluta:</strong>
</div>
</div>
<div class="col-xs-12 col-sm-12 col-md-12">
<div class="form-group">
<strong>Data wystawienia faktury:</strong>
{{ $invoice->invoicedate }}
</div>
</div>
<div class="col-xs-12 col-sm-12 col-md-12">
<div class="form-group">
<strong>Data sprzedaży:</strong>
{{ $invoice->selldate }}
</div>
</div>
<div class="col-xs-12 col-sm-12 col-md-12">
<div class="form-group">
<strong>Metoda płatności:</strong>
{{ $invoice->paymentmethod }}
</div>
</div>
<div class="col-xs-12 col-sm-12 col-md-12">
<div class="form-group">
<strong>Termin płatności:</strong>
{{ $invoice->paymentdate }}
</div>
</div>
<div class="col-xs-12 col-sm-12 col-md-12">
<div class="form-group">
<strong>Miejsce wystawienia:</strong>
{{ $invoice->city }}
</div>
</div>
<div class="col-xs-12 col-sm-12 col-md-12">
<div class="form-group">
<strong>Wpłacono:</strong>
{{ $invoice->paid }}
</div>
</div>
<div class="col-xs-12 col-sm-12 col-md-12">
<div class="form-group">
<strong>Status:</strong>
{{ $invoice->status }}
</div>
</div>
<div class="col-xs-12 col-sm-12 col-md-12">
<div class="form-group">
<strong>Uwagi:</strong>
{{ $invoice->comments }}
</div>
</div>
<div class="pull-left" style="margin: 15px;">
<h3>Pozycje proformy</h3>
</div>
<div class="col-xs-12 col-sm-12 col-md-12">
<div class="form-group">
<strong>Nazwa towaru lub usługi:</strong>
{{ $invoice->name }}
</div>
</div>
<div class="col-xs-12 col-sm-12 col-md-12">
<div class="form-group">
<strong>PKWiU:</strong>
{{ $invoice->PKWIU }}
</div>
</div>
<div class="col-xs-12 col-sm-12 col-md-12">
<div class="form-group">
<strong>Ilość:</strong>
{{ $invoice->quantity }}
</div>
</div>
<div class="col-xs-12 col-sm-12 col-md-12">
<div class="form-group">
<strong>Jednostka:</strong>
{{ $invoice->unit }}
</div>
</div>
<div class="col-xs-12 col-sm-12 col-md-12">
<div class="form-group">
<strong>Cena netto jednostki:</strong>
{{ $invoice->netunit }}
</div>
</div>
<div class="col-xs-12 col-sm-12 col-md-12">
<div class="form-group">
<strong>Netto razem:</strong>
{{ $invoice->nettotal }}
</div>
</div>
<div class="col-xs-12 col-sm-12 col-md-12">
<div class="form-group">
<strong>Stawka VAT:</strong>
{{ $invoice->VATrate }}
</div>
</div>
<div class="col-xs-12 col-sm-12 col-md-12">
<div class="form-group">
<strong>Brutto jednostka:</strong>
{{ $invoice->grossunit }}
</div>
</div>
<div class="col-xs-12 col-sm-12 col-md-12">
<div class="form-group">
<strong>Brutto razem:</strong>
{{ $invoice->grosstotal }}
</div>
</div>
</div>
#endsection
This is all code of invoice controller with show method
<?php
namespace App\Http\Controllers;
use Kyslik\ColumnSortable\Sortable;
use App\Invoice;
use Illuminate\Http\Request;
use App\User;
use App\Proform;
use App\Form;
use App\Currency;
use DB;
class InvoiceController extends Controller
{
/**
* Display a listing of the resource.
*
* #return \Illuminate\Http\Response
*/
function __construct()
{
$this->middleware('permission:product-list|product-create|product-edit|product-delete', ['only' => ['index','show']]);
$this->middleware('permission:product-create', ['only' => ['create','store']]);
$this->middleware('permission:product-edit', ['only' => ['edit','update']]);
$this->middleware('permission:product-delete', ['only' => ['destroy']]);
}
public function search4(Request $request)
{
$user = User::all('showname','id');
$invoices = Invoice::sortable()->paginate(5);
$query = DB::table('users')
->join('invoices', 'users.id', '=', 'invoices.user_id');
$search = $request->get('search');
$requestData = ['showname'];
/* $query = Proform::query(); */
foreach ($requestData as $field){
$query->orWhere($field, 'like', '%'.$search.'%');
}
$data2=$query->paginate(5);
return view('invoices.index', ['invoices' => $data2, 'user'=> $user])->with('i', ($request->input('page', 1) - 1) * 5);
}
/**
* Display a listing of the resource.
*
* #return \Illuminate\Http\Response
*/
public function index()
{
$user = User::all('showname','id');
$invoices = Invoice::sortable()->paginate(5);
return view('invoices.index',compact('invoices', 'user'))
->with('i', (request()->input('page', 1) - 1) * 5);
}
/**
* Show the form for creating a new resource.
*
* #return \Illuminate\Http\Response
*/
public function create()
{
$users = User::all('showname','id');
$forms = Form::all('id', 'form');
$currencys = Currency::all('id', 'currency', 'course');
return view('invoices.create', compact('users', 'forms', 'currencys'));
}
/**
* Store a newly created resource in storage.
*
* #param \Illuminate\Http\Request $request
* #return \Illuminate\Http\Response
*/
public function store(Request $request)
{
$autoyear = date('Y');
$automonth = date('m');
$autonumber = DB::table('proforms')
->select(DB::raw('MAX(autonumber) as autonumber'))
->where('automonth', '=', '$automonth')
->where('autoyear', '=', '$autoyear')
->get();
#$auto = #$autonumber[0]->autonumber;
#$auto[0]++;
print_r($auto);
$number = "$auto[0]/$automonth/$autoyear/faktura";
$validatedData = request()->validate([
'invoicedate' => 'required',
'user_id' => 'required',
'selldate' => 'required',
'paymentdate' => 'required',
'paymentmethod' => 'required',
'status' => 'required',
'comments' => 'nullable',
'city' => 'nullable',
'paid' => 'nullable',
'name' => 'required',
'PKWIU' => 'nullable',
'quantity' => 'required',
'unit' => 'required',
'netunit' => 'required',
'nettotal' => 'required',
'VATrate' => 'required',
'grossunit' => 'required',
'grosstotal' => 'required',
]);
DB::table('invoices')->insert([
['invoicenumber' => $request->invoicenumber,
'invoicedate' => $request->invoicedate,
'selldate' => $request->selldate,
'user_id' => $request->user_id,
'form_id' => $request->form_id,
'currency_id' => $request->currency_id,
'paymentmethod' => $request->paymentmethod,
'paymentdate' => $request->paymentdate,
'status' => $request->status,
'comments' => $request->comments,
'city' => $request->city,
'autonumber' => $request->autonumber,
'automonth' => $request->automonth,
'autoyear' => $request->autoyear,
'name' => $request->name,
'PKWIU' => $request->PKWIU,
'quantity' => $request->quantity,
'unit' => $request->unit,
'netunit' => $request->netunit,
'nettotal' => $request->nettotal,
'VATrate' => $request->VATrate,
'grossunit' => $request->grossunit,
'grosstotal' => $request->grosstotal,
'autonumber' => $number,
'automonth' => $automonth,
'autoyear'=> $autoyear],
]);
return redirect()->route('invoices.index')
->with('success','Invoice created successfully.');
}
/**
* Display the specified resource.
*
* #param \App\Product $product
* #return \Illuminate\Http\Response
*/
public function show(Invoice $invoice)
{
$users = User::all('showname','id');
$forms = Form::all('id', 'form');
$currencys = Currency::all('id', 'currency', 'course');
$query = DB::table('forms')
->join('invoices', 'forms.id', '=', 'invoices.form_id');
$query1 = DB::table('currencys')
->join('invoices', 'currencys.id', '=', 'invoices.currency_id');
/*$invoice2 = Invoice::with('form')->find($invoice->id);*/
$invoice2 = Invoice::find($invoice->id);
$form = $invoice2->form;
$invoice->id;
/* dd($invoice2->form); */
return view('invoices.show', compact('users', 'forms', 'currencys', 'invoice', 'query', 'query1', 'invoice2'));
}
/**
* Show the form for editing the specified resource.
*
* #param \App\Product $product
* #return \Illuminate\Http\Response
*/
public function edit(Invoice $invoice)
{
$users = User::all('showname','id');
$forms = Form::all('id', 'form');
$currencys = Currency::all('id', 'currency', 'course');
return view('invoices.edit',compact('invoice', 'users', 'forms', 'currencys'));
}
/**
* Update the specified resource in storage.
*
* #param \Illuminate\Http\Request $request
* #param \App\Product $product
* #return \Illuminate\Http\Response
*/
public function update(Request $request, Invoice $invoice)
{
request()->validate([
'invoicedate' => 'required',
'user_id' => 'required',
'selldate' => 'required',
'paymentdate' => 'required',
'paymentmethod' => 'required',
'status' => 'required',
'comments' => 'nullable',
'city' => 'nullable',
'paid' => 'nullable',
'name' => 'required',
'PKWIU' => 'nullable',
'quantity' => 'required',
'unit' => 'required',
'netunit' => 'required',
'nettotal' => 'required',
'VATrate' => 'required',
'grossunit' => 'required',
'grosstotal' => 'required',
]);
/* $invoice->update($request->all()); */
DB::table('invoices')
->where('id', $invoice->id)
->update([
'invoicenumber' => $request->invoicenumber,
'invoicedate' => $request->invoicedate,
'selldate' => $request->selldate,
'user_id' => $request->user_id,
'form_id' => $request->form_id,
'currency_id' => $request->currency_id,
'paymentmethod' => $request->paymentmethod,
'paymentdate' => $request->paymentdate,
'status' => $request->status,
'comments' => $request->comments,
'city' => $request->city,
'autonumber' => $request->autonumber,
'automonth' => $request->automonth,
'autoyear' => $request->autoyear,
'name' => $request->name,
'PKWIU' => $request->PKWIU,
'quantity' => $request->quantity,
'unit' => $request->unit,
'netunit' => $request->netunit,
'nettotal' => $request->nettotal,
'VATrate' => $request->VATrate,
'grossunit' => $request->grossunit,
'grosstotal' => $request->grosstotal,
]);
return redirect()->route('invoices.index')
->with('success','Invoice updated successfully');
}
/**
* Remove the specified resource from storage.
*
* #param \App\Product $product
* #return \Illuminate\Http\Response
*/
public function destroy(Invoice $invoice)
{
/* $invoice->delete(); */
DB::table('invoices')
->where('id', $invoice->id)
->update(['deleted' => 1]);
return redirect()->route('invoices.index')
->with('success','Invoice deleted successfully');
}
}

File Upload field using ActiveForm in Yii2 framework does not work

I want to add a File Upload field in an ActiveForm in Yii2 framework, but it's not working, the file is not uploaded nor the name of the file is stored in the database column 'company_cover'.
If someone could help me find my mistake.
Thanks in advance.
The VIEW
<div class="block-body collapse" id="company-block">
<?php $form = ActiveForm::begin([
'id' => 'form-change-company',
'method' => 'post',
'action' => ['account/update-company'],
'enableAjaxValidation' => false,
'options' => ['enctype' => 'multipart/form-data'],
]);
?>
<div class="row">
<div class="col-lg-2 col-md-2 col-sm-2 col-xs-12">
<?= $form->field($modelAbout, 'group_id', [
'template' => '{input} {error}',
])->dropDownList([2 => t('app','Enable'), 1 => t('app','Disable')])->label(false);
?>
</div>
</div>
<div class="row">
<div class="col-lg-4 col-md-4 col-sm-6 col-xs-12">
<?= $form->field($modelCompany, 'store_name', [
'template' => '{input} {error}',
])->textInput([ 'placeholder' => t('app','Store Name'), 'class' => ''])->label(false); ?>
</div>
<div class="col-lg-4 col-md-4 col-sm-6 col-xs-12">
<?= $form->field($modelCompany, 'company_name', [
'template' => '{input} {error}',
])->textInput([ 'placeholder' => t('app','Company Name'), 'class' => ''])->label(false); ?>
</div>
<div class="col-lg-4 col-md-4 col-sm-6 col-xs-12">
<?= $form->field($modelCompany, 'company_mail', [
'template' => '{input} {error}',
])->textInput([ 'placeholder' => t('app','Company E-Mail'), 'class' => ''])->label(false); ?>
</div>
</div>
<div class="row">
<div class="col-lg-4 col-md-4 col-sm-6 col-xs-12">
<?= $form->field($modelCompany, 'company_no', [
'template' => '{input} {error}',
])->textInput([ 'placeholder' => t('app','Company No'), 'class' => ''])->label(false); ?>
</div>
<div class="col-lg-4 col-md-4 col-sm-6 col-xs-12">
<?= $form->field($modelCompany, 'vat', [
'template' => '{input} {error}',
])->textInput([ 'placeholder' => t('app','VAT'), 'class' => ''])->label(false); ?>
</div>
</div>
<div class="row">
<div class="col-lg-4 col-md-4 col-sm-6 col-xs-12">
</div>
</div>
<div class="row">
<div class="col-lg-4 col-md-4 col-sm-6 col-xs-12">
<?= $form->field($modelCompany, 'company_lat', [
'template' => '{input} {error}',
])->textInput([ 'placeholder' => t('app','Company Latitude'), 'class' => ''])->label(false); ?>
</div>
<div class="col-lg-4 col-md-4 col-sm-6 col-xs-12">
<?= $form->field($modelCompany, 'company_lng', [
'template' => '{input} {error}',
])->textInput([ 'placeholder' => t('app','Company Longitude'), 'class' => ''])->label(false); ?>
</div>
</div>
<div class="row">
<div class="col-lg-4 col-md-4 col-sm-6 col-xs-12">
<?= $form->field($modelCompany, 'file')->fileInput([ 'placeholder' => t('app','Company Cover'), 'class' => '']) ?>
</div>
</div>
<div class="row">
<div class="col-lg-4 col-md-4 col-sm-6 col-xs-12">
<button type="submit" id="submit-company-info" class="btn-as" value="Submit"><?=t('app','Submit');?></button>
</div>
</div>
<?php ActiveForm::end(); ?>
</div>
</div>
The MODEL
class CustomerStore extends \app\models\auto\CustomerStore
{
// when inactive model
const STATUS_INACTIVE = 'inactive';
// when active model
const STATUS_ACTIVE = 'active';
// when deactivated
const STATUS_DEACTIVATED = 'deactivated';
public $file;
public function rules()
{
return [
[['store_name', 'company_name', 'company_mail', 'company_lat', 'company_lng', 'company_no', 'vat'], 'trim'],
[['store_name', 'company_name'], 'required'],
[['file'], 'file'],
[['store_name', 'company_name'], 'string', 'max' => 30],
[['company_no', 'vat'], 'string', 'max' => 20],
[['status'], 'safe']
];
}
/**
* #inheritdoc
*/
public function behaviors()
{
$behaviors = parent::behaviors();
$behaviors[] = [
'class' => SluggableBehavior::className(),
'value' => [$this, 'getSlug'] //https://github.com/yiisoft/yii2/issues/7773
];
return $behaviors;
}
/**
* #inheritdoc
*/
public function attributeLabels()
{
return ArrayHelper::merge(parent::attributeLabels(), [
'store_id' => t('app', 'Store ID'),
'customer_id' => t('app', 'Customer'),
'store_name' => t('app', 'Store Name'),
'company_name' => t('app', 'Company Name'),
'company_mail' => t('app', 'Company E-Mail'),
'company_no' => t('app', 'Company No'),
'vat' => t('app', 'VAT'),
'status' => t('app', 'Status'),
'company_lat' => t('app', 'Company Latitude'),
'company_lng' => t('app', 'Company Longitude'),
'company_cover' => t('app', 'Company Cover'),
'created_at' => t('app', 'Created At'),
'updated_at' => t('app', 'Updated At'),
]);
}
/**
* #return \yii\db\ActiveQuery
*/
public function getCustomer()
{
return $this->hasOne(Customer::className(), ['customer_id' => 'customer_id']);
}
/**
* #return bool
*/
public function deactivate()
{
$this->status = self::STATUS_INACTIVE;
$this->save(false);
return true;
}
/**
* #return bool
*/
public function activate()
{
if($this->status == self::STATUS_DEACTIVATED || $this->status == self::STATUS_INACTIVE) {
$this->status = self::STATUS_ACTIVE;
$this->save(false);
}
return true;
}
/**
* #param $slug
* #return array|null|\yii\db\ActiveRecord
*/
public function findBySlug($slug)
{
return $this->find()->where(array(
'slug' => $slug,
))->one();
}
/**
* #param $event
* #return string
* //https://github.com/yiisoft/yii2/issues/7773
*/
public function getSlug($event)
{
if(!empty($event->sender->slug)) {
return $event->sender->slug;
}
return Inflector::slug($event->sender->store_name);
}
}
The CONTROLLER (the controller is quite long so i just paste the part for the file upload field)
public function actionUpload()
{
$modelCompany = new CustomerStore();
if (Yii::$app->request->isPost) {
$modelCompany->file = UploadedFile::getInstance($modelCompany, 'file');
if ($modelCompany->validate()) {
$modelCompany->file->saveAs('/' . $modelCompany->file->baseName . '.' . $modelCompany->file->extension);
}
}
return $this->render('upload', ['model' => $modelCompany]);
}
I think you must to load post data to the model. You can try do like this
if ($modelCompany->load(Yii::$app->request->post())) {
$modelCompany->file = UploadedFile::getInstance($modelCompany, 'file');
if ($modelCompany->validate()) {
$modelCompany->file->saveAs('/' . $modelCompany->file->baseName . '.' . $modelCompany->file->extension);
}
}

Twill CMS (Laravel): How to set up Repeater blocks? Repeaters display at wrong place

I wanted to use the repeater blocks of twill for my new project, set it up like in the documentation but it displays outside the section area just right beneath it in the body. Can´t edit it in the cms. How do I declare the right variable in the component to link to the repeater items? Or is there something else I forgot? Couldn´t find anything specific about my issue.
Checked everything multiple times, and tried different syntax and different input types
../site/blocks/competences.blade.php
#php
/** #var A17\Twill\Models\Behaviors\HasMedias $block */
#endphp
<div class="competences">
<div class="l-container">
<div class="competences-wrapper">
<h1 class="competences-headline">{{ $block->translatedInput('headline') }}</h1>
{!! $block->input('items') !!}
</div>
</div>
</div>
../admin/blocks/competences.blade.php
#formField('input', [
'name' => 'headline',
'label' => 'Headline',
'required' => true,
'translated' => true
])
#formField('repeater', [
'name' => 'items',
'type' => 'competences_item'
])
twill.php
<?php
return [
'block_editor' => [
'blocks' => [
'competences' => [
'title' => 'Competences',
'icon' => 'text',
'component' => 'a17-block-competences'
]
],
'repeaters' => [
'competences_item' => [
'title' => 'Competences Item',
'trigger' => 'Add competence',
'component' => 'a17-block-competences_item',
'min' => 3,
'max' => 3
]
],
]
];
should display inside the section/div, but displays beneath it just inside the body with of course no CSS styles
Fixed it this way:
#php
/** #var A17\Twill\Models\Behaviors\HasMedias $block */
#endphp
<div class="competences">
<div class="l-container">
<div class="competences-wrapper">
<h1 class="competences-headline">{{ $block->translatedInput('headline') }}</h1>
#foreach ($block->children as $child)
#include('site.blocks.competences_item', ['child', $child])
#endforeach
</div>
</div>
</div>

Active Record in Yii2, Joining multiple table, find the rating of a thread, and retrieve the rating in ListView

I am confused with how to retrieve data that has been averaged after joining multiple tables.
Problem:
I have 3 tables i want to join, User, Thread, and Rate.
I need all data from the tables and for rate table, i need to do average operation to get the rating value
My code is:
MyController looks like this:
public function actionHome(){
$dataProvider = new ActiveDataProvider([
'query' => Thread::retrieveAll(),
'pagination' => [
'pageSize' =>5,
],
]);
return $this->render('home', ['listDataProvider' => $dataProvider]);
}
My active record class (model class) looks like this:
public static function retrieveAll(){
return Self::find()->joinWith('user')
->joinWith('rate')
->select(['AVG(rate.rating) as rating, thread.*, user.*'])
->groupBy('rate.thread_id');
}
public function getUser(){
return $this->hasOne(User::className(), ['id' => 'user_id']);
}
public function getRate(){
return $this->hasMany(Rate::className(), ['thread_id' =>'thread_id']);
}
Retrieve all is used in the data provider query
My home class looks like this:
<div class="col-md-6">
<?= ListView::widget([
'dataProvider' => $listDataProvider,
'options' => [
'tag' => 'div',
'class' => 'list-wrapper',
'id' => 'list-wrapper',
],
'layout' => "{summary}\n{items}\n{pager}",
'itemView' => function ($model, $key, $index, $widget) {
return $this->render('_list_thread',['model' => $model]);
},
'pager' => [
'firstPageLabel' => 'first',
'lastPageLabel' => 'last',
'nextPageLabel' => 'next',
'prevPageLabel' => 'previous',
'maxButtonCount' => 3,
],
]) ?>
</div>
my item for the listview looks like this:
<article>
<div class="box col-md-12">
<div class="row">
<?= Html::a($model->title, Url::to('thread/index.php?id='. $model->thread_id))?>
</div>
<div class="row">
<p> <?= $model->content ?> </p>
</div>
<div class="row">
<div class="col-md-5">
<?= StarRating::widget([
'name' => 'rating_2',
'value' => 2.5,
'readonly' => true,
'pluginOptions' => [
'showCaption' => false,
'min' => 0,
'max' => 5,
'step' => 1,
'size' => 'xs',
]])?>
</div class="col-md-7" align="center center-vertical">
<p align="right" style="font-size:8px">Created by <?= $model->user->first_name?> <?=$model->user->last_name?> at <?= $model->date_created ?></p>
</div>
</div>
<br><br><br>
<br><br><br><br><br><br><br><br><br>
</article>
I can retrieve $model->user->first_name and lst_name but i cannot retrieve $model->rate->rating
Any suggestion for this?
$this::select('AVG(rate.rating) as rating',*)->innerJoin('rate','your codition on you have to take a join')->innerJoin('thread','your codition on you have to take a join')->all()->groupBy('rate.thread_id');

Categories