Laravel 5.2 soft delete does not work - laravel

I am have simple app with a table post and model:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
use SoftDeletes;
class Post extends Model
{
protected $table = 'post';
protected $dates = ['deleted_at'];
protected $softDelete = true;
}
I am trying to make example of soft delete and i am using route just for example route.php:
<?php
use App\Post;
use Illuminate\Database\Eloquent\SoftDeletes;
Route::get('/delete', function(){
$post = new Post();
Post::find(12)->delete();
});
I have a column "created_at" created with migration:
Schema::table('post', function (Blueprint $table) {
$table->softDeletes();
});
, but instead of adding time to this column, when I run the site it deletes the row with selected id. Where am I wrong ?

You need to use SoftDeletes trait inside your model like so:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
class Post extends Model
{
use SoftDeletes;
protected $table = 'post';
protected $dates = ['deleted_at'];
}
Now you, are not applying trait, so obviously it doesn't work.
In addition you have unnecessary piece of code in your routes file. It should look like this:
<?php
use App\Post;
Route::get('/delete', function(){
Post::find(12)->delete();
});

Related

Voyager get records from another database and show in table. (customized view)

There are two databse one is used for voyager and another is exiting database which filled data, I have to show in table form same as voyager do, I am newby and searched about to connect another database in voyager.
Customized controller to show listinf of domains
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
// use Illuminate\Routing\Controller as BaseController;
use TCG\Voyager\Http\Controllers\VoyagerBaseController as BaseController;
use TCG\Voyager\Facades\Voyager;
use App\Models\Domain;
class DomainController extends BaseController {
//
public function domainListing(){
dd(Domain::limit(5)->get()->toArray());
$data['abc'] = 'test';
return view('domain-list');
}
}
Routing file where I have wrote routing which is showing correctly,
Route::group(['prefix' => 'admin'], function () {
Voyager::routes();
Route::get('domains', ['uses' => 'App\Http\Controllers\DomainController#domainListing', 'as' => 'domains']);
});
Domain Model from another database
<?php
namespace App\Models;
use Carbon\Carbon;
use Illuminate\Database\Eloquent\Model;
class Domain extends Model {
protected $connection = 'mysql-connection2';
protected $table = 'domain';
protected $guarded=[];
public $timestamps = true;

Laravel Join Query return Invalid Object Name

I have two Models (Color and ColorFamily Table).I would like to join two table.When I join Second Table (ColorFamily),its shows an error message "Invalid Object Name ColorFamily".If I use to Join First Table then its shows an error message "Invalid Object Name Color".
Note:-
If I use Individual Query (Select Query) without join ,then the records is showing,that means both the tables are available...
I have check with google but can not find the solution.....
Color Models:-
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class Color extends Model
{
protected $primaryKey = 'ColorId';
protected $table = 'Common_Ref_Color';
public $timestamps = false;
}
ColorFamily Models:-
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class ColorFamily extends Model
{
protected $primaryKey = 'ColorFamilyId';
protected $table = 'Common_Ref_ColorFamily';
public $timestamps = false;
}
Controller:-
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Input;
use Carbon\Carbon;
use Illuminate\Support\Facades\DB;
use App\Http\Requests;
use App\Models\ColorFamily;
use App\Models\Color;
class ColorController extends Controller
{
public function Index()
{
$Data = Color::select ('ColorId','ColorName','ColorCode','ColorShortCode')
->join('ColorFamily','ColorFamilyId','=','Color.ColorFamilyId')
->get();
return $Data;
}
}
Can you try the following code:
class ColorController extends Controller {
public function Index()
{
$Data = DB::table('Color')
->select ('ColorId','ColorName','ColorCode','ColorShortCode')
->join('ColorFamily','ColorFamily.ColorFamilyId','=','Color.ColorFamilyId')
->get();
return $Data;
}
}
I think that you need to add joined table name inside join
('ColorFamily.ColorFamilyId','=','Color.ColorFamilyId')

How to add Softdeletes to Notifications Table laravel

I am using laravel default database notifications, I want to add softdelete to notifications table.
I have created a migration with softdelete which has added deleted_at column to the notifications table. The problem is I have to add 'use SoftDeletes' to notifications model(according to laravel docs) but cannot find the notifications model.
$table->softDeletes();
I tried adding 'use SoftDeletes' to HasDatabaseNotifications trait but it still deletes the row. Is there another way to add softdelete to notifications table. TIA
In your model at top before start class use
use Illuminate\Database\Eloquent\SoftDeletes;
After class
class Notification extends Model
{
use SoftDeletes;
/**
* The attributes that should be mutated to dates.
*
* #var array
*/
protected $dates = ['deleted_at'];
}
This is how I solved it, I hope it will be useful for you and other friends
App\Classes\MyDatabaseNotification.php
namespace App\Classes;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Notifications\DatabaseNotification;
class MyDatabaseNotification extends DatabaseNotification
{
use SoftDeletes;
protected $dates = ['deleted_at'];
}
App\Classes\MyNotifiable.php
namespace App\Classes;
use Illuminate\Notifications\Notifiable;
trait MyNotifiable
{
use Notifiable;
/**
* Get the entity's notifications.
*/
public function notifications()
{
return $this->morphMany(MyDatabaseNotification::class, 'notifiable')
->orderBy('created_at', 'desc');
}
}
App\User.php
namespace App\Models;
use Illuminate\Foundation\Auth\User as Authenticatable;
use App\Classes\MyNotifiable;
...
class User extends Authenticatable
{
use MyNotifiable;
...

laravel voyager soft delete issue

I'm trying to get soft deleting work on laravel voyager but everytime I delete a post it's also deleted in my database.
here is my post model :
<?php
namespace App;
use TCG\Voyager\Traits\Resizable;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use App\Category;
class Post extends Model
{
use Resizable;
use SoftDeletes;
protected $dates = ['deleted_at'];
public function category()
{
return $this->belongsTo(Category::class);
}
public function scopeIspublished($query)
{
return $query->where('status','published');
}
}
I added a column deleted_at to my posts table as well.
Thanks for helping

Laravel 5.2 eloquent returns soft deleted records

I am using Laravel 5.2.
I have a model as below:
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\SoftDeletes;
class ZoomMeeting extends BaseModel {
public $timestamps=true;
protected $table = 'zoom_meetings';
use SoftDeletes;
protected $dates = ['deleted_at'];
protected $fillable = ['user_id', 'uuid', 'meeting_id', 'host_id', 'topic', 'status', 'type', 'start_url', 'join_url', 'created_at'];
public function users() {
return $this->belongsTo('App\Models\User');
}
}
And the base model is as below:
<?php namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Auth;
use Carbon\Carbon;
class BaseModel extends Model {
public $timestamps = false;
protected static function boot()
{
//parent::boot();
static::creating(function($model) {
if(empty($model->created_at))
{
$model->created_at = date('Y-m-d H:i:s');
}
return true;
});
static::updating(function($model) {
$model->updated_at = date('Y-m-d H:i:s');
return true;
});
}
}
I am using softdeletetrait in ZoomMeeting model, and soft deleting is working fine.
However, if I fetch records from the same model using eloquent, it returns the soft deleted records too. I am using code below to get the records:
$record = ZoomMeeting::where("user_id", $user_id)->where("meeting_id", $meeting_id)->orderBy("id", "DESC")->first();
The eloquent is building the query as:
select * from `zoom_meetings` where `user_id` = 3 and `meeting_id` = 707070707 order by `id` desc limit 1
See, there is no deleted at is null set in where statement. It is not preventing the deleted records.
I am not sure where am I making mistake?
It looks like you are overriding the boot method, but you aren't ever actually calling the parent boot method (it's commented out), so the trait is never getting initialized correctly. I believe that also means the data you have been deleting is actually being deleted from the database.
Is there a reason you need to override the boot method? What you are adding is already done handled by the framework, so it doesn't appear to be necessary.

Resources