I have 3 views (which display settings for each): Users, Groups, Options
Each of these views is successfully rendering, using the below. The controller passes the database info into each view.
#extends('master')
#section('main-title')
Title
#stop
#section('main-content')
// All the divs, content etc (working fine)
#stop
I also have one more view: Settings
The idea of this view is simple, to be an overview of all the settings from the Users, Groups and Options. So essentially I'm trying to pull together each of the 3 views 'main-content' output, and put it within the #section('main-content') within my Settings view. However I have no idea how.
The only option I can think of is to duplicate the content within the Settings view (index function) - however this will cause issues when I want to change something as I'll need to do it in two templates.
My controller:
public function index()
{
$users = User::all();
$options = Option::all();
$groups = Group::all();
return View::make('layouts.settings', array('users' => $users, 'options' => $options, 'groups' => $groups));
}
public function users()
{
$users = User::all();
return View::make('layouts.settings.users', array('users' => $users));
}
public function options()
{
$options = Option::all();
return View::make('layouts.settings.options', array('options' => $options));
}
public function groups()
{
$groups = Group::all();
return View::make('layouts.settings.groups', array('groups' => $groups));
}
Is there anyway, I can say within my Settings view: include the content within 'main-content' from the following views (Users, Groups, Options). Or, use nested view which I have tried but cannot get working.
Thanks in advance.
Here you go - may need fine-tuning: http://paste.laravel.com/xZr
Related
I have two entities in my database which are related by a one to many relationship: "User" and "Ad"
I have generated model classes using gii.
This is what I have in my model class for User:
public function getAds()
{
return $this->hasMany(Ad::className(), ['user' => 'id']);
}
and for my Ad model:
public function getUser0()
{
return $this->hasOne(User::className(), ['id' => 'user']);
}
according to Yii2 documentation, In the controller when I do
$ads = Ad::find()->all();
var_dump($ads[0]->user);
It should eagerly load user data from the DB but I only get the foreign key (1).
Even when I try
$ads = Ad::find()->with('user0')->all();
var_dump($ads[0]->user);
Its still the same.
thanks. If I want to send Ads and their related user data by xml in an ActiveController, do I have to do something like this:
$t = array();
foreach ($ads as $ad) {
$t[] = [$ad, $ad->user0];
}
return $t;
Or there is a more straightforward way to do that?
You are still getting Ad objects either with or without eager loading.
The difference is how the relations are populated, with lazy loading the relations are only loaded when they are accessed.
$ads = Ad::find()->all();
foreach ($ads as $ad) {
var_dump($ad->user0); // query to load user record here
}
With eager loading they are populated up front.
$ads = Ad::find()->with('user0')->all();
foreach ($ads as $ad) {
var_dump($ad->user0); // user0 already populated, no query
}
Probably You need joinWith
$ads = Ad::find()->joinWith('user0')->all();
I'm creating a RESTful API with Yii2 and have successfully setup a model named Contacts by following the Quick Start Tutorial*. I love how records can be created, listed, updated and deleted without creating any actions.
However I can't see how to filter results. I would like to only return contacts where contact.user_id is equal to 1 (for example) as it currently will reply with all records. Is this possible without creating the actions?
I am unsure also how I can limit results. From what I've read I feel it should append the URI with ?limit=5.
http://www.yiiframework.com/doc-2.0/guide-rest-quick-start.html
You should return a dataprovider instead of a set of objects, that supports pagination for you.
Perhaps this approach will be a bit more useful:
public function actionIndex()
{
return new \yii\data\ActiveDataProvider([
'query' => Contact::find()->where(['user_id' => \Yii::$app->user-id]),
]);
}
You could also leave the index action intact, but provide the preset action with a prepareDataProvider-callback:
public function actions()
{
$actions = parent::actions();
$actions['index']['prepareDataProvider'] = function($action)
{
return new \yii\data\ActiveDataProvider([
'query' => Contact::find()->where(['user_id' => \Yii::$app->user-id]),
]);
};
return $actions;
}
Hope that helps.
I have had to override the index method despite not wanting to. My solution looks like this:
public function actions()
{
$actions = parent::actions();
unset($actions['index']);
return $actions;
}
public function actionIndex()
{
return Contact::findAll(['user_id' => \Yii::$app()->user-id]);
}
I guess this solution means I need to write my own pagination code however which is something else I was hoping to avoid.
just started using Laravel but want to make sure I am using it correctly.
Most of my work is CMS based so read / write / update etc to a database.
An example of what I have done so far is an insertion into the DB:
On the view I have a form with a URL of 'addNewUser'.
In my routes I then do:
Route::post('addnewuser', array('uses' => 'UserController#addNewUser'));
My user controller 'addNewUser' method is (simplified):
public function addNewUser() {
$data = Input::all();
$rules = array(
'username' => 'required|alpha_dash|max:16|unique:users,username',
);
$validator = Validator::make($data, $rules, $messages);
if ($validator->fails())
{
Input::flash();
$errors = $validator->messages();
return Redirect::to('/register')->withErrors($validator)->withInput();
}
$user = new User;
$user->save();
return Redirect::to('/login')->with('successLogin', '1');
}
Is this correct? I have read somewhere that all DB interaction should be in the model?
Likewise when reading from the DB to display a foreach for example, I do the following directly in the view:
$builds = DB::table('blogs')->orderBy('id', 'desc')->get();
if ($builds) {
foreach ($builds as $build)
{
$safeURLSlug = stringHelpers::safeURLSlug($build->blogtitle);
echo "
// stuff
";
}
} else {
// no stuff
}
Should I be doing these sort of queries and showing of data directly in the view? or in a model / controller function etc?
Want to check im doing things 100% correct / the standard way of doing things before I get too involved.
I can see a few things that I personally would have done differently.
For example I usually put $rules as a class variable so it can be used in different functions related to your Users.
Have you tested your code yet? Any errors?
In your addNewUser function does it save any data? I know you have "simplified" above the code snippet but there should be $user->username = $data['username']; etc. in between creating your $user variable and running $user->save();, so if you excluded this on purpose then I don't see anything else with your model.
In your view code, $builds = DB::table('blogs')->orderBy('id', 'desc')->get(); should be done in your controller and passed to your view like so return View::make('example', array('builds' => $builds))
I'd also change
$builds = DB::table('blogs')->orderBy('id', 'desc')->get();
to
$builds = Blog::orderby('id','desc')->get(); if you have a Blog model, otherwise your code is fine.
You could move:
$rules = array(
'username' => 'required|alpha_dash|max:16|unique:users,username',
);
to User model as static variable, and instead of:
$validator = Validator::make($data, $rules, $messages);
you could use:
$validator = Validator::make($data, User::$rules, $messages);
But definitely you shouldn't get data from database in your View, this code should be in controller, for example:
$builds = DB::table('blogs')->orderBy('id', 'desc')->get();
return View::make('someview')->with('builds', $builds);
of course if you have Blog model, you should use here:
$builds = Blog::orderBy('id', 'desc')->get();
return View::make('someview')->with('builds', $builds);
It's also unclear what the following code does:
$safeURLSlug = stringHelpers::safeURLSlug($build->blogtitle);
but probably you could move it to your Blog model and use accessor to make the change:
public function getSafeSlugAttribute($value) {
return stringHelpers::safeURLSlug($this->blogtitle);
}
and now your view could look like this:
#foreach ($builds as $build)
{{{ $build->title }}} {{{ $build->safeSlug }}}
#endforeach
I suggest you take a look on Laravel Generators.
https://github.com/JeffreyWay/Laravel-4-Generators
Install and then run:
php artisan generate:scaffold customer
Laravel line command generator create a basic CRUD for you with controller, model, views and database migrations. That's good to safe time and keep your project with some default organization.
Hi I'm trying to make an API to send data encapsulating with json.
as cakephp manual said, I added extensions in routes.php
$routes->extensions(['json]);
and I've made an index function in controller.
public function index(){
$item = $this->Items->find('all');
$this->set(['items' => $items, '_serialize' => ['items']]);
}
here are the problem.
what should i do after this to make api encapsulating with json??
Please help.
thank you
According to Cake 2.x Book (http://book.cakephp.org/2.0/en/development/rest.html)
You have to add this to your routes.php file:
Router::mapResources('items');
Router::parseExtensions();
Then, in your items controller, add the RequestHandler to your components array:
public $components = array('RequestHandler');
Then, in your items controller, add your methods, in your example:
public function index() {
$recipes = $this->Items->find('all');
$this->set(array(
'items' => $items,
'_serialize' => array('items')
));
}
Note: according to model names convention you should call $this->Item instead of $this->Items unless you previously defined the model name as "Item" (singular) in your item model file.
Finally, the API is done, you can access to yourprojecturl/items.json and see the json result.
I had trouble with RicardoCamacho's code until I used the $recipes, which is $this->Items->find('all'); in the $this->set(array...
public function index() {
$recipes = $this->Items->find('all');
$this->set(array(
'items' => $recipes,
'_serialize' => array('items')
));
}
Our Yii Framework application has the following defined as part of the UserProfileImages model:
public function getProfileImages($param, $user_id) {
if(isset($param['select']) && $param['select']=='all'){
$profile_images = UserProfileImages::model()->findAllByAttributes( array( 'user_id'=>$user_id) );
} else {
$profile_images = UserProfileImages::model()->findByAttributes( array( 'user_id'=>$user_id) );
}
return $profile_images;
}
How would I wire up the above snippet to a widget in my view to return all the images for a given user?
Bonus Question: Which image rotator do you suggest to render the above?
In your view file, add something like this, assuming that your controller specified $user_id:
$this->widget('UserProfileImagesWidget', array(
"userProfileImages" => UserProfileImages::getProfileImages(
array("select" => "all"),
$user_id
),
"user_id" => $user_id
));
Depending on your MVC philosophy, you could also retrieve the userProfileImages data in the controller and pass that data to your view.
Define a widget like this:
class UserProfileImagesWidget extends CWidget {
public $user_id;
public $userProfileImages = array();
public function run() {
$this->render("userProfileImage");
}
}
Finally, in the userProfileImages.php view file, you can do something like this:
if(!empty($this->userProfileImages)) {
// Your display magic
// You can access $this->user_id
}
As a side note: You might want to change the order of your parameters in getProfileImages. If $user_id is the first parameter, you can leave out $params completely in case you don't want to specify any.