错误:在laravel视图中未定义变量

xpcnnkqh  于 2023-11-20  发布在  其他
关注(0)|答案(4)|浏览(109)

我得到了这个错误:未定义的变量。我读了很多关于它的帖子,但是,没有一个对我面临的问题有帮助。(Why I get "Undefined variable" in Laravel view?
这是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>


这是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');


我看不出是什么原因导致了这个错误?
我使用'arrondissement'作为表'arrondissements'的外键

v2g6jxz6

v2g6jxz61#

返回视图时,还应该传递带有数据的变量:

$arrondissement = ....

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

字符串

8oomwypt

8oomwypt2#

我解决了这个问题。很简单,我必须删除感叹号。因为,我需要测试值是否为空。

y3bcpkx1

y3bcpkx13#

public function create(){
    $data = [];
    $kategoris = Kategori::orderBy('kategori', 'ASC')->get();
    $data['kategoris'] = $kategoris; 
    return view('backend.produk.create', $data);
}

个字符

ee7vknir

ee7vknir4#

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

字符串
您还应该将此行添加到store函数中

相关问题