Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

johnathan2020's avatar

The page repeat itself many time after Edit, Create or Delete data in Modal pop-up

I working with a laravel project CRUD. I build a View with CRUD Modal so user can update, create...data directly to the View. But the matter is after I update, create, delete...the page just refresh itself with a success message. Then when I click on turn back icon in the top-left conner in Chrome. I can't go to HOME Page but I see that View page again and it show the success message again, I have to click to the back icon again to go back to Home page. Very weird! I will draw the flow for my situation

  1. This is when I don't using any create, update... Home Page <-----Back--------- View It take me just one click to go back to Home Page

  2. This is when I using create, update.... Frist time to vistit view page: Home Page ----go to----> View (edit some data) ------>Page refresh

After page refresh with the data just updated: Home Page <-----back----- View 1 <----back--- View 2 <----- Page refresh.

It feel like the more action I take the ore View it created and more times it get me to click back button to get to Home Page I think this is because my return redirect()->back(); in controller. But i not really sure, so if anyone really know the answer please help me. Thanks you

class DayTroController extends Controller { // public function __construct() { // header("Cache-Control: no-cache, no-store, must-revalidate"); // HTTP 1.1. // header("Pragma: no-cache"); // HTTP 1.0. // header("Expires: 0"); // Proxies. // header('Access-Control-Allow-Origin: *');
// }

// Hiện thị dãy trọ
public function daytro()
{
    // Lấy data ra ngoài để in ra màn hình

    $daytros = DayTro::paginate(5);
    return view('pages.daytro', compact('daytros'));       
}

// Đưa thông tin dãy trọ vào DB 
public function store(Request $request)
{
    // Kiểm tra thông tin đầu vào
    $request->validate([
        'tendaytro' => 'required|string|max:255|unique:daytro',
        'tinh' => 'required|string|max:255',
        'huyen' => 'required|string|max:255',
        'xa' => 'required|string|max:255',
        'sonha' => 'required|string|max:255',
    ],[
        'tendaytro.unique' => 'Tên dãy trọ đã tồn tại',
    ]);
    // Lấy id từ session => Đang lưu trong session id = 10 
    $chutro_id = session('chutro_id'); // Lấy chutro_id từ session

    // Tạo và lưu vào DB 
    $daytro = daytro::create([
        'chutro_id' => $chutro_id,
        'tendaytro' => $request->tendaytro,
        'tinh' => $request->tinh,
        'huyen' => $request->huyen,
        'xa' => $request->xa,
        'sonha' => $request->sonha,
    ]);

    // Lưu vào database
    $daytro->save();

    // Hiện thị thông báo thành công
    flash()->option('position', 'top-center')->success('Đã thêm dãy trọ thành công');

    return redirect()->back();
}   

public function destroy($id)
{
    // Khai báo dãy trọ và lấy ra hàng cần xóa theo ID 
    $daytro = daytro::find($id);
    // Xóa hàng đó theo ID
    
    $daytro->delete();
    
    // Hiện thị thông báo thành công 
    flash()->option('position', 'top-center')->success('Đã xóa dãy trọ thành công!');

    return redirect()->back();

}


public function update(Request $request, $id)
{
  // Tìm ra trường thông tin cần update => Lấy ra tập data 
  $daytro = DayTro::findOrFail($id);
  
  // Nếu validate thấy vị phạm nó sẽ lưu error vào session và nhảy tới redirect() ngay lập tức 
  $validator = Validator::make($request->all(), [
    'tendaytro' => 'required|string|max:255|unique:daytro,tendaytro,'.$id,
    'tinh' => 'required|string|max:255',
    'huyen' => 'required|string|max:255',
    'xa' => 'required|string|max:255',
    'sonha' => 'required|string|max:255',
    ], [
        'tendaytro.unique' => 'Tên dãy trọ vừa cập nhật đã tồn tại',
    ]);
  
    if ($validator->fails())
    {
        return redirect()->back()->withErrors($validator, 'update_errors_' . $id)->withInput();
    } 

    // Đưa tất cả các thông tin đã update vào DB để sửa
   $daytro->update($request->all());
   $daytro->save();
   // Hiện thị thông báo thành công 
   flash()->option('position', 'top-center')->success('Dãy trọ đã được cập nhật thành công!');
   
    //    $daytros = DayTro::paginate(5);
    //    return view('pages.daytro', compact('daytros'));
    //  return redirect()->back();
    return redirect()->route('daytro');

}   


public function search(Request $request)
{   
    // Get the search value from the request
    $searchValue = $request->input('query');

    // Tìm kiếm dãy trọ dựa trên tên hoặc các trường khác có chứa giá trị 'queryValue'
    $daytros = DayTro::where('tendaytro', 'LIKE', "%{$searchValue}%")
            ->orWhere('tinh', 'LIKE', "%{$searchValue}%")
            ->orWhere('huyen', 'LIKE', "%{$searchValue}%")
            ->orWhere('xa', 'LIKE', "%{$searchValue}%")
            ->paginate(5);  // Phân trang
            

    // Return the search view with the resluts compacted
    return view('pages.daytro', compact('daytros'));   
     
}   

}

0 likes
1 reply
martinbean's avatar

@johnathan2020 This is why I don’t use modals for CRUD functionality. The UX is awful. Just put the create and edit forms on their own pages.

Please or to participate in this conversation.