lundi 8 octobre 2018

eager load model ids only

Using Laravel 5.1, I have deeply connected and nested models for my HTML5 game. When the player logs in, it loads their profiles.

Each profile has m:m completed quests, m:m completed tasks, m:m completed minigames, etc.

The quests/tasks/minigames are belongsTo relationship, i.e., Task belongsTo Quest, Minigame belongsTo Task, etc.

Eager loading these on the user->profile then takes a ton of time.

What I need to do instead then is eager load only the IDs of tasks, minigames, etc for the profile. I tried this via $appends:

class Profile extends BaseModel
{
    protected $with = ['game', 'quests'];

    protected $appends = ['task_ids'];

    public function getTaskIdsAttribute()
    {
        return $this->tasks->pluck('task_id');
    }

Still, this loads the models and an array of two null task Id values (The loaded task models eager load with their related children too.):

  • task_ids is an array with two null values.

  • tasks is an array with two eager loads Task models.

enter image description here

I need to speed up login so how can I load IDs only without the rest of the attributes?



via Chebli Mohamed

dimanche 7 octobre 2018

laravel update Has Many Relation for items in invoice

I'm trying to update items in the invoice, and I need and the problem when deleting item or add an item if I delete all item it updates I need help, please

invoice table

  • id
  • invoice _no
  • date
  • sub_total
  • discount
  • total

items table

  • id

  • item_code

  • item_desc

  • unit_price

invoice_items table

  • invoice_id

  • item_id

  • unit_price

  • qty

invoice model

public function items()
    {
        return $this->hasMany(InvoiceProduct::class, 'invoice_id');
    }

my controller update

$invoice = Invoice::findOrFail($id);

        $items = [];
        $itemIds = [];
        $subTotal = 0;

        foreach($request->items as $item) {
            if(isset($item['id'])) {
                InvoiceProduct::where('invoice_id', $invoice->id)
                    ->Where('id', $item['id'])
                    ->update($item);

                $itemIds[] = $item['id'];
               $item ='please re add items';
            } else {
                $items[] = new InvoiceProduct($item);
            }

            $subTotal += $item['unit_price'] * $item['qty'];
        }


        $data = $request->except('items');
        $data['sub_total'] = $subTotal;
        $data['total'] = $data['sub_total'] - $request->discount;

        $invoice->update($data);

        InvoiceProduct::whereNotIn('id', $itemIds)
            ->where('invoice_id', $invoice->id)
            ->delete();

        if(count($items)) {
            $invoice->items()->saveMany($items);
        }
 return response()
            ->json(['saved' => true, 'id' => $invoice->id]);
    }



via Chebli Mohamed

vendredi 5 octobre 2018

no hint path laravel

Hey guys it's my first time deploying my work online.. I have some couple of routes that work perfectly on my local but when I'm on the server I get this error..

View [frontoffice.Sondage.list_sondage] not found

I'm using l5-modular package for modules

it's something like :

app/
└── Modules/
    └── Poll/
        ├── Controllers/
        │   └── PollController.php
        ├── Models/
        │   └── Poll.php
        ├── Views/
        |     └── frontoffice
        │     └── Sondage
        |      └── list_sondage.blade.php
        ├── Translations/
        │   └── en/
        │       └── example.php
        ├── routes
        │   ├── api.php
        │   └── web.php
        └── helper.php

I'm returning this view :

 return view('Poll::frontoffice.Sondage.list_sondage',compact('resultats_groupe','config'));

what can be this error coming from?



via Chebli Mohamed

mercredi 3 octobre 2018

How to Searching between min and max prices in Laravel

I know in mysql query it is something like this

SELECT id 
FROM listings 
WHERE id  IN (
  SELECT id
  FROM listings
  WHERE price between 200 and 500
);

in laravel query I tired

Listing::select('listings.*')
                 ->whereBetween('price', [200, 500])
                 ->groupBy('listings.id')
                 ->orderBy('listings.id', 'desc')
                 ->paginate(1000);

did I wrong somewhere ? It just show only 1 result ?

thank you for your helps!



via Chebli Mohamed

mardi 2 octobre 2018

Autocomplete or Live search in Laravel/app.php

I am planning to develop a database. It's not started yet but I want to know whether it's possible to include autocomplete or Live search options in app.php.

Any ideas regarding this is welcome.

Thanks.



via Chebli Mohamed

Resolve index issue in php laravel [duplicate]

I'm trying to display download links(pdf) on my page but I'm getting an index error

Undefined offset: 6 (View: C:\xampp\htdocs\bluepenlabs\fta\app\Modules\Fta\views\frontoffice\Fta\voir-docs.blade.php)

This is my controller :

 public function docs(){
        $docs = Document::all();
        return view("Fta::frontoffice.Fta.voir-docs",compact('docs'));
 }

this is the view

 <?php
       if(file_exists(public_path('frontoffice/fichiers/docs'))){
         $files = File::allFiles(public_path('/frontoffice/fichiers/docs/'));
       }
       if(!empty($docs)){
               foreach ($docs as $doc){
                     //$fichier = explode("/", $file);
                     $fichier = explode("-", $doc['file']);
                    $ext = explode(".", $fichier[6]);

  ?>
        <span style="font-size: x-large;color: royalblue;"></span>
         <br><br>
         <p style="color: black">{!!  $doc->description !!}</p>
         <a href="/frontoffice/fichiers/docs//" download style="font-size: x-large"></a>

          @if($ext[1]== "pdf")
                <div class="box"><iframe src="/frontoffice/fichiers/docs//" width = "300px" height = "300px"></iframe></div>
           @endif  
           <br>   
           <medium> Crée le :  <br>Modifier le : </medium>
            <hr>
             <?php }
                   }
                  ?>

I tried to dd($fichier) it gives me this :

enter image description here



via Chebli Mohamed

Left Joining with two table does not work properly. It shows error for using max function

$student_classtest_for_sba_mark=DB::table('tbl_student_admission') 
                ->leftJoin('tbl_student_subject_mark', 'tbl_student_admission.student_id', '=', 'tbl_student_subject_mark.student_id')
                ->selectRaw('tbl_student_subject_mark.*, tbl_student_admission.student_id,  tbl_student_admission.student_full_name_english, tbl_student_admission.class, tbl_student_admission.section, tbl_student_admission.roll_no, sum(total_obtained_mark) as total_mark, sum(grade_point) as total_gread_point,
                max(if(tbl_student_subject_mark.exam_title = "'.$exam_list[0]->exam_id.'" , total_obtained_mark, null)) E1,                                               
                max(if(tbl_student_subject_mark.result_status = "Fail" , result_status, null)) result_status')                
                ->where('tbl_student_subject_mark.academic_year', $academic_year)
                ->where('tbl_student_admission.class', $class)                
                ->where('tbl_student_admission.section', $section)                
                ->where('tbl_student_subject_mark.subject_name', $subject_name)
                ->groupBy('tbl_student_admission.student_id')
                ->get(); 

Display the Error:

SQLSTATE[42000]: Syntax error or access violation: 1055 'db_smsfinal1user.tbl_student_subject_mark.student_subject_mark_id' isn't in GROUP BY



via Chebli Mohamed