Sinres

Sinres

Member Since 2 Years Ago

Experience Points
1,570
Total
Experience

3,430 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,570 XP
Nov
04
2 months ago
Activity icon

Replied to API Resource Collection - How Use Realtionship?

I came to this, that's right I can't use in this case collection haha :-)

I tried your solution but receives an error:

Symfony\Component\Debug\Exception\FatalThrowableError: Call to undefined method App\Http\Resources\Sale::resource() in file C:\xampp\htdocs\Customer-Service_v_2.0\app\Http\Resources\DeviceInspection.php on line 27

I found a solution for my problem and I did it that way in my api controller:

<?php

namespace App\Http\Resources;

use Illuminate\Http\Resources\Json\JsonResource;

class DeviceInspection extends JsonResource
{
    /**
     * Transform the resource into an array.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return array
     */
    public function toArray($request)
    {
        return [
            'id' => $this->id,
            'sale_id' => $this->sale_id,
            'inspection_date' => $this->inspection_date,
            'service_man' => $this->service_man,
            'status' => $this->status,
            'warranty_service' => $this->warranty_service,
            'created_at' => $this->created_at,
            'updated_at' => $this->updated_at,
            'user' => $this->user,
            'sale'=> $this->sale
            
        ];
    }

    public function with($request) {
        return [
            'version' => '1.0.0'
        ];
    }
}

DeviceInspection Model:

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;

class DeviceInspection extends Model
{
    use SoftDeletes;
    /**
    * The atributes that should be muted to dates.
    *
    * @var array
    */
    protected $fillable = [
        'sale_id',
        'inspection_date',
        'service_man',
        'status'
    ];

    protected $dates = ['deleted_at'];
    
    public $sortable = ['created_at'];

    public function sale()
    {
        return $this->belongsTo(Sale::class);
    }

    public function user() {
        return $this->belongsTo(User::class);
    }
}

And reply from API like this:

{
    "data": {
        "id": 1,
        "sale_id": 1,
        "inspection_date": "2020-02-21",
        "service_man": null,
        "status": "expectant",
        "warranty_service": 1,
        "created_at": "2019-03-08T13:21:43.000000Z",
        "updated_at": "2019-03-08T13:21:43.000000Z",
        "user": {
            "id": 1,
            "name": "Adrian",
            "email": "[email protected]",
            "sex": "m",
            "department": "integrator",
            "avatar": null,
            "created_at": null,
            "updated_at": "2019-03-14 11:37:25"
        },
        "sale": {
            "id": 1,
            "user_id": 1,
            "sale_number": "Sale 045/02/2019/AG",
            "customer_institution": "Institution of Customer",
            "institution_address": "Institution Address",
            "product_name": "Genano 310",
            "manufacturer": "---",
            "serial_number": "0000-0000",
            "amount_of_sales": "1",
            "sale_date": "2019-02-21",
            "term_realization": "2019-02-21",
            "guarantee_period": "24",
            "directory_link": "C:/",
            "status": "expectant",
            "file_sales": null,
            "sale_type": "national",
            "created_at": "2019-03-08 14:21:43",
            "updated_at": "2019-10-09 23:56:55",
            "deleted_at": "2019-10-09 23:56:55"
        }
    },
    "version": "1.0.0"
}

This is work! :-) But way your solution don't work and what is the difference between these two ways?

You won't believe it but the data from eloquent relationship was not displayed in the API response because I was using softDeletes, do you know why this happens? If he comments on "use softDeletes;" everything works correctly

Nov
03
2 months ago
Activity icon

Started a new Conversation API Resource Collection - How Use Realtionship?

Hello!

In my REST API I have a Sale Resource and DeviceInspection Resource and I need use data from Sale in DeviceInspection for display records. I trying like this but this is not work :-( Where I make a mistake?

Sale model

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use App\DeviceInspection;

class Sale extends Model
{
    use SoftDeletes;
    protected $fillable = [
        'user_id', 
        'sales_number', 
        'customer_institution', 
        'institution_address', 
        'product_name', 
        'manufacturer', 
        'serial_number', 
        'amount_of_sales', 
        'sale_date', 
        'guarantee_period', 
        'directory_link',
    ];
    public function inspection() {
        return $this->hasMany(DeviceInspection::class);
    }
}

Device Inspection model

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use App\Sale;

class DeviceInspection extends Model
{
    use SoftDeletes;
    /**
    * The atributes that should be muted to dates.
    *
    * @var array
    */
    protected $fillable = [
        'sale_id',
        'inspection_date',
        'service_man',
        'status'
    ];
    protected $dates = ['deleted_at'];
    
    public $sortable = ['created_at'];
    
    public function sale()
    {
        return $this->belongsTo(Sale::class);
    }
}

Sale Resource

<?php

namespace App\Http\Resources;

use Illuminate\Http\Resources\Json\JsonResource;

class Sale extends JsonResource
{
    /**
     * Transform the resource into an array.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return array
     */
    public function toArray($request)
    {
        return [
            'id' => $this->id,
            'sale_number' => $this->sale_number,
            'user_id' => $this->user_id,
            'customer_institution' => $this->customer_institution,
            'institution_address' => $this->institution_address,
            'product_name' => $this->product_name,
            'manufacturer' => $this->manufacturer,
            'serial_number' => $this->serial_number,
            'amount_of_sales' => $this->amount_of_sales,
            'sale_date' => $this->sale_date,
            'term_realization' => $this->term_realization,
            'guarantee_period' => $this->guarantee_period,
            'directory_link' => $this->directory_link,
            'file_sales' => $this->file_sales,
            'sale_type' => $this->sale_type,
        ];
    }

    public function with($request) {
        return [
            'version' => '1.0.0'
        ];
    }
}

DeviceInspection Resource

<?php

namespace App\Http\Resources;

use Illuminate\Http\Resources\Json\JsonResource;

class DeviceInspection extends JsonResource
{
    /**
     * Transform the resource into an array.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return array
     */
    public function toArray($request)
    {
        return [
            'id' => $this->id,
            'sale_id' => $this->sale_id,
            'inspection_date' => $this->inspection_date,
            'service_man' => $this->service_man,
            'status' => $this->status,
            'warranty_service' => $this->warranty_service,
            'created_at' => $this->created_at,
            'updated_at' => $this->updated_at,
            'sale' => Sale::collection($this->whenLoaded('sale'))
        ];
    }

    public function with($request) {
        return [
            'version' => '1.0.0'
        ];
    }
}

And my API Controller method show

/**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        return new DeviceInspectionResource(DeviceInspection::with('sale')->findOrFail($id));
    }

And when I try to get data from API by postman receives an error:

Symfony\Component\Debug\Exception\FatalThrowableError: Call to a member function first() on null in file C:\xampp\htdocs\Customer-Service_v_2.0\vendor\laravel\framework\src\Illuminate\Http\Resources\CollectsResources.php on line 24

How fix this??

Thanks

Sep
23
3 months 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
4 months 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
4 months 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
5 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
5 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?