dimanche 2 mai 2021

How to order by column Accessor from eloquent laravel [duplicate]

hello everyone I have a problem here, can I sort the data based on the results of an Accessor, is it possible? or I do I have to move the data to the column database? this is my code in model

protected $appends = ['percentage'];
public function getPercentageAttribute()
{
    $book = $this->books->halaman;
    $read = $this->pageread;
    $total = $read/$book*100;
    return ceil($total);
}

I try with this code

 $data = Read::with(['books.pengarang'])
                    ->orderBy('percentage')
                    ->get();

but I get error

Column not found: 1054 Unknown column percentage


from Newest questions tagged laravel-5 - Stack Overflow https://ift.tt/2SkgJ66
via IFTTT

samedi 1 mai 2021

Call to undefined function App\Http\Controllers\Auth\get_option() in laravel 8

I'm trying to convert an Laravel 5.7 project to 8, still debugging but I can't fix that spesific issue.

When I run that project I get "Call to undefined function App\Http\Controllers\get_option()" error.

I tried:

  • Change helpers.php location Providers to Controllers
  • Remove if clause in helpers.php file
  • Add "use App\Providers\helpers;" in Mainpage controller
  • Check Laravel 8 upgrading guide (https://laravel.com/docs/8.x/upgrade)

This is a source of error in App\Http\Controllers\MainpageController:

...
        $products_recommendations = Product::select('product.*')

            ->join('product_details', 'product_details.product_id', 'product.id')

            ->where('product_details.show_recommendations', 1)

            ->orderBy('update_date', 'desc')

            ->take(get_option('mainpage_list_product_count'))->get();
...

This is app/Providers/helpers.php file, it has get_option() function:

<?php
use App\Models\Option;
use Illuminate\Support\Facades\Cache;

if (! function_exists('get_option')) {
    function get_option($key) {
        //$allOptions = Cache::rememberForever('allOptions', function() {
        $minute = 60;
        $allOptions = Cache::remember('allOptions', $minute, function() {
            return Option::all();
        });
        
        return $allOptions->where('key', $key)->first()->variable;
    }
}

How can I fix that?

Note: I translated variable and function names into English for asking here.



from Newest questions tagged laravel-5 - Stack Overflow https://ift.tt/3vyQ4AH
via IFTTT

How to appending a column db result with relation and some logic laravel

I have a problem here, I want to add one column from the result of the relation, namely the percentage column, this column is the result of pagebook / pageread * 100 this is my result db what I want

"id": 2,
    "id_book": 2,
    "id_user": 2,
    "pageread": 120,
    "books": {
                "id": 2,
                "pagebook":125
             }
    "percentage":96

this is my code in controller

$book= Read::with('books.authors')->where('id_user',$user->id)->get();

and this my code in model

public function books()
    {
        return $this->belongsTo(Book::class,'id_book','id');
    }


from Newest questions tagged laravel-5 - Stack Overflow https://ift.tt/3gOiwdQ
via IFTTT

Laravel Eloquent perform Aggregation function in the pivot relationship

I would like to perform the average rating of respective product which stored in the pivot table called "reviews".Below are my existing code.

Product Model:

public function reviews(){
        return $this->belongsToMany(User::class,'reviews')->withPivot('comment','rating')->withTimestamps();
    }
public function getProductRatingAttribute(){
        return $this->reviews()->average('rating');
    }

User Model:

public function reviews(){
        return $this->belongsToMany(Product::class,'reviews')->withPivot('comment','rating')->withTimestamps();
    }

Migration for the reviews

Schema::create('reviews', function (Blueprint $table) {
            $table->id();
            $table->timestamps();
            $table->unsignedBigInteger('user_id');
            $table->unsignedBigInteger('product_id');
            $table->string('comment')->nullable();
            $table->integer('rating');
            $table->foreign('user_id')->references('id')->on('users');
            $table->foreign('product_id')->references('id')->on('products');
        });

I have attempted to use the below to code construct the expected output but it leads N+1 issue because it does not take advantage of the eager loading of the Laravel Eloquent.

$s = Product::with('reviews')->append('product_rating');

As I check telescope, it produces 6 query which can lead performance issue when the data is large. enter image description here

Expected output:

{
  "msg": [
    {
      "id": 4,
      "created_at": "2021-04-09T07:32:35.000000Z",
      "updated_at": "2021-04-09T07:32:35.000000Z",
      "name": "MacDonald",
      "nickname": "MCD",
      "users_id": 1,
      "price": "0.00",
      "product_rating": "3.3333",
      "reviews": [
        {
          "id": 1,
          "name": "John Smith",
          "email": "john.smith@hotmail.com",
          "email_verified_at": null,
          "created_at": "2021-04-08T13:29:13.000000Z",
          "updated_at": "2021-04-08T13:29:13.000000Z",
          "role": 0,
          "pivot": {
            "product_id": 4,
            "user_id": 1,
            "comment": "Ouch",
            "rating": 3,
            "created_at": "2021-05-01T11:51:26.000000Z",
            "updated_at": "2021-05-01T11:52:07.000000Z"
          }
        },
        {
          "id": 2,
          "name": "Kelvin Ooi",
          "email": "kelvin.ooi@hotmail.com",
          "email_verified_at": null,
          "created_at": "2021-04-08T13:29:13.000000Z",
          "updated_at": "2021-04-13T12:07:11.000000Z",
          "role": 1,
          "pivot": {
            "product_id": 4,
            "user_id": 2,
            "comment": "Ouch",
            "rating": 5,
            "created_at": "2021-05-01T11:51:26.000000Z",
            "updated_at": "2021-05-01T11:52:07.000000Z"
          }
        },
        {
          "id": 1,
          "name": "John Smith",
          "email": "john.smith@hotmail.com",
          "email_verified_at": null,
          "created_at": "2021-04-08T13:29:13.000000Z",
          "updated_at": "2021-04-08T13:29:13.000000Z",
          "role": 0,
          "pivot": {
            "product_id": 4,
            "user_id": 1,
            "comment": "Ouch",
            "rating": 2,
            "created_at": "2021-05-01T11:51:26.000000Z",
            "updated_at": "2021-05-01T11:52:07.000000Z"
          }
        }
      ]
    },
    {
      "id": 10,
      "created_at": null,
      "updated_at": null,
      "name": "Mary Bown",
      "nickname": "MB",
      "users_id": 1,
      "price": "2.88",
      "product_rating": "2.0000",
      "reviews": [
        {
          "id": 1,
          "name": "John Smith",
          "email": "john.smith@hotmail.com",
          "email_verified_at": null,
          "created_at": "2021-04-08T13:29:13.000000Z",
          "updated_at": "2021-04-08T13:29:13.000000Z",
          "role": 0,
          "pivot": {
            "product_id": 10,
            "user_id": 1,
            "comment": "Ouch",
            "rating": 2,
            "created_at": "2021-05-01T11:51:26.000000Z",
            "updated_at": "2021-05-01T11:52:07.000000Z"
          }
        }
      ]
    },
    {
      "id": 11,
      "created_at": null,
      "updated_at": null,
      "name": "Pizza Hut",
      "nickname": "pizzahut",
      "users_id": 1,
      "price": "4.10",
      "product_rating": null,
      "reviews": [
        
      ]
    },
    {
      "id": 12,
      "created_at": "2021-04-09T08:00:42.000000Z",
      "updated_at": "2021-04-09T08:00:42.000000Z",
      "name": "Domino Pizza",
      "nickname": "domino",
      "users_id": 3,
      "price": "0.00",
      "product_rating": null,
      "reviews": [
        
      ]
    },
    {
      "id": 13,
      "created_at": "2021-04-26T16:12:53.000000Z",
      "updated_at": "2021-04-26T16:12:53.000000Z",
      "name": "Chicken Chop",
      "nickname": "chickchop",
      "users_id": 3,
      "price": "0.00",
      "product_rating": null,
      "reviews": [
        
      ]
    }
  ]
}


from Newest questions tagged laravel-5 - Stack Overflow https://ift.tt/3ubOoNc
via IFTTT

how to develop a website through which visitors will be able to view and download .cdr fie

want to develop a website from where the visitors will be able to view and download coreldraw .cdr files. Which is already uploaded on my google drive and it is around 1 TB. Now the problem is that, How can i link my drive to my hosting server database to fetch the details into the website and preview it.



from Newest questions tagged laravel-5 - Stack Overflow https://ift.tt/3eFgDNS
via IFTTT

want to develop a designing website where users will be able to download coreldraw .cdr files [closed]

want to develop a designing website where users will be able to download coreldraw .cdr files. Which is already uploaded on my google drive and it is 1 TB. How can i preview files and let the users to download it only after login/sign in.



from Newest questions tagged laravel-5 - Stack Overflow https://ift.tt/3h6hSbV
via IFTTT

Laravel Get Value From json_encode

I can't get value of id from json string.

$photoIdObj = DB::table('images')->select('id')->whereJsonContains('name', ['path' => $photoPath])->get(); 
    $jsonString = json_encode($photoIdObj, true); //object to json string conversion. Result: "[{"id":7}]"
    $jsonArr = json_decode($jsonString); // json string to array
    $photoId = $jsonArr->id;
    
    //$selectedImages = \App\Image::where('id', $jsonString);
    $selectedImages = \App\Image::findOrFail($photoId);
    $selectedImages->forceDelete();

Error message:

"Trying to get property 'id' of non-object"


from Newest questions tagged laravel-5 - Stack Overflow https://ift.tt/3f03EXp
via IFTTT