I have these models :
class Article extends \yii\db\ActiveRecord
{
.....
public function getTags()
{
return $this->hasMany(Tag::className(), ['id' => 'tag_id'])->viaTable('rel_tag_article', ['article_id' => 'id']);
}
}
class News extends \yii\db\ActiveRecord
{
.....
public function getTags()
{
return $this->hasMany(Tag::className(), ['id' => 'tag_id'])->viaTable('rel_tag_news', ['news_id' => 'id']);
}
}
class Tag extends \yii\db\ActiveRecord
{
.....
public function getRelTagArticles()
{
return $this->hasMany(RelTagArticle::className(), ['tag_id' => 'id']);
}
public function getRelTagNews()
{
return $this->hasMany(RelTagNews::className(), ['tag_id' => 'id']);
}
}
And in the controller
class ArticleController extends \yii\web\Controller
{
public function actionArticle($id_article)
{
$article = User::find($id_article);
...... here ....
return $this->render('article');
}
}
Under ...here... I have to find the news that have common tags with my actual article. What is the right way?
## You can use article active record to get tags and in turn use tags to get news ##
class ArticleController extends \yii\web\Controller
{
public function actionArticle($id_article)
{
$article = User::find($id_article);
$tags=$article->tags;
$related_news=array();
foreach($tags as $tag) {
$related_news=array_merge($tag->getRelTagNews(),$related_news);
}
return $this->render('related_news');
}
}
Visit this yii2 page
Related
This is my ReportResource file
public function toArray($request)
{
return [
'id' => $this->id,
'name' => $this->cname,
'start' => $this->whenPivotLoaded('company_package', function () {
return $this->pivot->start_date;
}),
];
}
Company Model
class Company extends Model
{
//
public function packages(){
return $this->belongsToMany('App\Package')->withPivot('start_date');
}
}
Package Model
class Package extends Model
{
public function companies(){
return $this->belongsToMany('App\Company')->withPivot('start_date');
}
}
Controller
return new ReportResource(Company::with('packages')->get());
What I am doing wrong .Please help
I have filters which belong to filter groups
Filter.php:
namespace App;
use Illuminate\Database\Eloquent\Model;
class Filter extends Model
{
public function group()
{
return $this->belongsTo('App\FilterGroup');
}
public function products()
{
return $this->belongsToMany('App\Product', 'product_filters');
}
public function categories()
{
return $this->belongsToMany('App\Filter', 'category_filter');
}
}
And categories which have many to many relationship with the filters
Category.php:
namespace App;
use App\Events\CategoryDelete;
use Illuminate\Database\Eloquent\Model;
class Category extends Model
{
protected $events = [
'deleting' => CategoryDelete::class
];
protected $table = 'categories';
public function parent()
{
return $this->belongsTo('App\Category', 'parent_id');
}
public function children()
{
return $this->hasMany('App\Category', 'parent_id');
}
public function parents()
{
$parentCategories = collect([]);
$parent = $this->parent;
while (!is_null($parent)) {
$parentCategories[] = $parent;
$parent = $parent->parent;
}
return $parentCategories->reverse();
}
public function products()
{
return $this->hasMany('App\Product');
}
public function filters()
{
return $this->belongsToMany('App\Filter', 'category_filter');
}
public function hasFilter($filter_id)
{
foreach ($this->filters as $filter) {
if ($filter->id == $filter_id) {
return true;
}
}
return false;
}
public function getFilterGroups()
{
$filterGroups = collect([]);
foreach ($this->filters as $filter) {
if (!$filterGroups->has($filter->group->id)) {
$filterGroups[$filter->group->id] = $filter->group;
}
}
return $filterGroups;
}
}
And in the category view I want to display the filters along with their filter group but when I try the following:
#foreach($category->filters as $filter)
{{ $filter->group->name }}
<br>
#endforeach
It throws the Trying to get property of non-object exception
Why can't I get the group of the filter?
I changed the group() method inside the Filter model to filterGroup and now when I call $filter->filterGroup it works fine. I dont know why, maybe group is some reserved word or something.
I have following eloquent.
Volume,
Issue,
Category,
Article,
ArticleTranslation,
Volume can have many issues.
Issue can have many categories.
Category can have many articles.
Article can have many translation.
SO how can i get Volume from Article/ArticleTranslation?
First your models.
..
class Volume extends Model {
public function issues() {
return $this->hasMany(Issue::class);
}
}
class Issue extends Model {
public function volume() {
return $this->belongsTo(Volume::class);
}
public function categories() {
return $this->hasMany(Category::class);
}
}
class Category extends Model {
public function issue() {
return $this->belongsTo(Issue::class);
}
public function articles() {
return $this->hasMany(Article::class);
}
public function articlesTranslated() {
return $this->hasMany(ArticleTranslated::class);
}
}
class Article extends Model {
public function category() {
return $this->belongsTo(Category::class);
}
}
..
Then in your code:
..
$articles = Articles::all();
$volumes = [];
foreach ($articles as $article) {
$volumes[] = $article->category->issue->volume;
}
..
Docs
How can I get all the submissions belongs to a particular user?
Trying this:
$user=User::find(1);
dd($user->submissions);
Throwing error:
Call to undefined method Illuminate\Database\Query\Builder::hasMany()
Will I have to loop through the models?
Here are the models:
class User extends Eloquent implements ConfideUserInterface, BillableInterface
{
public function categories()
{
return $this->hasMany('Category');
}
public function forms()
{
return $this->hasManyThrough('App\Models\Form', 'Category');
}
public function submissions()
{
return $this->hasMany('Category')->hasMany('Form')->hasMany('Submission');
}
}
class Category extends \Eloquent {
public function user()
{
return $this->belongsTo('User');
}
public function forms()
{
return $this->hasMany('App\Models\Form');
}
public function submissions()
{
return $this->hasManyThrough('Submission', 'App\Models\Form', 'id', 'form_id');
}
}
namespace App\Models;
class Form extends \Eloquent {
public function user()
{
return $this->category->user();
}
public function category()
{
return $this->belongsTo('Category');
}
public function submissions()
{
return $this->hasMany('Submission');
}
}
class Submission extends \Eloquent {
public function user()
{
return $this->form->category->user();
}
public function category()
{
return $this->form->category();
}
public function form()
{
return $this->belongsTo('App\Models\Form');
}
}
That doesn't really work that way with chaining relations...
What you can do, is this:
$submissions = Submission::whereHas('form.category.user', function($q){
$q->where('id', 1);
})->get();
Note that whereHas with nested relationships has only been added in the latest Laravel 4 release. Make sure to composer update.
If i'm not getting your question wrongly, you need to define relationship correctly.
According to laravel 4.2 you can use below kind of code to define relations:
class User extends \Eloquent implements ConfideUserInterface, BillableInterface{
//...
public function submissions(){
return $this->hasMany('Submission');
}
}
//...
class Submission extends \Eloquent {
public function user()
{
return $this->belongsTo('User');
}
//...
}
To further reading take a look at: http://laravel.com/docs/4.2/eloquent#relationships
I'm new to PHP framework (using Yii framework). How to get a value in MySQL then display it at a view? I'm confused in defining a model, controller, and how to use it.
Model:
public $name;
public $info;
$product = product::find()->orderBy('name')->all();
public function tablename()
{
return 'productdata';
}
Controller:
public function actionView($id)
{
$this->render('view',array(
'model'=>$this->loadModel($id),
));
}
public function LoadModel($id){
$model=productdata::model()->find($id);
return $model;
}
Have you tried any tutorials?
[models/MyModel.php]
class MyModel extends CActiveRecord {
public function rules() {
return ['id, name, value', 'safe'];
}
}
[controllers/MyController.php]
class MyController extends CController {
public function actionView($id) {
$model = MyModel::model()->findByPk($id);
$attributes = Yii::app()->request->getParam('MyModel');
if ($attributes) {
if ($model->save()) {
$this->redirect('myController/admin');
} else {
throw new CHttpException(500, 'Model not saved. Use echo CActiveForm::validat($model);');
}
}
$this->render('view', ['model' => $model]);
}
}
[views/MyController/view.php]
<?php echo $model->name; ?>