Search code examples
insertcrudstoreeditlaravel-5.7

Laravel 5.7 CRUD explanation


Hello I'm absolutely new to Laravel, and I'm hoping someone could help me enlighten with its flow. although I do understand some of it, but I want to be educated and to be assured that what I have understood is really correct.

So, I want to go and understand the basics. Can someone educate me line by line with the storing and editing?

This is for the store/insert:

public function store(Request $request)
{

    $residents = new Resident;
    $residents->resident_fname = $request->input('resident_fname');
    $residents->resident_lname = $request->input('resident_lname');
    $residents->resident_mi = $request->input('resident_mi');
    $residents->resident_email = $request->input('resident_email');
    $residents->resident_age = $request->input('resident_age');
    $residents->resident_dob = $request->input('resident_dob');
    $residents->role = 'resident';
    $residents->resident_address = $request->input('resident_address');
    $residents->resident_contact = $request->input('resident_contact');
    $residents->resident_gender = $request->input('resident_gender');
    $residents->ResidentVoter_status = $request->input('ResidentVoter_status');
    $residents->resident_status = $request->input('resident_status');
    $residents->resident_religion = $request->input('resident_religion');
    $residents->resident_purok = $request->input('resident_purok');
    $residents->save();

    return redirect('/residents')->with('success', 'Successfully Added!');
}

this is for the update:

public function update(Request $request, $id)
{
    $residents = Resident::find($id);
    $residents->resident_fname = $request->input('resident_fname');
    $residents->resident_lname = $request->input('resident_lname');
    $residents->resident_mi = $request->input('resident_mi');
    $residents->resident_age = $request->input('resident_age');
    $residents->resident_dob = $request->input('resident_dob');
    $residents->resident_email = $request->input('resident_email');
    $residents->resident_address = $request->input('resident_address');
    $residents->resident_contact = $request->input('resident_contact');
    $residents->resident_gender = $request->input('resident_gender');
    $residents->ResidentVoter_status = $request->input('ResidentVoter_status');
    $residents->resident_status = $request->input('resident_status');
    $residents->resident_religion = $request->input('resident_religion');
    $residents->resident_purok = $request->input('resident_purok');
    $residents->Save();

    return redirect('/residents');
}

or is there someone willing to educate me?


Solution

  • I will try my best for this,

    For store,

    For store you have to make new object of that model because you want to insert new record then left part of each line says that you have to insert that field on that particular model.

    Like here

    $residents->role = 'resident';

    here you want to insert resident value in role field of database and like that for all. Right side of each line means you request that input which is submitted.

    After that you call save method on this obejct to store all data.

    For update,

    In edit you are finding existing record and update it. so you call find method on that particular id. If you use find than laravel match with id. If you want to match another column than you have to use where. And same as store you are adding all data in object.

    And after that you are updating that record using update.

    Suggetions

    1. Suppose that you have records which is dependent on another field. So at that time you can use DB Transaction for that you can refer this Laravel DB transaction

    2. You should use try-catch to handle exception properly.

    3. Use findOrFail, firstOrFail to catch your exception if given id or wrong is passed in url directly.

    4. Use proper validation in controller and if possible than make new request class and use that class to validate your request. For that you can refer this Custom form Request

    5. Use proper naming convention.

    Hope this help :)