gpt4 book ai didi

laravel - 错误 : undefined variable in laravel view

转载 作者:行者123 更新时间:2023-12-02 03:07:28 25 4
gpt4 key购买 nike

我收到这个错误: undefined variable 。我读了很多关于它的帖子,但是没有一个能帮助解决我面临的问题。 ( Why I get "Undefined variable" in Laravel view? )

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

提交.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”的外键

最佳答案

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

$arrondissement = ....

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

关于laravel - 错误 : undefined variable in laravel view,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41809412/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com