I've got three tables
checkoutcounter {id, name}
user {id, username}
checkoutcounter_users { id, checkoutcounter_id, user_id}
I use gii and then add
in checkoutcounter model (I add and joinWith and find()->with but it still doesn't work):
public function getUser_display()
{
return $this->hasMany(User_display::className(), ['id' => 'user_id'])
->viaTable(CheckoutcounterUsers::tableName(), ['checkoutcounter_id' => 'id']
);
}
In checkoutcounter model search:
public function search($params)
{
$query = FinanceSettingsCheckoutcounter::find()->with('user_display');
$query->joinWith('user_display');
}
what should I add in checkoutcounter view to get usernames or user id's? Why when I add in gridview 'attribute'=>'user_display.id' it doesn't display any data?
echo yii\helpers\Json::encode($dataProvidercheckoutcounter);
shows
{"query":{"sql":null,"on":null,"joinWith":[[["user_display"],true,"LEFT JOIN"]],"select":null,"selectOption":null,"distinct":null,"from":null,"groupBy":null,"join":null,"having":null,"union":null,"params":[],"where":null,"limit":null,"offset":null,"orderBy":null,"indexBy":null,"modelClass":"app\\models\\FinanceSettingsCheckoutcounter","with":["user_display"],"asArray":null,"multiple":null,"primaryModel":null,"link":null,"via":null,"inverseOf":null},"key":null,"db":null,"id":null}
Im not sure how you're using your search() function, but you're not using $params.. And its not returning the results..
I belive the query should be like this:
public function search($params)
{
$result = FinanceSettingsCheckoutcounter::find()->with('user_display')->all();
echo yii\helpers\Json::encode($result);
}
if you are using this as part of a Search Model, returning a dataProvider, check out this link
http://www.ramirezcobos.com/2014/04/16/displaying-sorting-and-filtering-model-relations-on-a-gridview-yii2/
Related
I cant display my data, laravel's 404 page popping.
Here is my foreach codes.
#foreach($este as $row)
{{$row['価格']}}
{{$row['間取り']}}
{{$row['販売戸数']}}
{{$row['総戸数']}}
{{$row['専有面積']}}
{{$row['専有面積']}}
{{$row['その他面積']}}
{{$row['所在階/構造 階建']}}
{{$row['完成時期']}}
{{$row['住所']}}
#endforeach
What am I doing wrong here.
I want to edit table with css but data doesn't display.
My controller, and route is here:
public function sumos($id)
{
$este = estates::all();
return view('pages.sumo', ['este' => $este]);
}
Route::get("sumo/{id}", "PagesController#sumos");
Change your controller and route to the following
public function sumos()
{
$data['este'] = estates::all();
return view('pages.sumo', $data);
}
Route::get("sumo", "PagesController#sumos");
then your view to
#foreach($este as $row)
{{$row->価格}}
{{$row->間取り}}
{{$row->販売戸数}}
{{$row->総戸数}}
{{$row->専有面積}}
{{$row->専有面積}}
{{$row->その他面積}}
{{$row->所在階/構造 階建}}
{{$row->完成時期}}
{{$row->住所}}
#endforeach
Don't return the value from the controller . Instead send the values to the view as parameter and then use it in your view
Like
public function sumos()
{
$este = estates::get();
return view('viewName', ['este' => $este]);
}
And in your view
#foreach($este as $row)
write markup for your items here.
#endforeach
1.Actually get is for retrieve all the data from table ,
2.After getting related data pass it on view
public function sumos()
{
$este = estates::get();
return view("editPage" , compact('este'));
}
After View data in table using
#foreach($este as $est) //data here like html css #endforeach
add route to update data and pass it into controller
Then find related row using `
public function find($id){
$estates = estates::find($id);
//after changes
$estates->save();
return redirect()->back();
}
`
firs you have to create a view file
How to use views in Laravel?
then just use this construction:
public function sumos()
{
$este = estates::all();
return view('name_of_your_view_file', compact('este');
}
change your route to
Route::get("sumo/{id}", "PagesController#sumos");
I have been struggling with this for quite a while. I use PyroCMS and it has a Posts module that has all the fields in the database and all that and if you want to find a specific post, you can just use a normal WHERE clause and find a post by a date and so on.
But if a field is checked in CMS as translatable, I can't access that field and use it to find a post, because the CMS creates another field in another table that is called posts_translations, and it contains all the fields that are translatable. Usually that is a simple $posts->where("field","value"), but the field doesn't exist if it's translatable.
So I tried to use whereHas, but it doesn't really return anything.
public function meklet(PostRepositoryInterface $posts, $q)
{
$postss = $posts->all()->whereHas('translations', function($query) use($q) {
$query = $query->where(function($query) use($q) {
$query->where('title', 'like', '%'.$q.'%');
});
});
die(var_dump($q));
return $this->view->make("mendo.module.report::reports/search");
}
As you can see I use PostRepositoryInterface maybe I need to use some other class to access what I want? Im very confused, I know its a laravel base, but I can't really wrap my head around this simple problem.
You shouldn't use one letter variables and too much nested functions there:
/**
* Searches for all matches.
*
* #param PostRepositoryInterface $posts The posts
* #param string $search The search
* #return View
*/
public function search(PostRepositoryInterface $posts, $search)
{
/* #var PostCollection $results */
$results = $posts->all()->filter(
function (PostInterface $post) use ($search) {
return str_contains(
strtolower($post->getFieldValue('title')),
strtolower($search)
);
}
);
dd($results);
return $this->view->make('mendo.module.report::reports/search', [
'posts' => $results,
]);
}
And route should be like:
'posts/search/{search}' => [
'as' => 'anomaly.module.posts::posts.search',
'uses' => 'Anomaly\PostsModule\Http\Controller\PostsController#search',
],
To use a DB query directly you need to write translations join self. It is not so difficult.
in my Laravel App I have two tables:
Projects
- id
- user_id
- name
- etc...
Images
- id
- project_id
- url
How can I achieve it, to show all the Projects each user has and all the connected images (each project can have up to 20 images (stored in FTP) and Link in Field URL - the project_id from table "Projects" will be saved in field images.project_id)?
I learned, that I can show the projects like this:
$projects = DB::table('projects')->where('user_id','=',$user->id)->get();
and I tried with
$images = DB::table('images')->where('project_id','=',$projects->id)->get();
But I get an error message:
Property [id] does not exist on this collection instance.
What I am missing? Thank you for pointing me into the correct direction :-)
Kind Regards,
Stefan
For your question i suggest to use eloquent way like set up your models
class Project extends Model
{
public function images()
{
return $this->hasMany(\App\Models\Image::class, 'project_id');
}
public function user()
{
return $this->belongsTo(\App\Models\User::class, 'user_id');
}
}
class Image extends Model
{
public function project()
{
return $this->belongsTo(\App\Models\Project::class, 'project_id');
}
}
Now to find projects with their images you can query as
$projects = Project::with('images')->get();
Each object in $projects will have collection of their associated images.
To add filter for user you can use whereHas on relations
$projects = Project::with('images')
->whereHas('user', function ($query) use ($user) {
$query->where('id', '=', $user->id);
})->get();
I want to receive data like this:
categories
----category1
----category2
topProducts
----product1
--------photo1
--------photo2
----product2
--------photo1
--------photo2
I need get all categories and top x products.
Each product has two photos.
How can i do this by using yii2 restful?
Thanks.
the query shold look something like this
Category::find()
->with(['subcategories','topProducts', 'topProducts.images'])
->all();
you can use joinWith if you absolutely want a single query
if you retrieve your data with an ActiveController, you need to specify extraFields to the Category model. (here's a rest-specific usage example - rest of the guide should prove usefull as well)
Category model:
public function extraFields() {
return ['subcategories', 'topProducts'];
}
// product relation
public function getTopProducts(){
return $this->hasMany(Product::className(), ['category_id' => 'id'])
// ->order()->where() // your criterias
->limit(10);
}
// subcategories
public function getChildren(){
return $this->hasMany(Category::className(), ['id' => 'parent_id']);
}
Product model:
public function extraFields() {
return ['iamges'];
}
public function getImages(){
return $this->hasMany(Image::className(), ['product_id' => 'id'])
}
ps. since you haven't posed any code or table structure, all relations in my example are based on standard naiming convention
I'm using Cake's find('all') method in the controller to query a table I have, and the requestAction method to use the controller function (and so to output the data) within the view. The problem is that the find method is returning empty arrays(ouput is 'ArrayArrayArray'). The table is named 'users' (as per Cake conventions) and the code for all three files are below:
Code for the view (test.ctp)
<?php
$users = $this->requestAction('/users/read');
foreach($users as $user) {
echo $user;
}
?>
Code for the controller (users_controller.php)
<?php
class UsersController extends AppController {
var $name = 'Users';
function read(){
$users = $this->User->find('all');
if(isset($this->params['requested'])) {
return $users;
}
}
}
?>
Code for the model (user.php)
<?php
class User extends AppModel {
var $name = 'User';
}
?>
I've dwindled it down and discovered that $users is being set to NULL by the $this->User->find('all') statement because that statement is actually not returning any values. I know Cake is finding the table because of a typo I had earlier with the table name ('usres' instead of 'users'). I can't figure out what I'm missing or what I'm doing wrong. Would love some help. :)
The ouput is 'ArrayArrayArray' doen't means itz empty
try
foreach($users as $user) {
debug($user);
}
For me this code is meaningless.
if you want to use requestAction() use it in the proper way. It's used to call (and print) the parts of the application in the view.
Here is how to achieve the working and correct code:
Your Users class:
class Users extends AppController {
function read(){
$this->set('users', $this->User->find('all'));
$this->layout = "ajax";
$this->render();
}
}
Your read.ctp:
<ul><?php
if(isset($users)){
foreach($users as $user) {
echo '<li>'.$user.'</li>';
}
}
?></ul>
Now in your test.ctp you need to call:
<?php echo $this->requestAction(
array(
'controller'=>'users',
'action'=>'read'
)
);?>
And you will see the result in your test action as unsorted list.