3

I am getting this error: undefined variable. I read a lot of posts about it but, none of them helped with the problem i am facing. (Why I get "Undefined variable" in Laravel view? )

This is Project_Controller :

class Project_Controller extends Controller
{

public function create()
{
$arrondissement = Arrondissements::pluck('arrondissement', 'id');

return view::make('projets.create', compact('arrondissement'));
}

public function store(Request $request)
{

    $validator = Validator::make($request->all(), [
        'intitule' => 'required|max:255',
        'code' => 'required|max:255',
        'dateDebut' => 'required|max:255',
        'dateFin' => 'required|max:255',
        'estimation' => 'required|max:255',
        'arrondissement' => $request->arrondissement,
    ]);     
if ($validator->fails()) {
    return back()
        ->withInput()
        ->with(['arrondissement'=>$arrondissement])
        ->withErrors($validator);
}               
    $projet = new Projet;

    $projet->intitule = $request->intitule;
    $projet->code = $request->code;
    $projet->dateDebut = $request->dateDebut;
    $projet->dateFin = $request->dateFin;
    $projet->estimation = $request->estimation;
    $projet->arrondissement = $request->arrondissement;


    $projet->save();

     return view('/submit', compact('arrondissement'));     
}
}

submit.blade.php :

    <select name="arrondissement_id">
        @if (!empty($arrondissement))                   
            Whoops! Something went wrong                
        @else
            @foreach($arrondissement as $id => $arrondissement)
                <option value="{{$id}}">{{$arrondissement}}</option>                    
            @endforeach
        @endif
</select>

and this is routes.php :

Auth::routes();

Route::get('/home', 'HomeController@index');

Route::get('/', function () {
   $projets = \App\Projet::all();
   return view('welcome', compact('projets'));
});

Route::get('/submit', function () {
return view('submit');
});

Route::post('submit/projects', 'Project_Controller@store');

I can't see what's causing this error ??

I am using 'arrondissement' as a foreign key of table 'arrondissements'

1
  • 1
    Your Project_Controller (as posted) is broken php. You start method store() before ever ending the method create() Commented Jan 23, 2017 at 15:14

4 Answers 4

2

When returning the view, you should also pass the variable with data:

$arrondissement = ....

return view('/submit', compact('arrondissement'));
Sign up to request clarification or add additional context in comments.

5 Comments

I already did in 'create' method...I tried it, but still not working
@Naj you also loading the view in the store method and in a closure in routes file.
I don't get it, I shouldn't load the view in the store ?
@Naj you can redirect user, like return redirect()->back(). But if you've loading a view which works with some variables, these variables shouldn't be empty. Or, you could just check if variable is empty, like @if (!empty($arrondissement)) do some stuff with data @endif
Please chek the post I edited it, I addedd @if and else, and I also insert some data into "arrondissements" table in database
0

I solved the problem. It's simple, I had to remove exclamation mark. Because, I need to test if the value is empty not unempty.

Comments

0
public function create(){
    $data = [];
    $kategoris = Kategori::orderBy('kategori', 'ASC')->get();
    $data['kategoris'] = $kategoris; 
    return view('backend.produk.create', $data);
}
@if (count($kategoris) > 0)
    @foreach($kategoris as $kategori)
        <option value="{{ $kategori->id }}">{{ $kategori->kategori }}</option>
    @endforeach
@endif

Comments

-1
$arrondissement = Arrondissements::pluck('arrondissement', 'id');

You should also add this line into the store function

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.