lundi 1 avril 2019

Laravel 5 Server Error 500 Hosting in cpanel repost

I have posted a question yesterday here in relation with hosting a laravel project in Cpanel.

I have tried all solutions that have been proposed in several topics in relation with this problem by still having the same error.

I am sorry for republishing this just trying to get another advices & propositions maybe I have published my question in an inappropriate hour.

I can publish the content of any file you want if you think that the problem may come somewhere else.



via Chebli Mohamed

Laravel eager loading error "Trying to get property of non-object in .../BelongsToMany.php"

Laravel 5.8

I'm having an issue getting eager loading to work on some models but not on others.

Using artisan tinker I can run;

$p = App\Programme::find(34)->reviews

and get the correct result. If I change this to;

$p = App\Programme::with('reviews')->find(34)

So that the reviews are eager loaded, it fails with the error

PHP Notice:  Trying to get property of non-object in .../vendor/laravel/framework/src/Illuminate/Database/Eloquent/Relations/BelongsToMany.php on line 301

output from artisan with query log, bindings and execute time

$p = App\Programme::with('destinations', 'reviews')->find(34)

"select * from `programmes` where `programmes`.`id` = ? and `programmes`.`deleted_at` is null limit 1"
array:1 [
  0 => 34
]
1.08


"select `destinations`.*, `programme_destination`.`programme_id` as `pivot_programme_id`, `programme_destination`.`destination_id` as `pivot_destination_id`, `programme_destination`.`created_at` as `pivot_created_at`, `programme_destination`.`updated_at` as `pivot_updated_at` from `destinations` inner join `programme_destination` on `destinations`.`id` = `programme_destination`.`destination_id` where `programme_destination`.`programme_id` in (34)"
[]
0.88


"select `reviews`.*, `programme_reviews`.`programme_id` as `pivot_programme_id`, `programme_reviews`.`review_id` as `pivot_review_id`, `programme_reviews`.`created_at` as `pivot_created_at`, `programme_reviews`.`updated_at` as `pivot_updated_at` from `reviews` inner join `programme_reviews` on `reviews`.`id` = `programme_reviews`.`review_id` where `programme_reviews`.`programme_id` in (34)"
[]
0.85

The final query if run manually works just fine.

I can run the exact same two commands using either the User or Destination models and get a successful response. So there must be something different about the relationship of $programme->reviews when compared to $programme->user or $programme->destinations

Here are my models (trimmed to the relevant functions);

App\BaseModel

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use OwenIt\Auditing\Contracts\Auditable;
use Log;
use DB;

class BaseModel extends Model
{
    protected $guarded = ['alias', 'created_at', 'updated_at', 'deleted_at', 'slug'];

    public $custom_attributes = [];
    public $index_attributes = ['alias', 'user'];


    public function alias()
    {
        return $this->morphOne('App\Alias', 'aliased');
    }

    public function user()
    {
        return $this->belongsTo('App\User');
    }

}

App\Programme

<?php

namespace App;

use App\BaseModel;
use Illuminate\Database\Eloquent\SoftDeletes;
use OwenIt\Auditing\Contracts\Auditable;
use Log;

class Programme extends BaseModel implements Auditable
{
    use SoftDeletes;
    use \OwenIt\Auditing\Auditable;


    protected $table = 'programmes';

    protected $dates = ['deleted_at'];


    function __construct(array $attributes = array())
    {
        parent::__construct($attributes);
    }

    public function destinations()
    {
        return $this->belongsToMany('App\Destination', 'programme_destination')
            ->withTrashed()
            ->withTimestamps();
    }

    public function reviews()
    {
        return $this->belongsToMany('App\Review', 'programme_reviews')
            ->withTrashed()
            ->withTimestamps();
    }
}

App\Review

<?php

namespace App;

use App\BaseModel;
use Illuminate\Database\Eloquent\SoftDeletes;
use OwenIt\Auditing\Contracts\Auditable;
use Log;

class Review extends BaseModel implements Auditable
{
    use SoftDeletes;
    use \OwenIt\Auditing\Auditable;

    protected $fillable = ['title', 'name', 'first_name', 'last_name', 'review_type', 'email_address', 'created_at'];


    function __construct(array $attributes = array())
    {
        parent::__construct($attributes);
    }

    public function programmes()
    {
        return $this->belongsToMany('App\Programme', 'programme_reviews')
            ->withTrashed()
            ->withTimestamps();
    }
}

I can run $p = App\Programme::with('destinations', 'alias')->find(34) successfully. Here's the model for destinations

App\Destination

<?php

namespace App;

use App\BaseModel;
use Illuminate\Database\Eloquent\SoftDeletes;
use OwenIt\Auditing\Contracts\Auditable;


class Destination extends BaseModel implements Auditable
{
    use SoftDeletes;
    use \OwenIt\Auditing\Auditable;


    protected $table = 'destinations';

    protected $dates = ['deleted_at'];


    function __construct(array $attributes = array())
    {
        parent::__construct($attributes);
    }

    public function programmes()
    {
        return $this->belongsToMany('App\Programme', 'programme_destination')
            ->withTrashed()
            ->withTimestamps();
    }
}

It seems the relationship works based on the first artisan command, so why does this not work when eager loading?

For reference here are the database create codes;

programmes

CREATE TABLE `programmes` (
    `id` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,
    `name` VARCHAR(191) NOT NULL COLLATE 'utf8_unicode_ci',
    `destination_id` INT(10) UNSIGNED NULL DEFAULT NULL,
    `user_id` INT(10) UNSIGNED NOT NULL DEFAULT '1',
    `created_at` TIMESTAMP NULL DEFAULT NULL,
    `updated_at` TIMESTAMP NULL DEFAULT NULL,
    `deleted_at` TIMESTAMP NULL DEFAULT NULL,
    PRIMARY KEY (`id`),
    INDEX `programmes_user_id_foreign` (`user_id`),
    INDEX `programmes_destination_id_foreign` (`destination_id`),
    CONSTRAINT `programmes_destination_id_foreign` FOREIGN KEY (`destination_id`) REFERENCES `destinations` (`id`),
    CONSTRAINT `programmes_user_id_foreign` FOREIGN KEY (`user_id`) REFERENCES `users` (`id`)
)
COLLATE='utf8_unicode_ci'
ENGINE=InnoDB
;

reviews

CREATE TABLE `reviews` (
    `id` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,
    `title` VARCHAR(191) NULL DEFAULT NULL COLLATE 'utf8_unicode_ci',
    `name` VARCHAR(191) NOT NULL COLLATE 'utf8_unicode_ci',
    `first_name` VARCHAR(191) NULL DEFAULT NULL COLLATE 'utf8_unicode_ci',
    `last_name` VARCHAR(191) NULL DEFAULT NULL COLLATE 'utf8_unicode_ci',
    `email_address` VARCHAR(191) NULL DEFAULT NULL COLLATE 'utf8_unicode_ci',
    `created_at` TIMESTAMP NULL DEFAULT NULL,
    `updated_at` TIMESTAMP NULL DEFAULT NULL,
    `deleted_at` TIMESTAMP NULL DEFAULT NULL,
    PRIMARY KEY (`id`)
)
COLLATE='utf8_unicode_ci'
ENGINE=InnoDB
;

programme_reviews - many to many

CREATE TABLE `programme_reviews` (
    `id` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,
    `programme_id` INT(10) UNSIGNED NOT NULL,
    `review_id` INT(10) UNSIGNED NOT NULL,
    `created_at` TIMESTAMP NULL DEFAULT NULL,
    `updated_at` TIMESTAMP NULL DEFAULT NULL,
    PRIMARY KEY (`id`),
    INDEX `programme_reviews_review_id_foreign` (`review_id`),
    INDEX `programme_id` (`programme_id`),
    CONSTRAINT `programme_reviews_programme_id_foreign` FOREIGN KEY (`programme_id`) REFERENCES `programmes` (`id`) ON UPDATE CASCADE ON DELETE CASCADE,
    CONSTRAINT `programme_reviews_review_id_foreign` FOREIGN KEY (`review_id`) REFERENCES `reviews` (`id`) ON UPDATE CASCADE ON DELETE CASCADE
)
COLLATE='utf8_unicode_ci'
ENGINE=InnoDB
;

destinations

CREATE TABLE `destinations` (
    `id` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,
    `name` VARCHAR(191) NOT NULL COLLATE 'utf8_unicode_ci',
    `user_id` INT(10) UNSIGNED NOT NULL DEFAULT '1',
    `created_at` TIMESTAMP NULL DEFAULT NULL,
    `updated_at` TIMESTAMP NULL DEFAULT NULL,
    `deleted_at` TIMESTAMP NULL DEFAULT NULL,
    PRIMARY KEY (`id`),
    INDEX `destinations_parent_id_index` (`parent_id`),
    INDEX `destinations_user_id_foreign` (`user_id`),
    CONSTRAINT `destinations_user_id_foreign` FOREIGN KEY (`user_id`) REFERENCES `users` (`id`)
)
COLLATE='utf8_unicode_ci'
ENGINE=InnoDB
;

programme_destinations one to many

CREATE TABLE `programme_destination` (
    `id` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,
    `programme_id` INT(10) UNSIGNED NOT NULL,
    `destination_id` INT(10) UNSIGNED NOT NULL,
    `created_at` TIMESTAMP NULL DEFAULT NULL,
    `updated_at` TIMESTAMP NULL DEFAULT NULL,
    PRIMARY KEY (`id`),
    INDEX `programme_destination_programme_id_foreign` (`programme_id`),
    INDEX `programme_destination_destination_id_foreign` (`destination_id`),
    CONSTRAINT `programme_destination_destination_id_foreign` FOREIGN KEY (`destination_id`) REFERENCES `destinations` (`id`) ON UPDATE CASCADE ON DELETE CASCADE,
    CONSTRAINT `programme_destination_programme_id_foreign` FOREIGN KEY (`programme_id`) REFERENCES `programmes` (`id`) ON UPDATE CASCADE ON DELETE CASCADE
)
COLLATE='utf8_unicode_ci'
ENGINE=InnoDB
;


The only real difference between $programme->destinations and $programme->reviews is that reviews is a many to many relationship.



via Chebli Mohamed

foreign key is not working when I migrate

Foreign key is not working.

This is my migration file

Schema::create('course_prospect', function (Blueprint $table) {
      $table->bigIncrements('id');

      $table->integer('prospect_id')->length(11)->unsigned();
      $table->foreign('prospect_id')->references('id')->on('prospect');

      $table->integer('course_id')->length(11)->unsigned();
      $table->foreign('course_id')->references('course_id')->on('course');

      $table->timestamps();
    });

I'm getting this error

 Illuminate\Database\QueryException  : SQLSTATE[HY000]: General error: 
1005 Can't create table `customerinquirydb`.`#sql-104c_e9` (errno: 150 
"Foreign key constraint is incorrectly formed") (SQL: alter table 
`course_prospect` add constraint `course_prospect_prospect_id_foreign` 
foreign key (`prospect_id`) references `prospect` (`id`))



via Chebli Mohamed

Sql whereNOTIn in laravel

I need your support to use whereNOTIn with laravel 5 to show all id in (times) table and they not in (booking)table and user_id

 $bookappoitm=DB::table('times')
    ->where('times.Dates_id',$id)
    ->whereNOTIn('id',function($query){
           $query->select('times_id')->from('bookappoitments')
           ->where('users_id',Auth::user()->id)
           ->get();
        })->get();



via Chebli Mohamed

Laravel eloquent get model property of current query

I'm trying to do where clause for fortune_code inside joindraw table, comparing with the lucky_fortune_code from product table. How can i access and do the check?

Product::where('status', StatusConstant::PT_ENDED_PUBLISHED)
       ->where('lucky_fortune_code', '<>', '')
       ->with(['joindraw' => function ($query){
              $query->where('fortune_code', $this->lucky_fortune_code)
                    ->with('user');}])->desc()->get();



via Chebli Mohamed

how to write variable in {!! in laravel

I am using ternary operator. here is my code :

<td>
        {!! $cooking->deleted_at ?
        '<button type="button" data-id=""
            data-target="#restore-CF">Enable</button>'
        :'<button type="button" data-id=""
          data-target="#exampleModal-3">Disable</button>'!!}
</td>

When I inspect, in data-id="" shows <?php echo e($cooking->id); ?>, but its value is in number i.e 12. How to fix this??? I would be grateful to your help



via Chebli Mohamed

failed POST request in postman with Laravel 5.7

I am using postman to send post request as json file to the tasks table. this is My api.php route

Route::post('/tasks', 'TaskController@create'); 

and TaskController

public function create(Request $request)
    {
        $rules = [
            'name' => 'required',

            'owner' => 'required'
        ];

        $validate = Validator::make($request->all(), $rules);
        if ($validate->fails()) {
            return $validate->errors();
        }

        return Task::create([
            'name' => $request->title,

            'owner' => $request->status
        ]);
    }

and I have following url in postman http://localhost:8000/api/tasks with POST request and under the ‘Body’ tab choose the ‘raw’ radio button and then ‘JSON (application/json)’ from the drop down. and send data body in postman is like this.

{ 
"name": "sara",
"owner": "mala" 
}

but when send data it is not saving to the table and occured following error massage. SQLSTATE[23000]: Integrity constraint violation: 1048 Colum 'name' cannot be null how can fix this problem?



via Chebli Mohamed