academico-sis /
academico
| 1 | <?php |
||||
| 2 | |||||
| 3 | namespace App\Http\Controllers; |
||||
| 4 | |||||
| 5 | use App\Http\Requests\ContactRequest as StoreRequest; |
||||
| 6 | use App\Models\Contact; |
||||
| 7 | use App\Models\PhoneNumber; |
||||
| 8 | use App\Models\Profession; |
||||
| 9 | use Illuminate\Http\Request; |
||||
| 10 | use Prologue\Alerts\Facades\Alert; |
||||
| 11 | |||||
| 12 | class ContactController extends Controller |
||||
| 13 | { |
||||
| 14 | public function __construct() |
||||
| 15 | { |
||||
| 16 | parent::__construct(); |
||||
| 17 | $this->middleware(backpack_middleware()); |
||||
| 18 | } |
||||
| 19 | |||||
| 20 | /** |
||||
| 21 | * Store a newly created resource in storage. |
||||
| 22 | */ |
||||
| 23 | public function store(StoreRequest $request) |
||||
| 24 | { |
||||
| 25 | $request->validate([ |
||||
| 26 | 'firstname' => 'required', |
||||
| 27 | 'lastname' => 'required', |
||||
| 28 | 'email' => 'required', |
||||
| 29 | 'address' => 'required', |
||||
| 30 | 'phone_number' => 'required', |
||||
| 31 | 'idnumber' => 'required', |
||||
| 32 | 'contact_type' => 'required', |
||||
| 33 | ]); |
||||
| 34 | |||||
| 35 | $contact = new Contact(); |
||||
| 36 | $contact->student_id = $request->input('student_id'); |
||||
| 37 | $contact->firstname = $request->input('firstname'); |
||||
| 38 | $contact->lastname = $request->input('lastname'); |
||||
| 39 | $contact->idnumber = $request->input('idnumber'); |
||||
| 40 | $contact->address = $request->input('address'); |
||||
| 41 | $contact->email = $request->input('email'); |
||||
| 42 | $contact->relationship_id = $request->input('contact_type'); |
||||
| 43 | $contact->save(); |
||||
| 44 | |||||
| 45 | // register the phone number |
||||
| 46 | if ($request->input('phone_number') !== null) { |
||||
| 47 | $phone = new PhoneNumber(); |
||||
| 48 | $phone->phoneable_id = $contact->id; |
||||
| 49 | $phone->phoneable_type = Contact::class; |
||||
| 50 | $phone->phone_number = $request->input('phone_number'); |
||||
| 51 | $phone->save(); |
||||
| 52 | } |
||||
| 53 | |||||
| 54 | Alert::success(__('The information has successfully been saved'))->flash(); |
||||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||||
| 55 | |||||
| 56 | return redirect()->back(); |
||||
| 57 | } |
||||
| 58 | |||||
| 59 | public function update(Contact $contact, Request $request) |
||||
| 60 | { |
||||
| 61 | // check if the user is allowed to edit the contact |
||||
| 62 | if (! backpack_user()->can('update', $contact)) { |
||||
| 63 | abort(403); |
||||
| 64 | } |
||||
| 65 | |||||
| 66 | $request->validate([ |
||||
| 67 | 'firstname' => 'required|max:255', |
||||
| 68 | 'lastname' => 'required|max:255', |
||||
| 69 | 'email' => 'required', |
||||
| 70 | ]); |
||||
| 71 | |||||
| 72 | $contact->update([ |
||||
| 73 | 'firstname' => $request->firstname, |
||||
| 74 | 'lastname' => $request->lastname, |
||||
| 75 | 'idnumber' => $request->idnumber, |
||||
| 76 | 'address' => $request->address, |
||||
| 77 | 'email' => $request->email, |
||||
| 78 | 'relationship_id' => $request->contact_type, |
||||
| 79 | ]); |
||||
| 80 | |||||
| 81 | if ($request->profession) { |
||||
| 82 | $profession = Profession::firstOrCreate([ |
||||
| 83 | 'name' => $request->profession, |
||||
| 84 | ]); |
||||
| 85 | |||||
| 86 | $contact->update([ |
||||
| 87 | 'profession_id' => $profession->id, |
||||
| 88 | ]); |
||||
| 89 | } |
||||
| 90 | |||||
| 91 | Alert::success(__('The information has successfully been saved'))->flash(); |
||||
|
0 ignored issues
–
show
It seems like
__('The information has successfully been saved') can also be of type array and array; however, parameter $text of Prologue\Alerts\Facades\Alert::success() does only seem to accept string, maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
| 92 | |||||
| 93 | return redirect($request->redirect_path); |
||||
| 94 | } |
||||
| 95 | |||||
| 96 | // open a page to update contact information |
||||
| 97 | public function edit(Contact $contact) |
||||
| 98 | { |
||||
| 99 | // check if the user is allowed to edit the contact |
||||
| 100 | if (! backpack_user()->can('update', $contact)) { |
||||
| 101 | abort(403); |
||||
| 102 | } |
||||
| 103 | |||||
| 104 | return view('students.edit-contact', [ |
||||
| 105 | 'contact' => $contact, |
||||
| 106 | 'redirect_url' => url()->previous(), |
||||
| 107 | ]); |
||||
| 108 | } |
||||
| 109 | |||||
| 110 | // delete additional contact information |
||||
| 111 | public function destroy(Contact $contact) |
||||
| 112 | { |
||||
| 113 | $contact->delete(); |
||||
| 114 | } |
||||
| 115 | } |
||||
| 116 |