1
1
1programmer2018-04-22 15:30:51
Laravel
1programmer, 2018-04-22 15:30:51

How to properly organize data output in laravel view?

Hi all.
There are 2 buttons and routes for them.
1) /add-code
2) /add-item
After clicking on the first button, I return to the /root page
After clicking on the second button, I also return to the /root
page On the /root page there are 2 standard bootstrap tabs, in the first view it is displayed variable {{ $msg }}

return view('root')->with(['msg' => '10 кодов добавлено в базу']);

After clicking on the 2nd button, I accordingly received an error message
Undefined variable: msg
because I also need to return it like this
return view('root')->with(['msg' => '10 codes added to the database']) ;
But this variable does not apply to the second button at all, it turns out some kind of shit code.
I'm just learning Laravel, can you explain how to do it better?
All code
<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Codes;
use App\Item;
class RootHomeController extends Controller
{
    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        $this->middleware('auth:root');
    }

    /**
     * Show the application dashboard.
     *
     * @return \Illuminate\Http\Response
     */
      
   

    public function addCode(Request $request)
    {
        


        if($request->isMethod('post')) {

            $arrayName = array();
            $i = 0;
               while ($i < 1) {
                  $chars="qazxswedcvfrtgbnhyujmkiolp1234567890QAZXSWEDCVFRTGBNHYUJMKIOLP"; 
                    $max=10; 
                    $size=StrLen($chars)-1; 
                    $password=null; 
                    while($max--) 
                    $password.=$chars[rand(0,$size)]; 
                    $arrayName[$i] = $password;

                        //Добавляем данные в бд

                    Codes::insert(['code' => $arrayName[$i]]);
                    $i++;


               }
               
                   

            return view('root')->with(['msg' => '10 кодов добавлено в базу']);
        }
    }

    public function addItem(Request $request)
    {
        if ($request->isMethod('POST')){
            Item::create([
                'name' => $request->name,
                'description' => $request->desc,
            ]);
               return view('root'); // здесь ошибка
            //нужно потому что вот типа того - > return view('root')->with(['msg' => '10 кодов добавлено в базу']);
        }
    }
public function index()
    {
        // $add = addCode();
        return view('root')->with(['msg' => 'После нажатия кнопки, будет добавлено 10 кодов']);
    }
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
Y
Yan-s, 2018-04-22
@1programmer

There are 2 standard bootstrap tabs on the /root page, in the first view the variable {{ $msg }} is displayed

Add a condition to this view, check for the existence of the $msg variable, and print it only if it exists.
Type:
@if(isset($msg ))
{{ $msg }}
@endif

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question