How to get rid of errors foreach?

I have a problem in the template main.php I have a problem and displays this record Invalid argument supplied for foreach(). Just don't understand why foreach is invalid. Most likely, as I understand it, I am using in main. But how to implement a system of notification then the question stands. He never did these moments and can't understand how to do it.
<?php NavBar::begin([ 'brandLabel' =--> 'Holland', 'brandUrl' => ['/zakaz/index'], 'options' => [ 'class' => 'navbar-inverse navbar-fixed-top', ], ]); if (!Yii::$app->user->isGuest) { $menuItems[] = ['encode' => false, 'label' => '<span class="glyphicon glyphicon-bell"></span>', 'options' => ['id' => 'notification']]; } if (Yii::$app->user->isGuest) { // $menuItems[] = ['label' => 'Signup', 'url' => ['/site/signup']]; $menuItems[] = ['label' => 'Login', 'url' => ['/site/login']]; } else { $menuItems[] = '<li>' . Html::beginForm(['/site/logout'], 'post') . Html::submitButton( 'Log out (' . Yii::$app->user->identity->username . ')', ['class' => 'btn btn-link logout'] ) . Html::endForm() . '</li>'; } echo Nav::widget([ 'options' => ['class' => 'navbar-nav navbar-right'], 'items' => $menuItems, ]); NavBar::end(); ?> <div class="notification-container"> <?php foreach ($notification as $notifications): ?> <?php echo $notifications--->name; ?> <?php endforeach ?> <?php Html::a('Read the reminder', ['#']) ?> </div>
June 26th 19 at 14:02
1 answer
June 26th 19 at 14:04
Solution
In foreach you pass the variable $notification, and where it provisioning? This variable should contain the array of objects. And comes, apparently, is null.
Yes I have it in the controller brought, as you know in view it is necessary to take the data as you know? - Waylon_Kub commented on June 26th 19 at 14:07
Throw the controller code and it will be clear. And Yes, in the controller you use a method render() or renderPartial() which first parameter indicative view, and the second is a named array of variables for the view.

Check that you transfer the correct first argument to foreach. It needs to be an array - Khalid_Yost commented on June 26th 19 at 14:10
: As I understand the whole not necessary, and only action, or no need all. Just easier to give a guide

use app\models\Notification;
class Controller extends ZakazController
{
public function actionView($id)
{
 $model = $this->findModel($id);
 $notification = new Notification();
 $shipping = $model->idShipping;
 $statusDisain = $model->statusDisain;
 $status = $model->status;

 $shipping = new Courier();
 if ($shipping->load(Yii::$app->request->post()) && $shipping->save()) {
 $model->id_shipping = $shipping->id;//Clearance delivery

 $notification->id_user = 7;//making the notification
 $notification->name = 'shipping '.$model->description;
 $notification->active = 0;

$notification->save();
$model->save();
 return $this->redirect(['view', 'id' => $model->id_zakaz]);
}

 if ($model->load(Yii::$app->request->post())) {
 $model->file = UploadedFile::getInstance($model, 'file');//work Done by the designer
if(isset($model->file))
{
$model->file->saveAs('maket/Maket_'.$model->id_zakaz.'.'.$model->file->extension);
 $model->maket = 'Maket_'.$model->id_zakaz.'.'.$model->file->extension;
 $model->status = 4;
 $notification->id_user = 5;//making the notification
 $notification->name = 'Designer completed the work number'.$model->id_zakaz.' '.$model->description;
 $notification->active = 0;
 } 
$model->save();
 if ($model->status == 3) {
 $notification->id_user = 3;//making a notification to the designer
 $notification->name = 'New order#'. $model->id_zakaz;
 $notification->active = 0;
$notification->save();
 } elseif ($model->status == 6) {
 $notification->id_user = 6;//making a notification to the master
 $notification->name = 'New order#'. $model->id_zakaz;
 $notification->active = 0;
$notification->save();
}

 return $this->redirect(['view', 'id' => $model->id_zakaz]);
}

 return $this->render('view', [
 'model' => $this->findModel($id),
 'notification' => $notification
 'user_name' => $user_name,
 'shipping' => $shipping,
 'file' => $file,
]);
}
}
- Waylon_Kub commented on June 26th 19 at 14:13
Clear. You pass a variable to a normal view file. And catch it want to layot template main.php

It is not available in main.php in order that it had been available there, you can create a public property on the class ZakazController, for example:

use app\models\Notification;
class Controller extends ZakazController
{

public function actionView($id)
{
 $model = $this->findModel($id);
 $notification = new Notification();
 $shipping = $model->idShipping;
 $statusDisain = $model->statusDisain;
 $status = $model->status;

 $shipping = new Courier();
 if ($shipping->load(Yii::$app->request->post()) && $shipping->save()) {
 $model->id_shipping = $shipping->id;//Clearance delivery

 $notification->id_user = 7;//making the notification
 $notification->name = 'shipping '.$model->description;
 $notification->active = 0;

$notification->save();
$model->save();
 return $this->redirect(['view', 'id' => $model->id_zakaz]);
}

 if ($model->load(Yii::$app->request->post())) {
 $model->file = UploadedFile::getInstance($model, 'file');//work Done by the designer
if(isset($model->file))
{
$model->file->saveAs('maket/Maket_'.$model->id_zakaz.'.'.$model->file->extension);
 $model->maket = 'Maket_'.$model->id_zakaz.'.'.$model->file->extension;
 $model->status = 4;
 $notification->id_user = 5;//making the notification
 $notification->name = 'Designer completed the work number'.$model->id_zakaz.' '.$model->description;
 $notification->active = 0;
 } 
$model->save();
 if ($model->status == 3) {
 $notification->id_user = 3;//making a notification to the designer
 $notification->name = 'New order#'. $model->id_zakaz;
 $notification->active = 0;
$notification->save();
 } elseif ($model->status == 6) {
 $notification->id_user = 6;//making a notification to the master
 $notification->name = 'New order#'. $model->id_zakaz;
 $notification->active = 0;
$notification->save();
}

 return $this->redirect(['view', 'id' => $model->id_zakaz]);
}

 // Here looking for those notifications we need and assign them to the parameter view
 $this->view->params['notification'] = Notification::find()->all(); 

 return $this->render('view', [
 'model' => $this->findModel($id),
 'user_name' => $user_name,
 'shipping' => $shipping,
 'file' => $file,
]);
}
}


Then assign it the desired value and it will be available in main.php like $this->notification. Just note that your $notification will only be visible when you use ZakazController. Corrected the answer now, the assignment occurs before rendering. - Khalid_Yost commented on June 26th 19 at 14:16
So thick and controllers - not ice. Move business logic into the model and make the controller thinner - Khalid_Yost commented on June 26th 19 at 14:19
: Well I have toned it down a bit where it sets the status and conservation. Or even to make a description and an indication of which user the notification belongs to. But out there in the description still need to connect a different model
public function actionView($id)
{
 $model = $this->findModel($id);
 $notification = new Notification();
 $shipping = $model->idShipping;
 // $this->view->params['notification'] = $notification;

 $shipping = new Courier();
 if ($shipping->load(Yii::$app->request->post()) && $shipping->save()) {
 $model->id_shipping = $shipping->id;//Clearance delivery

 $notification->id_user = 7;//making a notification of delivery
 $notification->name = 'shipping '.$model->description;
$notification->saveNotification;
$model->save();
 return $this->redirect(['view', 'id' => $model->id_zakaz]);
}

 if ($model->load(Yii::$app->request->post())) {
 $model->file = UploadedFile::getInstance($model, 'file');//work Done by the designer
if(isset($model->file))
{
$model->file->saveAs('maket/Maket_'.$model->id_zakaz.'.'.$model->file->extension);
 $model->maket = 'Maket_'.$model->id_zakaz.'.'.$model->file->extension;
 $model->status = 4;
 $notification->id_user = 5;//registration of the notice the implementation of the designer's work
 $notification->name = 'Designer completed the work number'.$model->id_zakaz.' '.$model->description;
$notification->saveNotification;
 } 
$model->save();
 if ($model->status == 3) {
 $notification->id_user = 3;//making a notification to the designer
 $notification->name = 'New order#'. $model->id_zakaz;
$notification->saveNotification;
 } elseif ($model->status == 6) {
 $notification->id_user = 6;//making a notification to the master
 $notification->name = 'New order#'. $model->id_zakaz;
$notification->saveNotification;
}

 return $this->redirect(['view', 'id' => $model->id_zakaz]);
}

 return $this->render('view', [
 'model' => $this->findModel($id),
 'notification' => $notification
 'user_name' => $user_name,
 'shipping' => $shipping,
 'file' => $file,
]);
 }
- Waylon_Kub commented on June 26th 19 at 14:22
It is possible to reduce. And make the save function in the notification model Notification. For example:

public function actionView($id)
{
 $model = $this->findModel($id);
 $notification = new Notification();
 $shipping = $model->idShipping; // Here assign to $id shipping
 $shipping = new Courier(); // And here from override $shipping, that is the above line not needed
 if ($shipping->load(Yii::$app->request->post()) && $shipping->save()) {
 $model->id_shipping = $shipping->id;//Clearance delivery

 // In the model Notification write a function saveByIdUser($id) that will keep
 // notification depending on the id, that would not write the business logic in the controller
$notification->saveByIdUser(7);

$model->save();
 return $this->redirect(['view', 'id' => $model->id_zakaz]);
}

 if ($model->load(Yii::$app->request->post())) {

 // To download files, it is better to write a separate method in the model such as uploadFile()
 // to check the size of the file, a directory, generate a random name, etc.
 // to avoid problems in the future.

 /* Then it can be removed from the controller
 $model->file = UploadedFile::getInstance($model, 'file');//work Done by the designer
if(isset($model->file)){
$model->file->saveAs('maket/Maket_'.$model->id_zakaz.'.'.$model->file->extension);
 $model->maket = 'Maket_'.$model->id_zakaz.'.'.$model->file->extension;
 $model->status = 4;
$notification->saveByIdUser(5);
 } */ 


$model->uploadFile();
$model->save();

 if ($model->status == 3 || $model->status == 6)
$notification->saveByIdUser($model->status)

 return $this->redirect(['view', 'id' => $model->id_zakaz]);
}

 return $this->render('view', [
 'model' => $this->findModel($id),
 'notification' => $notification
 'user_name' => $user_name,
 'shipping' => $shipping,
 'file' => $file,
]);
 }


And you need to check carefully the contents of this controller. Because there's quite a lot of controversial issues, but to describe all there is no time. In the end, your controller will look something like this

public function actionView($id)
{
 $model = $this->findModel($id);
 $notification = new Notification();
 $shipping = new Courier(); 

 if ($shipping->load(Yii::$app->request->post()) && $shipping->save()) {
 $model->id_shipping = $shipping->id;
$model->save();
$notification->saveByIdUser(7);

 return $this->redirect(['view', 'id' => $model->id_zakaz]);
}

 if ($model->load(Yii::$app->request->post())) {
$model->uploadFile()->save();

 if ($model->status == 3 || $model->status == 6)
$notification->saveByIdUser($model->status)

 return $this->redirect(['view', 'id' => $model->id_zakaz]);
}

 return $this->render('view', [
 'model' => $this->findModel($id),
 'notification' => $notification
 'user_name' => $user_name,
 'shipping' => $shipping,
 'file' => $file,
]);
 }


Still need to add validation when saving models and validation. - Khalid_Yost commented on June 26th 19 at 14:25
And so definitely will work?
$notification->saveByIdUser(7);
I just wrote Calling unknown method: app\models\Notification::saveByIdUser()
And the second point just on the main issue. How to pass an array in foreach - Waylon_Kub commented on June 26th 19 at 14:28
Need to write a method saveByuserId() yourself. You don't have one.

The array is passed, so in the controller:
public function actionView($id)
{
 $model = $this->findModel($id);
 $notification = new Notification();
 $shipping = new Courier(); 

 if ($shipping->load(Yii::$app->request->post()) && $shipping->save()) {
 $model->id_shipping = $shipping->id;
$model->save();
$notification->saveByIdUser(7);

 return $this->redirect(['view', 'id' => $model->id_zakaz]);
}

 if ($model->load(Yii::$app->request->post())) {
$model->uploadFile()->save();

 if ($model->status == 3 || $model->status == 6)
$notification->saveByIdUser($model->status)

 return $this->redirect(['view', 'id' => $model->id_zakaz]);
}

 // Create the notifications option in the model view, where we will store the array
 // And pass back an array of Notification objects
 $this->view->params['notifications'] = Notification::find()->all();

 return $this->render('view', [
 'model' => $this->findModel($id),
 'user_name' => $user_name,
 'shipping' => $shipping,
 'file' => $file,
]);
 }


In the template main.php:
// Your template code
// ...
//

foreach($this->params['notifications'] as $notification){

 // Action model instance Notification, for example
 echo $notification->name;
}
- Khalid_Yost commented on June 26th 19 at 14:31
: I somehow again invalid again. Probably with $this as you know. Or not?
More precisely, when vyvode this design of the array Getting unknown property: yii\web\View::view - Waylon_Kub commented on June 26th 19 at 14:34
All caught up, I'm on another output. Thanks for the help - Waylon_Kub commented on June 26th 19 at 14:37
Not at all! A good coding - Khalid_Yost commented on June 26th 19 at 14:40

Find more questions by tags Yii