I got this code:
public function Locate($name){
$locate= Products::where('name', 'like', "$name%") ->get();
return response()->json($locate, 200);
}
I want to access belongsTo by this $name and put it in json
Here is my model:
class Products extends Model
{
protected $fillable = ['name', 'code', 'price'];
protected $hidden = ['id'];
public function Group()
{
return $this->belongsTo('App\pGroup', 'product_id', 'id');
}
}
If you want to get a relationship :
$locate= Products::where('name', 'like', "$name%")->with('group')->get();
Related
I have a logger user I want to show the articles, categories, for this user only.
Database schema:
Users: category: article:
id id id
name name title
users_id categories_id
User Model
class User extends Model
public function Category
{
return $this->hasMany(Category::class,'users_id');
}
category model (works)
class Category extends Model
{
public function article
{
return $this->hasMany(Article::class );
}
public function users
{
return $this->belongsTo(User::class, 'users_id');
}
article model (works)
class Article extends Model
{
public function category()
{
return $this->belongsTo(Category::class,'categories_id');
}
Controller category
class CategoryController extends Controller
{
public function index()
{
$categories = Category::all();
return view('category. view', compact('categories'));
}
class Article Controller extends Controller
{
public function index()
{
$articles = Article::all();
return view(' article . index', compact('articles'));}
In your controller you can use the following code:
public function getArticles(){
$user = auth()->user();
$articles = $user->articles;
return view('articlesView',array('articles' => $articles));
}
public function getCategories(){
$user = auth()->user();
$articles = $user->categories;
return view('categoryView',array('articles' => $articles));
}
In order for these functions to work you need to add these methods to your User model:
public function categories(){
return $this->hasMany('App\Category');
}
public function articles(){
return $this->hasMany('App\Articles');
}
A model fields and B model fields in same form
Ex:
public static function tableName()
{
return 'a';
}
public function rules()
{
return [
[['id','b_id'], 'id'],
];
}
public function beforeSave($insert)
{
//I want to save b model here
}
I cant access B model attributes in beforeSave()
I want use dynamic form widget (Demo 2:file upload)
URL:http://wbraganca.com/yii2extensions/dynamicform-demo2/source-code
Every thing is good but when i prees 'minus' button or 'new' button i recieve this error from ActiveForm.js:
TypeError: $(...).data(...) is undefined
methods.find()
$.fn.yiiActiveForm()
_fixFormValidaton/<()
.each()
jQuery.prototype.each()
_fixFormValidaton()
_addItem()
methods.addItem()
$.fn.yiiDynamicForm()
what is the problem?
what's the problem?
my AppAsset :
class AppAsset extends AssetBundle
{
public $basePath = '#webroot';
public $baseUrl = '#web';
public $css = [
'css/site.css',
'css/tabs.css',
'css/tabstyles.css',
];
public $js = [
'js/cbpFWTabs.js',
'js/cbpHorizontalMenu.js',
];
public $depends = [
'yii\web\YiiAsset',
'yii\bootstrap\BootstrapAsset',
];
}
HI All,
This example will help you how to get products details of customer using restful API call,
your API call should like this :
http://127.0.0.1:8080/api/web/customer?expand=orders,orderItems,products
class Customer extends \yii\db\ActiveRecord
{
/**
* #inheritdoc
*/
public static function tableName()
{
return 'customer';
}
/**
* #inheritdoc
*/
public function rules()
{
return [
[['ID', 'Name'], 'required'],
[['ID'], 'integer'],
[['Name'], 'string'],
[['PhoneNo'], 'string', 'max' => 45]
];
}
/**
* #inheritdoc
*/
public function attributeLabels()
{
return [
'ID' => 'ID',
'Name' => 'Name',
'PhoneNo' => 'Phone No',
];
}
/**
* #return \yii\db\ActiveQuery
*/
public function getOrders()
{
return $this->hasMany(Order::className(), ['customer_id' => 'ID']);
}
public function getOrderItems()
{
return $this->hasMany(Orderitem::className(),['Order_id' => 'ID' ])
->via('orders');
;
}
public function getProducts()
{
return $this->hasMany( Product::className() , ['ID' => 'Product_ID' ] )
->via('orderItems') ;
}
/**
* #inheritdoc
* #return CustomerQuery the active query used by this AR class.
*/
public static function find()
{
return new CustomerQuery(get_called_class());
}
/**
* #info: call : http://127.0.0.1:8080/api/web/customer?expand=orders for get customer and his orders also;
* #return type
*/
public function extraFields()
{
return ['orders','orderItems','products'];
}
}
How to include my .js files after javascript code generated by Datepicker widget in view file.
echo DatePicker::widget([
'name' => 'datepicker--2',
'id' => 'datepicker--2',
'clientOptions' => [
'showOtherMonths' => true,
'maxDate' => '+ 0d',
'showOtherMonths' => true,
'selectOtherMonths' => true,
]
]);
My asset bundle:
namespace app\assets;
use yii\web\AssetBundle;
class ChartsAsset extends AssetBundle
{
public $basePath = '#webroot';
public $baseUrl = '#web';
public $js = [
'js/charts.js',
'js/charts-init.js',
];
public $depends = [
'yii\web\YiiAsset',
'yii\jui\JuiAsset',
'yii\web\JqueryAsset',
];
}
What I'm getting on my page source:
...
<script src="/new/js/charts.js"></script>
<script src="/new/js/charts-init.js"></script>
<script type="text/javascript">jQuery(document).ready(function () {
$('#datepicker--2').datepicker($.extend({}, {"showOtherMonths":true,"maxDate":"+ 0d","selectOtherMonths":true,"dateFormat":"M d, yy"}));
});</script></body>
</html>
With yii2, you have options to define the position (HEAD, BEGIN OR END) of the client scripts with in the the document. This can be achieved by doing something like this
public $jsOptions = [
'position' => \yii\web\View::POS_HEAD
];
Or Using This.
Create BaseAssets like this:
namespace app\assets;
class BaseAsset extends AssetBundle
{
public $sourcePath = '#resources'; // #app/resources
public $css = [];
public $js = [];
public $depends = [
'yii\web\YiiAsset',
'yii\jui\JuiAsset',
'yii\web\JqueryAsset',
];
public $jsOptions = [ 'position' => \yii\web\View::POS_HEAD ];
}
And your Assets:
namespace app\assets;
use yii\web\AssetBundle;
class ChartsAsset extends AssetBundle
{
public $basePath = '#webroot';
public $baseUrl = '#web';
public $js = [
'js/charts.js',
'js/charts-init.js',
];
public $depends = [
'app\assets\BaseAssets',
];
public $jsOptions = [ 'position' => \yii\web\View::POS_END ];
}