ahdeveloper
3 سال پیش توسط ahdeveloper مطرح شد
1 پاسخ

شرط وجود داشتن یک دیتا

سلام وقت بخیر دوستان
من یک شرطی رو در کنترلر خودم گذاشتم و گفتم اگه یک دوره با مشخصات یوزر کدی که از ریکوئست ارسال میشه توی لیست دوره ها وجود نداشت اون دوره رو ایجاد کن

حالا میخوام ی شرط دیگه هم بهش اضافه کنم و بگم اگه دوره ای با مشخصات این یوزر کد وجود داشت و status این دوره برابر 1 بود دوره رو ایجاد نکنه

چطور باید شرط دوم رو بزارم ممنون میشم راهنمایی بفرمایید ممنونم

if (!(Intership::where('user_code',$request->user_code)->exists())){
            Intership::create([
                'intership_code' => $request->intership_code,
                'intership_term_code' => $request->intership_term_code,
                'user_code' => $request->user_code,
                'teacher_code' => $request->teacher_code,
                'school_code' => $request->school_code,
                'work_place_code' => $request->work_place_code,
            ]);
            return redirect()->route('admin.interships.index')->with('success', 'دوره کار آموزی با موفقیت ایجاد شد');
        }

ثبت پرسش جدید
مهران مرندی
تخصص : برنامه نویس
@mehranmarandi 3 سال پیش آپدیت شد
0

متدی که خود لاراول برای این کار معرفی کرده firstOrCreate/ firstOrNew هست
لینک

// Retrieve the flight by the attributes, or create it if it doesn't exist...
$flight = App\Flight::firstOrCreate(['name' => 'Flight 10']);

// Retrieve the flight by the attributes, or instantiate a new instance...
$flight = App\Flight::firstOrNew(['name' => 'Flight 10']);

بر اساس کد خودتون هم به این شکل میشه انجامش داد

if (!(Intership::where('user_code',$request->user_code)->where('status','!=',1)->exists())){
            Intership::create([
                'intership_code' => $request->intership_code,
                'intership_term_code' => $request->intership_term_code,
                'user_code' => $request->user_code,
                'teacher_code' => $request->teacher_code,
                'school_code' => $request->school_code,
                'work_place_code' => $request->work_place_code,
            ]);
            return redirect()->route('admin.interships.index')->with('success', 'دوره کار آموزی با موفقیت ایجاد شد');
        }

برای ارسال پاسخ لازم است وارد شده یا ثبت‌نام کنید

ورود یا ثبت‌نام