Create Backup controller in Laravel - php

I am not experienced in programming. I am trying to create a controller in laravel that backups tables. The tables are backed up correctly, but I've received some remarks that my code can easily be broken. I've been trying to do my best, but I'm really exhausted and getting frustrated. I have a table, and each row in the table takes from 1 to 10 MB of memory. Total rows in table 100. The maximum amount of memory (memory_limit) is set to 35 MB. What would be the best way solve this problem? I really appreciate any suggestion or help. This is my code:
The BackupController class
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Storage;
use Illuminate\Database\Events\StatementPrepared; // set the fetch mode
use Illuminate\Support\Facades\Cache;
use Carbon\Carbon;
use Illuminate\Support\Facades\Schema;
use DB;
use App\Post;
use App\User;
use App\Jobs\BackupTableJob;
use Artisan;
class BackupController extends Controller
{
const MAX_VALUE = 35000; //kilobytes
public function lockTable()
{
// lock all tables
DB::unprepared('FLUSH TABLES WITH READ LOCK;');
}
public function setQueueJob(User $user, Post $post)
{
BackupTableJob::dispatch($user, $post)
->delay(Carbon::now()
->addSeconds(5));
// Artisan::call('queue:work');
}
public function unlockTable()
{
// unlock all tables
DB::unprepared('UNLOCK TABLES');
}
public function queryFetch($data)
{
$pdo = DB::connection()->getPdo();
$stmt = $pdo->prepare($data);
$stmt->execute();
// $stmt = $pdo->query($data);
$results = $stmt->fetch();
return $results;
}
public function create(Request $request)
{
$this->setPdoMode();
$numTables = DB::select("SHOW TABLES");
$countUserRecords = User::count();
$countPostRecords = Post::count();
return view('backup', compact('numTables','countUserRecords', 'countPostRecords'));
}
public function setPdoMode()
{
\Event::listen(StatementPrepared::class, function($event) {
$event->statement->setFetchMode(\PDO::FETCH_ASSOC);});
}
// public function backup(Request $request, User $user, Post $post)
public function backup(Request $request, User $user, Post $post)
{
$this->setQueueJob($user, $post);
if ($request->all()) {
$tables = request('table');
$output = '';
foreach ($tables as $key => $table) {
$this->lockTable();
$show_table_query = $this->queryFetch("SHOW CREATE TABLE {$table}");
$output .="\n" . $show_table_query[1] . ";\n";
$this->setPdoMode();
$single_result = DB::select("SELECT * FROM {$table}");
$output .= $this->getTableData($single_result, $table);
$output .= $this->cacheData($table, $output);
}
if ($this->checkFileSize($output)) {
return redirect()->route('create');
}
}
return redirect()->route('backupError');
}
// Stores the file in this location: storage/app
public function download($output)
{
$dt = Carbon::now();
$file_name = 'backup_on[' . $dt->format('y-m-d H-i-s') . '].sql';
Storage::disk('local')->put($file_name, $output);
}
public function getTableData($single_result, $table)
{
$this->unlockTable();
$output = '';
foreach ($single_result as $key => $table_val) {
if ($table === "posts" || $table === "users") {
$output .= "\nINSERT INTO $table(";
$output .= "" .addslashes(implode(", ", array_keys($table_val))) . ") VALUES(";
$output .= "'" . addslashes(implode("','", array_values($table_val))) . "');\n";
}
}
// $output .= $this->cacheData($table, $output);
return $output;
}
public function checkFileSize($file)
{
$file_size = strlen($file);
// convert bytes to kilobytes
$file_size = round($file_size / 1024, 0, PHP_ROUND_HALF_UP);
if ($file_size <= self::MAX_VALUE) {
$this->download($file);
return true;
}
return false;
}
public function cacheData($table, $data)
{
// $table = $table;
$start = microtime(true);
$data = Cache::remember('table', 10, function() use ($table){
return DB::table($table)->get();
});
$duration = (microtime(true) -$start) * 1000;
\Log::info("From cache: " . $duration .' ms');
return $data;
}
}
BackupTableJob class
<?php
namespace App\Jobs;
use App\User;
use App\Post;
use App\Http\Controllers\BackupController;
use Illuminate\Http\Request;
use Illuminate\Bus\Queueable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
class BackupTableJob implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable; //SerializesModels;
protected $user;
protected $post;
/**
* Create a new job instance.
*
* #return void
*/
public function __construct(User $user, Post $post)
// public function __construct()
{
$this->user = $user;
$this->post = $post;
}
/**
* Execute the job.
*
* #return void
*/
public function handle()
{
// $this->user->save();
// $this->post->save();
}
}

You don't need to process that data with PHP. You can simply use raw SQL queries in laravel:
SELECT *
INTO tableToBeBackedUp
FROM currentTable;

Related

Laravel Model is freezing the server. Timeout error

I'm having a really strange issue here. I have a user model (detailed below).
It all works fine until I added the getReportsSharedAttribute function. When this is added, the server freezes and I get:
PHP Fatal error: Maximum execution time of 60 seconds exceeded in C:\Users\User\PhpstormProjects\laravel-vue\vendor\laravel\framework\src\Illuminate\Database\Eloquent\Concerns\HasRelationships.php on line 637
more:
exception: "Symfony\\Component\\ErrorHandler\\Error\\FatalError"
file: "C:\\Users\\User\\PhpstormProjects\\laravel-vue\\vendor\\laravel\\framework\\src\\Illuminate\\Database\\Eloquent\\Concerns\\HasAttributes.php"
I thought there was something up with the code, so I ran it manually in a controller and dumped it, it worked fine.
So I tried it as a relation instead of an attribute. Same error.
So then I thought, is it just specific to the ReportingSetAssigned model, so I did another query on another collection, and another, and I still get the timeout error.
I tried another Model, it worked fine, for no apparent reason. Even though there were a lot more records inside. It doesn't seem to be dependant on how many columns are involved in the return. None of my tables have more than 50 records inside, even in the relations.
What's going on here? Is there some limit somewhere?
<?php
namespace App\Models;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Query\Builder;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Illuminate\Support\Facades\DB;
use Laravel\Sanctum\HasApiTokens;
use stdClass;
use Illuminate\Database\Eloquent\SoftDeletes;
class User extends Authenticatable
{
use HasApiTokens, HasFactory, Notifiable, SoftDeletes;
public $appends = [
'full_name',
'profile_photo_thumb',
'permissions_alt',
'line_managed_only_id',
'line_managers_only_id',
'permissions_meetings_only_id',
'reports_shared',
];
/**
* The attributes that are mass assignable.
*
* #var string[]
*/
protected $fillable = [
'name',
'email',
'password',
];
/**
* The attributes that should be hidden for serialization.
*
* #var array
*/
protected $hidden = [
'password',
'remember_token',
];
/**
* The attributes that should be cast.
*
* #var array
*/
protected $casts = [
'email_verified_at' => 'datetime',
];
protected $dates = ['deleted_at'];
public function permissions(){
return $this->belongsToMany(Permission::class);
}
public function timelineitems(){
return $this->hasMany(TimelineItem::class);
}
public function line_managers(){
return $this->belongsToMany(User::class,'permissions_lm','user_id','lm_id');
}
public function line_managed(){
return $this->belongsToMany(User::class,'permissions_lm','lm_id','user_id');
}
public function permissions_meetings(){
return $this->belongsToMany(Area::class,'permissions_meetings','user_id','area_id')->withPivot('level');
}
public function getPermissionsMeetingsOnlyIdAttribute(){
return $this->permissions_meetings()->pluck('permissions_meetings.area_id');
}
public function permissions_qed(){
return $this->belongsToMany(Area::class,'permissions_qed','user_id','area_id')->withPivot('level');
}
public function permissions_reporting(){
return $this->belongsToMany(Area::class,'permissions_reporting','user_id','area_id')->withPivot('level');
}
public function permissions_reporting_sets(){
return $this->belongsToMany(ReportingSet::class,'permissions_reporting_sets','user_id','set_id')->withPivot('level');
}
public function improvement_category_objective_action_milestones(){
return $this->belongsToMany(ImprovementSetCategoryObjectiveActionMilestone::class);
}
public function planning_review_forms(){
return $this->hasMany(PerformanceManagementSetAssigned::class)->whereHas('set', function($q) {
$q->where('appraisal', 0);
});
}
public function appraisal_forms(){
return $this->hasMany(PerformanceManagementSetAssigned::class)->whereHas('set', function($q) {
$q->where('appraisal', 1);
});
}
public function performance_manager_set_as_lm(){
return $this->belongsTo(PerformanceManagementSetAssigned::class, 'lm_id');
}
public function getPermissionsAttribute(){
return $this->permissions()->get();
}
public function getLineManagersOnlyIdAttribute(){
return $this->line_managers()->pluck('permissions_lm.lm_id');
}
public function getLineManagedOnlyIdAttribute(){
return $this->line_managed()->pluck('permissions_lm.user_id');
}
public function hasPermissionTo($permission){
if(auth()->user()->super_admin){
return true;
}
if($permission==='super_admin'&&auth()->user()->super_admin){
return true;
}
if(!is_array($permission)){
$access = $this->permissions()->where('permission', $permission)->exists();
if($access){
return true;
}
return false;
}else{
foreach($permission as $p){
$access = $this->permissions()->where('permission', $permission)->exists();
if($access){
return true;
}
}
}
}
public function checkPermissionReportingSet($permission){
$access = $this->permissions_reporting_sets()->where('set_id', $permission)->first();
if($access){
if($access->pivot->level=='read'){
return 'read';
}
if($access->pivot->level=='write'){
return 'write';
}
}
}
public function checkPermissionReportingArea($permission){
$access = $this->permissions_reporting()->where('area_id', $permission)->first();
if($access){
if($access->pivot->level=='true'){
return true;
}
}
}
public function truePermission($permission){
$access = $this->permissions()->where('permission', $permission)->exists();
if($access){
return true;
}
}
public function updateTimeline($type_main,$type_sub,$title,$content,$icon,$color,$link,$relevant_id = null,$user_id = null){
if(!$user_id){
$user_id = $this->id;
}
$t = new TimelineItem();
$t->type_main = $type_main;
$t->type_sub = $type_sub;
$t->title = $title;
$t->content = $content;
$t->icon = $icon;
$t->color = $color;
$t->link = $link;
$t->user_id = $user_id;
$t->relevant_id = $relevant_id;
$t->save();
}
public function getPermissionsForVueAttribute(){
$permissions = $this->permissions;
$new = [];
foreach($permissions as $p){
$new[$p->permission] = true;
}
$new['meeting_areas'] = [];
$permissions = $this->permissions_meetings;
foreach($permissions as $p){
$new['meeting_areas'][$p->id] = $p->pivot->level;
}
$new['qed_areas'] = [];
$permissions = $this->permissions_qed;
foreach($permissions as $p){
$new['qed_areas'][$p->id] = $p->pivot->level;
}
$new['reporting_areas'] = [];
$permissions = $this->permissions_reporting;
foreach($permissions as $p){
$new['reporting_areas'][$p->id] = $p->pivot->level;
}
$new['reporting_sets'] = [];
$permissions = $this->permissions_reporting_sets;
foreach($permissions as $p){
$new['reporting_sets'][$p->id] = $p->pivot->level;
}
return json_encode($new);
}
public function getPermissionsAltAttribute(){
//General permissions
$permissions = Permission::get();
$newP = [];
foreach($permissions as $p){
$newP[$p->permission] = false;
}
$permissions = $this->permissions;
foreach($permissions as $p){
$newP[$p->permission] = true;
}
$newP['meeting_areas'] = [];
$newP['qed_areas'] = [];
$newP['reporting_areas'] = [];
$newP['reporting_sets'] = [];
foreach(Area::orderBy('name', 'ASC')->get() as $p){
$newP['meeting_areas'][$p->id] = "false";
$newP['qed_areas'][$p->id] = "false";
$newP['reporting_areas'][$p->id] = "false";
}
$meetings = DB::table('permissions_meetings')->where('user_id', '=', $this->id)->get();
foreach($meetings as $p){
$newP['meeting_areas'][$p->area_id] = $p->level;
}
$qed = DB::table('permissions_qed')->where('user_id', '=', $this->id)->get();
foreach($qed as $p){
$newP['qed_areas'][$p->area_id] = $p->level;
}
$reporting = DB::table('permissions_reporting')->where('user_id', '=', $this->id)->get();
foreach($reporting as $p){
$newP['reporting_areas'][$p->area_id] = $p->level;
}
foreach(ReportingSet::orderBy('name', 'ASC')->get() as $p){
$newP['reporting_sets'][$p->id] = "false";
}
$reporting = DB::table('permissions_reporting_sets')->where('user_id', '=', $this->id)->get();
foreach($reporting as $p){
$newP['reporting_sets'][$p->set_id] = $p->level;
}
return $newP;
}
public function getCyclesAttribute(){
return Cycle::orderBy('id')->get();
}
public function getFullNameAttribute(){
return $this->first_name . " " . $this->last_name;
}
public function getProfilePhotoThumbAttribute(){
if($this->profile_photo){ return "THUMB-" . $this->profile_photo; }else{ return "no-avatar.png"; }
}
public function getReportsSharedAttribute(){
return ReportingSet::where('observee_id', $this->id)->where('observee_share', 1)->where('published', 1)->without('set.modules')->get()->toArray();
}
public function canLineManage($id){
if($this->super_admin==1) return true;
foreach($this->line_managed as $lm){
if($lm->id==$id){
return true;
}
}
}
}
EDIT: If I run this code in a controller, it does't hang at all. It loads up the data in less than a second
EDIT: Restarted computer, still happening
This looks a lot like an infinitive call-loop, please refer to this on github issue
Allowed Memory size exhaused when accessing undefined index in toArray
You are just running out of memory when calling parent::toArray(). You
either need to reduce the amount of items in your collection or
increase your allowed memory allocation.

Laravel/Eloquent collitions with model traits

I am using laravel 5.8 for my project, and i am getting the error Bad call Call to undefined method Illuminate\Database\Eloquent\Builder::withSum() because I removed LaravelSubQueryTrait in my traits. When i re-add LaravelSubQueryTrait, my project does not run, I am getting this error Trait method newBaseQueryBuilder has not been applied, because there are collisions with other trait methods on App\Traits\TestModelTrait. Upgrading to laravel 8.x is not an option at this point and I don't quite understand what I am doing wrong. Any tips/advise/ resources to read will be highly appreciated to help solve my problem. Thanks
My TestmodelTrait is as below :
use Alexmg86\LaravelSubQuery\Traits\LaravelSubQueryTrait;
use Carbon\Carbon;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Support\Str;
use Watson\Rememberable\Rememberable;
trait MedyqModelTrait
{
protected static $skipUuid = false;
use MedyqTrait, SoftDeletes, Rememberable, LaravelSubQueryTrait;
protected static function boot()
{
parent::boot();
self::creating(function ($record) {
if (!self::$skipUuid) {
$record->{'uuid'} = (string)Str::orderedUuid();
}
if (auth()->check()) {
$record->{'created_by'} = auth()->id();
$record->{'updated_by'} = auth()->id();
}
});
self::updating(function ($record) {
if (auth()->check()) {
$record->{'updated_by'} = auth()->id();
}
});
self::updated(function ($record) {
$record->flushCache();
});
self::deleted(function ($record) {
if (auth()->check()) {
$record->{'deleted_by'} = auth()->id();
$record->save();
}
});
self::restored(function ($record) {
if (auth()->check()) {
$record->{'restored_by'} = auth()->id();
$record->{'restored_at'} = Carbon::now();
$record->save();
}
});
}
/**
* Get the route key for the model.
*
* #return string
*/
public function getRouteKeyName()
{
return 'uuid';
}
public function scopeAlive($query)
{
$query->where("status", config("constants.status.alive"));
}
}
My TestTrait looks like this :
use App\Entities\User;
use Carbon\Carbon;
use Exception;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Str;
trait MedyqTrait
{
public static function getTableName()
{
return with(new static)->getTable();
}
public function scopeActive($query)
{
$query->where("status", config("constants.status.active"));
}
public function scopeInActive($query)
{
$query->where("status", config("constants.status.inactive"));
}
/**
* Mass (bulk) insert for Postgres Laravel 5
*
* insert([
* ['id'=>1,'value'=>10],
* ['id'=>2,'value'=>60]
* ]);
*
*
* #param array $rows
* #param null $table
* #param bool $skipUUid
* #param bool $timestamps
* #param bool $authorstamp
* #return bool
* #throws Exception
*/
public function bulkInsert(array $rows, $table = null, $skipUUid = false, $timestamps = true, $authorstamp = true)
{
$return_values = array_filter($rows, 'is_array');
if (!count($return_values) > 0) {
throw new Exception("This function expects a multidimensional associative array");
}
if (is_null($table)) {
$table = DB::getTablePrefix() . $this->getTable();
}
$rows = array_map(function ($item) use ($skipUUid, $timestamps, $authorstamp) {
$authenticated = auth()->check();
$authorId = auth()->id();
$time = Carbon::now();
if (!$skipUUid) {
$item['uuid'] = Str::orderedUuid();
}
if ($timestamps && !key_exists('created_by', $item) && $authenticated && $authorstamp) {
$item['created_by'] = $authorId;
}
if ($timestamps && !key_exists('updated_by', $item) && $authenticated && $authorstamp) {
$item['updated_by'] = $authorId;
}
if ($timestamps && !key_exists('created_at', $item)) {
$item['created_at'] = $time;
}
if ($timestamps && !key_exists('updated_at', $item)) {
$item['updated_at'] = $time;
}
return $item;
}, $rows);
$first = reset($rows);
$columns = implode(',',
array_map(function ($value) {
return "$value";
}, array_keys($first))
);
$values = implode(',', array_map(function ($row) {
return '(' . implode(',',
array_map(function ($value) {
return "'" . str_replace("'", "''", $value) . "'";
}, $row)
) . ')';
}, $rows)
);
$sql = "INSERT INTO {$table}({$columns}) VALUES {$values}";
return DB::statement($sql);
}
public function deletedBy()
{
return $this->belongsTo(User::class, "deleted_by", "id");
}
}
Guess you have problem with the syntax.
$invoice = Invoice::where('id', $invoice->id)
->with('items')
->withSum('items', 'total_amount')
->withSum('items', 'amount_paid')
->first();
withSum is probably available only since Laravel version 8.x, before that I think only withCount was available. But if you still want to use withSum in earlier version you can define a macro'
You could also try use LaravelSubQueryTrait trait directly in the model/models.
use Alexmg86\LaravelSubQuery\Traits\LaravelSubQueryTrait;
use Illuminate\Database\Eloquent\Model;
class Invoice extends Model
{
use LaravelSubQueryTrait;
Reference https://github.com/Alexmg86/laravel-sub-query

Laravel- getting i think POST message after set_date submit

Hello i have a form which sets the date of voting start and stop, today I started to get this information on my screen. Could anyone tell me what does it mean ?
This functionality uses 2 php files.
MakeVoteController in which i take the date from form and then do Carbon::create and put them into database and there's function in my VotingStatus model. It is checking if the current date is in between begin and end date then it returns voting_status(started or stopped)
VOTINGMGMT CONTROLLER
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use DB;
use App\Http\Requests;
use Illuminate\Support\Facades\Input;
use Carbon\Carbon;
class VotingMgmtController extends Controller
{
public function start()
{
self::setStart();
return view('panel.startvoting');
}
public function stop()
{
self::setStop();
return view('panel.stopvoting');
} //
public function setDateView()
{
return view('panel.startvoting');
}
public function setDate(Request $request)
{
$rok_start = Input::get('rok');
$miesiac_start = Input::get('miesiac');
$dzien_start = Input::get('dzien');
$godzina_start = Input::get('godzina');
$minuta_start = Input::get('minuta');
$rok_stop = Input::get('rok_end');
$miesiac_stop = Input::get('miesiac_end');
$dzien_stop = Input::get('dzien_end');
$godzina_stop = Input::get('godzina_end');
$minuta_stop = Input::get('minuta_end');
$begin_date = Carbon::create($rok_start,$miesiac_start,$dzien_start,$godzina_start,$minuta_start,59,'Europe/Warsaw');
$stop_date = Carbon::create($rok_stop,$miesiac_stop,$dzien_stop,$godzina_stop,$minuta_stop,59,'Europe/Warsaw');
$now = Carbon::now('Europe/Warsaw');
//Set begin and end date in database
DB::table('voting_status')
->where('id',1)
->update(['voting_start_date' => $begin_date]);
DB::table('voting_status')
->where('id',1)
->update(['voting_end_date' => $stop_date]);
return redirect()->route('set_date')->with('success','Ustawiono datę rozpoczęcia i zakończenia głosowania');
}
public function setEndDate()
{
}
private function setStart()
{
try
{
DB::table('voting_status')
->where('id',1)
->update(['status' => 'started']);
}
catch(\Illuminate\Database\QueryException $ex)
{
return view('info.dash_service_unavailable');
}
}
private function setStop()
{
try
{
DB::table('voting_status')
->where('id',1)
->update(['status' => 'stopped']);
}
catch(\Illuminate\Database\QueryException $ex)
{
return view('info.dash_service_unavailable');
}
return true;
}
private function checkDate()
{
}
}
VOTINGSTATUS MODEL
<?php
namespace App;
use DB;
use PDO;
use Illuminate\Database\Eloquent\Model;
use Carbon\Carbon;
class VotingStatus extends Model
{
protected $table = "voting_status";
//check table VotingStatus whether started or not
function checkStatus()
{
/*query database about status of voting and
print output */
DB::setFetchMode(PDO::FETCH_ASSOC);
$begin_date = DB::select('select voting_start_date from voting_status where id=1 ');
$end_date = DB::select('select voting_end_date from voting_status where id=1');
$now = Carbon::now('Europe/Warsaw');
$begin_var;
$end_var;
foreach($begin_date as $key => $value)
{
$begin_var= (string)$value['voting_start_date'];
echo $begin_var;
}
foreach($end_date as $key => $value)
{
$end_var= (string)$value['voting_end_date'];
echo $end_var;
}
$carbon_start = Carbon::parse($begin_var,'Europe/Warsaw');
$carbon_stop = Carbon::parse($end_var,'Europe/Warsaw');
if(($now->gt($carbon_start)) && ($now->lt($carbon_stop)))
{
try
{
DB::table('voting_status')
->where('id',1)
->update(['status' => 'started']);
}
catch(\Illuminate\Database\QueryException $ex)
{
dd("Upss start");
}
}
else
{
try
{
DB::table('voting_status')
->where('id',1)
->update(['status' => 'stopped']);
}
catch(\Illuminate\Database\QueryException $ex)
{
dd("Upss stop");
}
}
DB::setFetchMode(PDO::FETCH_CLASS);
$db_stat = DB::table('voting_status')->where('id',1)->first();
$status = $db_stat->status;
return $status;
}
}
FORM
Error has been fixed. After uploading newer version of application on the server there still was old version of web.php. In mentioned web.php my form submit was handled by function
set_date(Request $request)
{
return $request;
}
Now everything works

Code called in schedular deletes the records in table but doesnt add

My controller kernel is like this
protected function schedule(Schedule $schedule)
{
$schedule->call('\App\Http\Controllers\HomeController#automatic')->everyMinute();
}
When i call the controller
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use DB;
use Illuminate\Support\Facades\Storage;
use App\news;
use Auth;
use DOMDocument;
use Exception;
class HomeController extends Controller
{
public function automatic()
{
function delete(){
DB::table('news')->delete();
echo "table deletted";
}
delete();
}
}
It deletes the records in the table. But when i call controller having this code
class HomeController extends Controller
{
public function automatic()
{
function follow_links_reportersnepal($url,$cat)
{
ini_set('max_execution_time', 9000);
global $already_crawled;
global $crawling;
$i=0;
$doc = new DOMDocument();
#$doc->loadHTML(#file_get_contents($url));
$linklist = $doc->getElementsByTagName("a");
$already_crawled[]="sailaab";
foreach ($linklist as $link)
{
try {
$l = $link->getAttribute("href");
if(strlen($l)==45)
{
if (!in_array($l, $already_crawled))
{
$i++;
if ($i>2) {
break;
}
$already_crawled[] = $l;
$content = file_get_contents($l);
$first_step = explode( '<h3 class="display-4">' , $content);
$second_step = explode('</h3>' , $first_step[1]);//title
$third_step=explode('<div class="entry-content">',$second_step[1]);
$fourth_step=explode('<p>',$third_step[1]);
$fifth_step=explode('<div class="at-below-post', $fourth_step[1]);
$sixth_step=explode('<figure class="figure">', $content);
if(isset($sixth_step[1])){
$seventh_step=explode('src="', $sixth_step[1]);
$eighth_step=explode('"', $seventh_step[1]);
$url = $eighth_step[0];
$img=rand();
$img=(string)$img;
file_put_contents($img, file_get_contents($url));
$user = Auth::user();
news::create([
'news_title'=>strip_tags($second_step[0]),
'category_id'=>$cat,
'source_id'=>'reportersnepal',
'reference_url'=>"www.reportersnepal.com",
'reference_detail'=>$l,
'news_summary'=>"null",
'news_detail'=>strip_tags($fifth_step[0]),
'news_image'=>$img,
'news_video'=>"null",
'news_status'=>"1",
'created_by'=>$user->id,
'last_updated_by'=>$user->id,
]);
}
else{
$user = Auth::user();
news::create([
'news_title'=>strip_tags($second_step[0]),
'category_id'=>$cat,
'source_id'=>'reportersnepal',
'reference_url'=>"www.reportersnepal.com",
'reference_detail'=>$l,
'news_summary'=>"null",
'news_detail'=>strip_tags($fifth_step[0]),
'news_image'=>"default.png",
'news_video'=>"null",
'news_status'=>"1",
'created_by'=>$user->id,
'last_updated_by'=>$user->id,
]);
}
}
}
} catch (Exception $e) {
continue;
}
}
}
follow_links_reportersnepal('http://reportersnepal.com/category/featured','1');
}
}
It doesnt write anything in my database table. When i echo the variables it dispays the data.
This code works fine when i call them manually.
And my cron tab is
php-cli -q /home/allnewsnepal/public_html/artisan schedule:run
Laravel scheduler are command line based and you cannot use session and Auth components there
Your code below dont make any sense here
$user = Auth::user();
You need to store user information on some other in memory database like redis and then use it

Laravel one to many relationship insert data

I have two tables notification and alerFrequency. they have a one to many relationships respectively. the notification_id is a foreign key in the alerFrequency table. Both tables have models. now what I am trying to do is, to automatically insert data into the alertFrequency table if website is add in the notification table. this is the notification table
<?php
namespace App;
use App\Status;
use App\Notification;
use App\AlertFrequency;
use Illuminate\Database\Eloquent\Model;
class Notification extends Model
{
protected $fillable = ['id','website_url','email','slack_channel','check_frequency','alert_frequency','speed_frequency','active'];
public function statuses(){
return $this->belongsToMany('App\Status')->withPivot('values')->withTimestamps();
}
public function alertFrequencies(){
return $this->hasMany('App\AlertFrequency');
}
public function alert(){
$alert_timestamp = AlertFrequency::with('notification')->orderBy('created_at','desc')->select('created_at')->first();
$alert_timestamp=$alert_timestamp->created_at->toDateTimeString();
if($alert_timestamp==null){
return false;
}
return $alert_timestamp; }
and in the guzzle controller, I am using 3 functions: the add function to add a new alertFrequency into the table (which is not working at all) and the I called it in the sendnotification function, so that if it is time to send notification, it will add a new created_at in the alerFrequency table. Here is the guzzle controller
<?php
namespace App\Http\Controllers;
use \GuzzleHttp\Client;
use App\Utilities\Reporter;
use GuzzleHttp\Exception\ClientException;
use App\Notification;
use App\Status;
use App\Setting;
use Carbon;
use App\AlertFrequency;
class GuzzleController extends Controller
{
private $default_check_frequency;
protected $client;
protected $reporter;
public function __construct()
{
$this->client = new Client();
$this->reporter = new Reporter;
$this->default_check_frequency = Setting::defaultCheckFrequency();
}
private function addStatusToNotification(Notification $notification, Status $status, $resCode)
{
$notification->statuses()->attach($status, [
'values' => strval($resCode)
]);
}
/*function to add new time stamp into the alertFrequency table*/
private function add(Notification $notification, AlertFrequency $alert){
$notification->alertFrequency()->save();
}
private function report(Notification $notification, $resCode)
{
if(empty($resCode)){
$resCode = "no response found";
}
$status = Notification::health($resCode);
$this->reporter->slack($notification->website_url . ':' . ' is '. $status . ' this is the status code!' . ' #- ' .$resCode, $notification->slack_channel);
$this->reporter->mail($notification->email,$notification->website_url.' is '. $status . ' this is the status Code: '. $resCode);
}
private function sendNotification(Notification $notification, $status_health, $alert_frequency, $resCode,$alert)
{
echo "elpse time alert";
var_dump(\Carbon\Carbon::parse($alert)->diffInMinutes());
// If this is the first time we check, OR if the status changed from up to down and vice versa, notify!!!
if (empty($status_health['timestamp']) || Notification::health($resCode) <> Notification::health($status_health['value'])){
$this->report($notification,$resCode);
return;
}
// If the website is (still) down and the alert frequency is exceeded, notify!!!
if(Notification::health($resCode) === 'down' && \Carbon\Carbon::parse($alert)->diffInMinutes() >= $alert_frequency){
$this->report($notification,$resCode);
$this->add($notification,$alert);
}
}
public function status()
{
$notifications = Notification::where('active', 1)->get();
//$alert = AlertFrequency::
$status = Status::where('name', 'health')->first();
foreach ($notifications as $notification) {
$frequency = $this->updateStatus($notification, $status);
if (!empty($frequency)) {
$notification->alertFrequencies()->create([
'notification_id' => $frequency
]);
}
}
}
private function updateStatus(Notification $notification, Status $status)
{
$status_health = $notification->status('health');
$check = empty($status_health['timestamp']);
$elapsed_time = $check ? 10000 : \Carbon\Carbon::parse($status_health['timestamp'])->diffInMinutes();
$check_frequency = $this->getCheckFrequency($notification);
/* create an attachemtn in to the alerFrequenct table*/
$alert = $notification->alert();
var_dump($alert);
if ($check || $elapsed_time >= $check_frequency) {
$resCode = $this->getStatusCode($notification->website_url);
$this->addStatusToNotification($notification, $status, $resCode);
$this->sendNotification(
$notification,
$status_health,
$this->getAlertFrequency($notification),
$resCode,
$alert
);
}
}
private function getCheckFrequency(Notification $notification)
{
return isset($notification->check_frequency)
? intval($notification->check_frequency)
: $this->default_check_frequency;
}
private function getAlertFrequency(Notification $notification)
{
return isset($notification->alert_frequency)
? intval($notification->alert_frequency)
: $this->default_check_frequency;
}
private function getStatusCode($url)
{
try {
$response = $this->client->get($url, [
'http_errors' => false
]);
return $response->getStatusCode();
} catch (\GuzzleHttp\Exception\ConnectException $e) {
}
}
}

Categories