samedi 21 août 2021

Call to undefined relationship [relation] on model [App/Model]

Environment:

  • Laravel Version: 5.7.29
  • PHP Version $ php --version: PHP 7.2.24 (cli)
  • Database Driver & Version $ mysql --version: mysql Ver 8.0.23-0ubuntu0.20.04.1
  • Doctrine/Inflector: 1.4.4

composer.json

    "require": {
        "php": ">=7.2.24",
        "fideloper/proxy": "~4.0",
        "laravel/framework": "5.7.*",
        "laravel/tinker": "~1.0",
        "doctrine/inflector": "1.4.4"
    },

Problem Statement:

Below relation is working on localhost but throwing error on Server. This seems like issue is with Doctrine/Inflector package.

$ php artisan tinker
>>> $borrower = Borrower::with('application')->find(2);

PHP Deprecated:  The "Doctrine/Common/Inflector/Inflector::pluralize" method is deprecated and will be dropped in doctrine/inflector 2.0. Please update to the new Inflector API. in /path/vendor/doctrine/inflector/lib/Doctrine/Common/Inflector/Inflector.php on line 264
Illuminate/Database/Eloquent/RelationNotFoundException with message 'Call to undefined relationship [application] on model [App/Borrower].'

Models

App\Borrower.php

<?php  

namespace App;

use Illuminate\Foundation\Auth\User as Authenticatable;

class Borrower extends Authenticatable
{

    protected $table = 'borrowers';

    public function application()
    {
        return $this->hasMany('App\Models\Application', 'borrower_id', 'borrower_id');
    }

App\Models\Application.php

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class Application extends Model
{
    
    protected $table = 'applications';

    public function borrower()
    {
        return $this->belongsTo('App\Borrower', 'borrower_id', 'borrower_id');
    }

Table


borrowers

| borrower_id | fname  | email                  |
|-------------|--------|------------------------|
| 1           | John   | john@peakyblinder.com  |
| 2           | Thomas | tommy@peakyblinder.com |

applications

| id | name       | borrower_id |
|----|------------|-------------|
| 1  | Birmingham | 1           |
| 2  | BBC        | 1           |



via Chebli Mohamed

vendredi 20 août 2021

Error trying to get property 'users' of non-object

I have a problem, i am doing a shift history of the user logged in the application.

   @foreach($eventos as $evento)
                        if($evento->solicitud->users->id == (Auth::user()->id))
                            <div class="item-timeline">
                                <div class="t-meta-date">
                                    <p class=""></p>
                                </div>
                                <div class="t-dot">
                                </div>
                                <div class="t-text">
                                    <p></p>
                                    <p></p>
                                </div>
                            </div>
                     @endif
                        @endforeach

I get all shifts with:

 $eventos = Eventos::All();

and then filter with if condition

But it shows me error Trying to get property 'users' of non-object

dd capture: https://i.imgur.com/WUVOaO6.png

data base: https://i.imgur.com/J5YSvxQ.png

i need to get the User_id from the solicitudes table.

the relation is: Eventos table-> solicitud_id with the ID column of the Solicitudes table



via Chebli Mohamed

Laravel upgrade form 5.5 to 5.6

I am trying to upgrade laravel application from 5.5 to 5.6 in the documentation, regarding database modification, in Index Order Of Morph Columns section, it was mentioned that

If the application is in production, you should pass an explicit index name to the morphs method.

What kind of modification should I do there?

Thanks!



via Chebli Mohamed

open PDF in modal with laravel and HTTPS subdomain

I´m traying to open pdf file in modal with laravel, but always return this:

Sorry, the page you are looking for could not be found.

i don´t know that i´m doing bad.

First i´m upload my file with input type file and ajax using formData, in my controller process this file:

/**
     * FUNCTION TO SAVE ORDER WITH DEALER
     */
    public function saveOrderDealerNumber(Request $request)
    {   
        try{

            if($request->file('adjunto') != ""){

                $nombreArchivo = $request->file('adjunto')->getClientOriginalName();
                $file = $request->file('adjunto');

                $path = public_path().'/postventa';
                $uplaod = $file->move($path,$nombreArchivo);
                
                return asset('postventa/'.$nombreArchivo);
            }else{
                return "Error al subir fichero, consulte al administrador del sistema";
            }
    
        }catch(Exception $e){
            return $e->getMessage();
        }
    }

this function upload ok my file in my folder in public_path() i don´t use storage folder.

In javasript response i have this:

success: function(response){
                    var today = new Date();
                    var date = today.getDate()+"/"+"0"+(today.getMonth()+1)+"/"+today.getFullYear()+" "+
                    today.getHours() + ":" + today.getMinutes();

                    //date to insert in db
                    var dateDB = today.getFullYear()+"-"+"0"+(today.getMonth()+1)+"-"+today.getDate()+" "+today.getHours()+":"+today.getMinutes()+":"+today.getSeconds();

                    $("#adjuntoSend").val(file.name);
                    $("#usuarioSend").val('');
                    $("#fecha_horaSend").val(dateDB);
                    $("#routeAttachedSend").val(file.name);

                    $("#tableFiles tbody").append(`
                        <tr class="text-center">`+
                        `<td>`+file.name+`</td>`
                        +`<td>`+file.type+`</td>`
                        +`<td>`+"{!! auth()->user()->nombre !!}"+`</td>`
                        +`<td>`+date+`</td>`
                        +`<td><button type="button" class="btn" data-toggle="modal" data-target="#myModal"><i class="fas fa-eye text-dark fa-2x"></i></button></td>`
                        +`</tr>`);

                    $(".btn").on('click', function(){
                        $("#document").attr("src", response);
                    })
                },

I´m set src attr to embed tag in my modal bootstrap, this it´s result:

<embed src="https://xxxx/postventa/cliente.pdf" frameborder="0" width="100%" height="400px">

postventa it´s a folder in public.

When i´m editting my order, i have same code and also i have same error.. But in my localhost it´s ok.

my subdomain it´s in VPN in my server.

Any body have idea that i´m doing wrong?

this it´s my file My file uploaded

this it´s my route in my pc My route in my pc

Thanks for help. I´m modifying this app, all system it´s in windows server with xampp.....



via Chebli Mohamed

jeudi 19 août 2021

Array and string offset access syntax with curly braces is deprecated in php-7.4.19-Win32-vc15-x64

I have this old project with importing excel data to db,i am using laragon with latest version with php-7.4.19-Win32-vc15-x64 but the project is run with 7.1 in the server so i have used php-7.1.25-Win32-VC14-x64 this php version,but when i try to upload excel it throws this error I have searched also found using [] instead of {} will fix the issue,but i have tried that, this is line where showed the error but i tried to use [] this but same error throws }); return back()->with('success', 'Your File Is Successfully Uploaded To Database!');

public function importExcel(Request $request)
{
    if ($request->hasFile('import_file')) {

        Excel::load($request->file('import_file')->getRealPath(), function ($reader) {
            foreach ($reader->toArray() as $key => $row) {
                
                $old_customer = Registration::where('customerid', $row['customerid'])->first();
                if(count($old_customer)==0)
                {

                $data['customername'] = $row['customername'];
                $data['chassis'] = $row['chassis'];
                $data['model'] = $row['model'];
                $data['branchcode'] = $row['branchcode'];
                $data['delivery'] = $row['delivery'];
                $data['customerid'] = $row['customerid'];
                $data['phone'] = $row['phone'];
                $data['invoicedate'] = $row['invoicedate'];
                
                $registration_id = Registration::orderBy('registration_id', 'desc')->take(1)->get();

                if (count($registration_id) > 0) {
                    $regid = $registration_id[0]->registration_id;
                    $regid = $regid + 1;

                } else {
                    $regid = 1;
                }

                $register = new Registration();
                $register->registration_id = $regid;
                $register->customername = $row['customername'];
                $register->chassis = $row['chassis'];
                $register->model = $row['model'];
                $register->branchcode = $row['branchcode'];
                $register->delivery = $row['delivery'];
                $register->customerid = $row['customerid'];
                $register->phone = $row['phone'];
                $register->invoicedate = $row['invoicedate'];
                $register->created_user_id = Session::get('created_id');
                $register->save();

                $regidn = Registration::orderBy('registration_id', 'desc')->get();
                $regidd = $regidn[0]->registration_id;
                $ssitrack = new Ssi_track();
                $ssitrack->registration_id = $regid;
                $ssitrack->ssi_track_id = $regid;
                $ssitrack->save();
                $ssitrackk = Ssi_track::orderBy('ssi_track_id', 'desc')->get();
                $ssitrackk = $ssitrackk[0]->registration_id;

                }
                
            }
        });
      return back()->with('success', 'Your File Is Successfully Uploaded To Database!');
    }
    return back()->with('success', 'File Doesnt Uploaded!');
}


via Chebli Mohamed

How to pass condition in laravel 5 relation model

I am trying to get the data from group model but i am relate the student table new_group_id to group table id if it not null else connect the group_id to group table id.

public function grade(Builder $query){
       
        
        return $query
          ->when($this->new_group_id != 0,function($q){
              return $q->with('new_group_id');
         })
         ->when($this->new_group_id === 0,function($q){
              return $q->with('group_id');
         });
    }

But given the error is Argument 1 passed to App\Students::grade() must be an instance of App\Builder,

please help me to solve these error



via Chebli Mohamed

mercredi 18 août 2021

Access Laravel URL with ID only (Best approach)

My application is made in laravel 5.8, I want to ask about the best approach for a unique scenario where I want to display some of my customer profiles like

https://www.example.com/1234

I know that in laravel I have to give a unique identifier before the URL like https://www.example.com/profile/1234 so that it know which function to look for. But for some reason I have to build the above URL.

I tried it directly like:

Route::get('/{id}', 'ExampleController@myfunction')->name('profiler');

But this only works when I put it in bottom of my Routes file. If I place it somewhere in the middle or top. All other links like https://www.example.com/about-us stops working.

My Question is:

  • What is the best approach to achieve something like this. (Is it fine that I keep it in the bottom of the route file or will it be some kind of threat or any drawbacks to this approach?)
  • Should I use this approach or add a unique identifier instead.

I would really want to use the URL I mentioned in the start of the question.

Thank you all in advance for helping me.

Peace :)



via Chebli Mohamed