Sinres

Sinres

Member Since 1 Year Ago

Experience Points
1,510
Total
Experience

3,490 experience to go until the next level!

In case you were wondering, you earn Laracasts experience when you:

  • Complete a lesson — 100pts
  • Create a forum thread — 50pts
  • Reply to a thread — 10pts
  • Leave a reply that is liked — 50pts
  • Receive a "Best Reply" award — 500pts
Lessons Completed
5
Lessons
Completed
Best Reply Awards
0
Best Reply
Awards
  • start-engines Created with Sketch.

    Start Your Engines

    Earned once you have completed your first Laracasts lesson.

  • first-thousand Created with Sketch.

    First Thousand

    Earned once you have earned your first 1000 experience points.

  • 1-year Created with Sketch.

    One Year Member

    Earned when you have been with Laracasts for 1 year.

  • 2-years Created with Sketch.

    Two Year Member

    Earned when you have been with Laracasts for 2 years.

  • 3-years Created with Sketch.

    Three Year Member

    Earned when you have been with Laracasts for 3 years.

  • 4-years Created with Sketch.

    Four Year Member

    Earned when you have been with Laracasts for 4 years.

  • 5-years Created with Sketch.

    Five Year Member

    Earned when you have been with Laracasts for 5 years.

  • school-session Created with Sketch.

    School In Session

    Earned when at least one Laracasts series has been fully completed.

  • welcome-newcomer Created with Sketch.

    Welcome To The Community

    Earned after your first post on the Laracasts forum.

  • full-time-student Created with Sketch.

    Full Time Learner

    Earned once 100 Laracasts lessons have been completed.

  • pay-it-forward Created with Sketch.

    Pay It Forward

    Earned once you receive your first "Best Reply" award on the Laracasts forum.

  • subscriber-token Created with Sketch.

    Subscriber

    Earned if you are a paying Laracasts subscriber.

  • lifer-token Created with Sketch.

    Lifer

    Earned if you have a lifetime subscription to Laracasts.

  • lara-evanghelist Created with Sketch.

    Laracasts Evangelist

    Earned if you share a link to Laracasts on social media. Please email [email protected] with your username and post URL to be awarded this badge.

  • chatty-cathy Created with Sketch.

    Chatty Cathy

    Earned once you have achieved 500 forum replies.

  • lara-veteran Created with Sketch.

    Laracasts Veteran

    Earned once your experience points passes 100,000.

  • 10k-strong Created with Sketch.

    Ten Thousand Strong

    Earned once your experience points hits 10,000.

  • lara-master Created with Sketch.

    Laracasts Master

    Earned once 1000 Laracasts lessons have been completed.

  • laracasts-tutor Created with Sketch.

    Laracasts Tutor

    Earned once your "Best Reply" award count is 100 or more.

  • laracasts-sensei Created with Sketch.

    Laracasts Sensei

    Earned once your experience points passes 1 million.

  • top-50 Created with Sketch.

    Top 50

    Earned once your experience points ranks in the top 50 of all Laracasts users.

Level 1
1,510 XP
Sep
23
3 weeks ago
Activity icon

Replied to Store Data To Two Table From One Controller

Thanks. How I can fix this ? By model? I need store record from one function and use in other for create device inspection with sale. Can you give some one for example?

Activity icon

Replied to Store Data To Two Table From One Controller

What do you think about this?

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function createSale(Request $request)
    {
        // New sales register by user from SalesController
        $sale = new Sale();
        $sale->sale_number = $request->sale_number;
        $sale->user_id = $request->user_id;
        $sale->customer_institution = $request->customer_institution;
        $sale->institution_address = $request->institution_address;
        $sale->product_name = $request->product_name;
        $sale->manufacturer = $request->manufacturer;
        $sale->serial_number = $request->serial_number;
        $sale->amount_of_sales = $request->amount_of_sales;
        $sale->sale_date = $request->sale_date;
        $sale->term_realization = $request->term_realization;
        $sale->guarantee_period = $request->guarantee_period;
        $sale->directory_link = $request->directory_link;
        $sale->file_sales = $request->file_sales;
        $sale->sale_type = $request->sale_type;
        $sale->save();

        if($sale->save()) {
            return $this->createDeviceInspection($sale);
            return new SaleResource($sale);
        }

    }

    public function createDeviceInspection($sale) {

        $number_of_inspection = $this->createSale($sale->guarantee_period) / 12; 

        //Variable for if where check data of sale != about current date
        $term_realization = Carbon::parse($this->createSale($sale->term_realization));
        $term_realization->format('Y-m-d');
        $current_date = Carbon::now()->format('Y-m-d');

        if ($term_realization->addYears($number_of_inspection) <= $current_date) {
            $current_date_record = Carbon::now()->format('Y');
            $date_record = Carbon::parse($sale->term_realization)->format('Y');
            $number_of_records = $current_date_record-$date_record;
            $number_of_records = $number_of_records + $number_of_inspection;
            $number_of_records += 3;
        } else {
            $number_of_records = $number_of_inspection;
            $number_of_records += 3;
        }
        // Checking if the sale is national
        if ($this->createSale($sale->sale_type) == 'national') {
            // Checking if the device inspection is guaranteed
            for ($i=1; $i <= $number_of_records; $i++) {
                // Create DeviceInspections
                $deviceInspection = new DeviceInspection;
                $deviceInspection->user_id = $this->createSale($sale->user_id);
                $deviceInspection->sale_id = $this->createSale($sale->id);
                $deviceInspection->inspection_date = $this->createSale($sale->term_realization);
                
                // Add one year for first inspection and make other
                $parse_inspection_date = Carbon::parse($deviceInspection->inspection_date);
                $parse_inspection_date->format('Y-m-d');
                $parse_inspection_date->addYears($i);
                $deviceInspection->inspection_date = $parse_inspection_date;
    
                $date = Carbon::parse($this->createSale($sale->term_realization));
                $date->format('Y-m-d');
                $date->addYears($number_of_inspection);

                // Checking if the device inspection is out-of-warranty
                if ($this->createSale($sale->guarantee_period) != NULL && $date >= $deviceInspection->inspection_date) {
                    $deviceInspection->warranty_service = 1;
                } else {
                    $deviceInspection->warranty_service = 0;
                }

                $deviceInspection->save();
            }
        }
    }

But I have a this error:

Symfony\Component\Debug\Exception\FatalThrowableError: Argument 1 passed to App\Http\Controllers\Api\SaleController::createSale() must be an instance of Illuminate\Http\Request, string given, called in C:\xampp\htdocs\Customer-Service_v_2.0\app\Http\Controllers\Api\SaleController.php on line 61 in file C:\xampp\htdocs\Customer-Service_v_2.0\app\Http\Controllers\Api\SaleController.php on line 32

Thank you very much for your solution I will use it but I also need to learn how to use the function in other function in this same class and I would like to learn how to perform such an operation in this way :-)

Sep
18
1 month ago
Activity icon

Started a new Conversation Store Data To Two Table From One Controller

Hello Guys!

In my function of sale controller when user created a new sales a record for the table with device reviews is created with the sale. This code is very ugly and I would like to break it down into several functions.

Principle of operation:

  • The user creates a new sale
  • device reviews are generated based on the installation date and warranty period
  • inspections are carried out every year
  • inspections are divided into warranty and post-warranty

Okey, so far everything was done in one controller function - very bad, I know.

 /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
            
        if ($request->ajax()) {
            // New sales register by user from SalesController
            $sales = new Sale;
            $sales->user_id = Auth::id();
            $sales->sale_number = $request->get('sale_number');
            $sales->customer_institution = $request->get('customer_institution');
            $sales->institution_address = $request->get('institution_address');
            $sales->product_name = $request->get('product_name');
            $sales->manufacturer = $request->get('manufacturer');
            $sales->serial_number = $request->get('serial_number');
            $sales->amount_of_sales = $request->get('amount_of_sales');
            $sales->sale_date = $request->get('sale_date_create');
            $sales->term_realization = $request->get('term_realization_create');
            $sales->guarantee_period = $request->get('guarantee_period');
            $sales->directory_link = $request->get('directory_link');
            $sales->file_sales = $request->get('file_sales');
            $sales->sale_type = $request->get('sale_type');

            $sales->save();

            $number_of_inspection = $sales->guarantee_period / 12;

            //Variable for if where check data of sale != about current date
            $term_realization = Carbon::parse($sales->term_realization);
            $term_realization->format('Y-m-d');
            $current_date = Carbon::now()->format('Y-m-d');

            if ($term_realization->addYears($number_of_inspection) <= $current_date) {
                $current_date_record = Carbon::now()->format('Y');
                $date_record = Carbon::parse($sales->term_realization)->format('Y');

                $number_of_records = $current_date_record-$date_record;
                $number_of_records = $number_of_records + $number_of_inspection;
                $number_of_records += 3;

            } else {
                $number_of_records = $number_of_inspection;
                $number_of_records += 3;
            }

            // Checking if the sale is national
            if ($sales->sale_type == 'national') {
                // Checking if the device inspection is guaranteed
                for ($i=1; $i <= $number_of_records; $i++) {
                    // Create DeviceInspections
                    $deviceInspection = new DeviceInspection;
                    $deviceInspection->user_id = $sales->user_id;
                    $deviceInspection->sale_id = $sales->id;
                    $deviceInspection->inspection_date = $sales->term_realization;
                    
                    // Add one year for first inspection and make other
                    $parse_inspection_date = Carbon::parse($deviceInspection->inspection_date);
                    $parse_inspection_date->format('Y-m-d');
                    $parse_inspection_date->addYears($i);
                    $deviceInspection->inspection_date = $parse_inspection_date;
        
                    $date = Carbon::parse($sales->term_realization);
                    $date->format('Y-m-d');
                    $date->addYears($number_of_inspection);
    
                    // Checking if the device inspection is out-of-warranty
                    if ($sales->guarantee_period != NULL && $date >= $deviceInspection->inspection_date) {
                        $deviceInspection->warranty_service = 1;
                    } else {
                        $deviceInspection->warranty_service = 0;
                    }
    
                    $deviceInspection->save();
                }
            }

            return response()->json($sales);
        }
    }

Okey, it's that shit.

How do you think creating sales and creating device inspection break down into two separate functions? Separate into two separate controllers, e.g. SalesController and DeviceInspectionsController? Jeżeli zapisywany jest rekord z danymi sprzedaży jak mogę w locie przechwycić dane do uzupełnia device inspection record?

Sep
17
1 month ago
Activity icon

Started a new Conversation Calling Other Function In This Same Controller

Hello Guys!

Look this is piece of my controller where a store a new record of sale. Where is the problem? I need use other function in function store in this same controller.

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $sale = new Sale;
        $sale->user_id = Auth::id();
        $sale->sale_number = $request->sale_number;
        $sale->customer_institution = $request->customer_institution;
        $sale->institution_address = $request->institution_address;
        $sale->product_name = $request->product_name;
        $sale->manufacturer = $sales->manufacturer;
        $sale->serial_number = $request->serial_number;
        $sale->amount_of_sale = $request->amount_of_sale;
        $sale->sale_date = $request->sale_date;
        $sale->term_realization = $request->term_realization;
        $sale->guarantee_period = $request->guarantee_period;
        $sale->directory_link = $request->directory_link;
        $sale->file_sale = $request->file_sale;
        $sale->sale_type = $request->sale_type;
        $sale->save();

        return $this->checkGuaranteePeriod($sale->guarantee_period);
    }

    /**
     * Checks the number of device inspection based on guarantee period.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function checkGuaranteePeriod($guarantee_period)
    {
        if ($guarantee_period == 60) {
            $number_of_inspection = 5;
        } elseif ($guarantee_period == 48) {
            $number_of_inspection = 4;
        } elseif ($guarantee_period == 36) {
            $number_of_inspection = 3;
        } elseif ($guarantee_period == 24) {
            $number_of_inspection = 2;
        } elseif ($guarantee_period == 12) {
            $number_of_inspection = 1;
        }
    }

My solution is good way for SOLID practice? I am asking for your understanding haha I'm at junior level

Aug
14
2 months ago
Activity icon

Replied to (errno: 150 "Foreign Key Constraint Is Incorrectly Formed")

You must make a new migration because one of the migrations has an older date in its name. It's best to check the order of your migrations in database\migration because one of the migrations must be first ;-)

Aug
05
2 months ago
Activity icon

Replied to Best Way To Dynamic Content Loading In 2019?

Okey Guys,

Thanks a lot! I try use Vuejs for dynamic content loading in my app.

Activity icon

Started a new Conversation Best Way To Dynamic Content Loading In 2019?

Hello Everone!

I have a question what is best way to loading dynamic content in Laravel 5.7 ? I have used the jQuery library so far but I would like to use something more optimal. What are your suggestions?