Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
456 views
in Technique[技术] by (71.8m points)

laravel - (laravel8) ErrorException Undefined variable $eshop (View: /opt/lampp/htdocs/alleshops/resources/views/edit_eshop.blade.php)

Route::middleware(['auth:sanctum', 'verified'])->any('/edit_eshop/{eshop}', function () {
    return view('edit_eshop');
})->name('eshop.edit');

my controller:

public function edit_eshop(Eshop $eshop,Request $request){
        if ($request->method() == 'POST'){            
            
            $eshop->title = $request->get('title');
            $eshop->link = $request->get('link');
            $eshop->telephone = $request->get('telephone');
            $eshop->email = $request->get('email');
            $eshop->payathome = $request->get('payathome');
            $eshop->paywithcard = $request->get('paywithcard');
            $eshop->tags = $request->get('tags');
            if($eshop->save()){
                echo "Το eshop δημιουργ?θηκε επιτυχ??.";
                return redirect('/eshops');
            };
        };        
        return view('edit_eshop', ['eshop' => $eshop]);
    }
<form action="" method="POST">
     @csrf
     <input type="text" name="title" placeholder="Ονομασ?α eshop" class="form-control" value={{ $eshop->title }}>
     <input type="text" name="link" placeholder="Ηλεκτρονικ? διε?θυνση eshop" class="form-control"  value={{ $eshop->link }}>
...
</form>

...

It suggests me :

$eshop is undefined Make the variable optional in the blade template. Replace {{ $eshop }} with {{ $eshop ?? '' }}

I 've already tried this , but keep getting empty fields in the form.

question from:https://stackoverflow.com/questions/65713757/laravel8-errorexception-undefined-variable-eshop-view-opt-lampp-htdocs-all

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You getting this error, because your controller method never called from your route. Change your route :

Route::middleware(['auth:sanctum', 'verified'])
  ->any('/edit_eshop/{eshop}', 'AppHttpControllersYourController@edit_eshop')
  ->name('eshop.edit');

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...