Look here :
When I click button delete, data deleted still appear in the interface.
I look at the table in the database, the data is erased.
Data can be lost if I run this: php artisan cache:clear on git bash.
How without running the command, data can be erased?
UPDATE
My controller is like this :
public function deleteAccount(Request $request)
{
$id_account = $request->input('id_account');
try{
if($this->user_service->deleteAccount($id_account)) {
$status='success';
}else {
$status = 'failed';
}
return redirect('member/profile/setting/account')->with('status',$status);
}catch (\Exception $exception){
return $this->respondInternalError();
}
}
My service is like this :
public function deleteAccount($id_account)
{
return $this->user_bank_repository->delete($id_account);
}
My repository is like this :
<?php
namespace App\Repositories;
use App\Models\UsersBank;
use Illuminate\Contracts\Container\Container;
use Rinvex\Repository\Repositories\EloquentRepository;
class UserBankRepository extends EloquentRepository
{
public function __construct(Container $container)
{
$this->setContainer($container)
->setModel(UsersBank::class)
->setRepositoryId('rinvex.repository.uniqueid');
}
public function delete($id)
{
$data = self::find($id)->delete();
return $data;
}
}
I see you are also using the rinvex repository. I don't see your code about fetch data. I assume you are fetching data from the repository cache. In your repository, your delete function.
public function delete($id){
return $this->delete($id);
}
this should clear the repository cache.
if you would like clear the cache of the repository manually, try the following in your repository method.
$this->forgetCache();
or
$this->getContainer('events')->fire($this->getRepositoryId() . '.entity.updated', [$this, $modelInstance]);
actually forgetCache() method shall do, I'm using it in my repository after I made mass update to my table.
Related
I would like to make a package which uses a custom stub for creating migrations needed for my package. More precisely, running the command should make pivot tables for models which have a specific trait present.
If I make a "normal" command, I can register it within my service provider:
public function boot()
{
if ($this->app->runningInConsole()) {
$this->commands([
MakeContainerMigration::class,
]);
}
}
However in this case I wanted to reuse Laravel's coding and save myself the trouble to reinvent the wheel. So my command looks like this:
class MakeContainerMigration extends MigrateMakeCommand
{
protected $name = 'custom:make-container';
protected $description = '...';
}
Since MigrateMakeCommand doesn't have stubs defined, but rather it's dependency MigrationCreator, I needed to find a way to provide custom stub path to it without disrupting "regular" migration stubs.
I tried doing something like this but failed:
public function register()
{
$this->registerCreator();
$this->registerMigrateMakeCommand();
if ($this->app->runningInConsole()) {
$this->commands([
//MakeContainerMigration::class,
'custom.command.migrate.make'
]);
}
}
protected function registerCreator()
{
$this->app->singleton('custom.migration.creator', function ($app) {
return new MigrationCreator($app['files'], __DIR__ . '/stubs');
});
}
protected function registerMigrateMakeCommand()
{
$this->app->singleton('custom.command.migrate.make', function ($app) {
$creator = $app['custom.migration.creator'];
$composer = $app['composer'];
return new MakeContainerMigration($creator, $composer);
});
}
I'm aware that registering the command shouldn't function like this as I am simply registering singletons to Laravel app instance, but I have no idea how to register it through the class while ensuring that the right version of MigrationCreator will be injected. I'm kinda stuck here, is there a way to do it?
Turns out everything is working, I just needed to replace
protected $name = 'custom:make-container';
with
protected $signature = 'custom:make-container';
I have tried to register to the container an Uuid and i have tried to retrive it from a route controller more than once, but the uuid value is not the first registered.
Can anyone help me to understand?
class AppServiceProvider extends ServiceProvider
{
public function boot()
{
if(App::bound('conf')==NULL)
App::instance('conf', Uuid::generate()->string);
}
}
class InstanceController extends Controller
{
public function getUuid()
{
return App::make('conf');
}
}
I need to register an unique value or object that will be accessible to all.
I have also tried to put this code:
config(['uuid' => Uuid::generate()->string]);
in Laravel command handle method:
class RegisteredInstances extends Command
{
public function handle()
{
config(['uuid' => Uuid::generate()->string]);
}
}
and execute it, but when i try to retrive the uuid from a service, the response is null.
Now i have registered a laravel command that do this:
class RegisteredInstances extends Command
{
.
.
.
public function handle()
{
if(App::bound('conf')==NULL)
App::instance('conf', Uuid::generate()->string);
if(config('uuid2')==NULL)
config(['uuid2' => Uuid::generate()->string]);
}
}
A task every minute execute this command and i try to retrive the uuid from a service controller like this:
class InstanceController extends Controller
{
public function getUuid()
{
return App::make('conf');
}
public function getUuid()
{
return config('uuid2');
}
}
The problem, in this case, is that the controller return NULL:
You need to use laravel Configuration (accessing-configuration-values) with AppServiceProvider
Example:
class AppServiceProvider extends ServiceProvider
{
public function boot()
{
config([ 'theconfig.uuid' => $class_here->UUID ]);
}
}
Then to use it, call
config('theconfig.uuid');
anywhere in the program
Is this in Laravel 4? I haven't seen the App::instance markup before, but I found it in Laravel 4.2 docs for the IoC Container.
This looks like a case for using a singleton. You can use this to ensure that conf is only resolved once. Looking at 4.2 docs, you could define your singleton as follows.
App::singleton('conf', function()
{
return Uuid::generate()->string;
});
I am Trying to Using Observer for Deleting With Relationship But Problem Is When i DD in Created Function Its Working Fine But When i DD In Deleted Function It Shows Nothing (POSTMAN) Means Neither Working Nor Error With Same Everything
Here Is Api:
$api->post('store','App\Http\Controllers\CustomerController#store');
$api->delete('delete/{id}','App\Http\Controllers\CustomerController#destroy');
Here Is Observer file made by artisan
namespace App\Observers;
use App\Customer;
class CustomerObserver
{
public function created(Customer $customer)
{
dd($customer);
}
public function deleted(Customer $customer)
{
dd($customer);
}
}
Here is Customer Controller
class CustomerController extends Controller
{
public function store(Request $request)
{
return Customer::store($request->person);
}
public function destroy($id)
{
$delete = Customer::where('person_id',$id);
$delete->delete();
}
}
Here Is Customer Model File.
class Customer extends Model
{
//Relationship Start From Here
public function person()
{
return $this->belongsTo(Person::class);
}
//End Here
public static function store($request)
{
//Call to Person Model And Save User
$user = Person::store($request);
//Create object of Customer Model
$customer = new Customer();
$customer->fill($request['customers']);
$customer->person()->associate($user)->save();
//return customer
return $customer;
}
}
I know this might be a late reply and not sure if you are still looking for the answer. I think the issue is about how you delete your Customer model.
When you do something like
$delete = Customer::where('person_id',$id);
$delete->delete();
You are executing a mass delete statement. As stated in laravel document, mass deletes will not fire any model events for the models that are deleted This is the reason your deleted observer event didn't fire.
When executing a mass delete statement via Eloquent, the deleting and deleted model events will not be fired for the deleted models. This is because the models are never actually retrieved when executing the delete statement
Now look at how you create a Customer. You create a model one at a time. Therefore your created observer does get run.
//Create object of Customer Model
$customer = new Customer();
$customer->fill($request['customers']);
$customer->person()->associate($user)->save();
To solve your problem, the easiest way is to retrieve all the models and delete one by one, so that you can trigger the event.
foreach (Customer::where('person_id',$id)->get() as $delete) {
$delete->delete();
}
can you do all things
1 add line in Customer::observe(CustomerObserver::class); in CustomerServiceProvider in boot method
add CustomerServiceProvider in app.php file in provider array
composer dump-autoload
php artisan config:cache
I was also having this problem, however I was calling the delete method from the repository, which prevents the Observer's deleted event from being triggered. When I used the delete from the mode it worked normally.
Only these Methods Works
$customer=Customer::where('id',$id)->first();
if($customer){
$customer->delete();
}
Or
$customer=Customer::find($id)->delete();
I have a selection control on a blade form that is to be refreshed via ajax through this function:
function getOpciones(tbName) {
$.get('/ajax/read-data/' + tbName, function(data){
return (data);
});
}
The function takes a string variable 'tbName' whith the name of the table the control is related to, and passes it on as a parameter to the route:
Route::get('/ajax/read-data/{modelo}', 'AjaxController#readData');
Then the controller should get the parameter {modelo}, and retrieve the records in that table:
use App\RegFiscal;
public function readData($modelo) {
$arreglo = $modelo::all();
return response($arreglo);
}
But even though I am referencing the model with 'use App\RegFiscal', all I get is this error in laravel log:
2018-03-23 18:52:08] local.ERROR: exception
'Symfony\Component\Debug\Exception\FatalErrorException' with message
'Class 'RegFiscal' not found' in
C:\wamp64\www\laravel\cte\app\Http\Controllers\AjaxController.php:32
I´m new to Laravel, so needless to say I am lost and any help would be greatly appreciated. Thanks!
Just because you use App\RegFiscal doesn't mean $modelo is associated with it.
What you can do, though, is use app("App\\$modelo") to load in your model based on the parameter you get from the router. You would no longer need to use App\RegFiscal either.
$arreglo = app("App\\$modelo");
return response($arreglo::all());
This is assuming your model is stored in the default app directory within your Laravel project. If not you can change "App\" to where ever it is stored. If for example your model is in app\models\modelname.php it would be "App\Models\\$modelo".
You can do this as the following:
public function readData($modelo) {
$modelName = '\App' . '\\' . $modelo;
$class = new $modelName();
arreglo = $class::all();
return response($arreglo);
}
To those like me who wanted to inject it on a constructor, here's how to do it:
~$ php artisan make:provider MyProvider
Then override the register function like so:
class MyProvider implements ServiceProvider {
/** #override */
public function register() {
$this->app->bind(ShapeInterface::class, function ($app) {
return new Square($app->make(MyModel::class));
});
}
}
The ShapeInterface is a simple interface and Square is a simple class that implements the shape interface with a constructor parameter of the eloquent model.
class Square implements ShapeInterface {
private MyModel $model;
function __construct(MyModel $model) {
$this->model = $model;
}
...
}
I'm attempting to make cascading soft deletes work by overriding each model's delete() function, which I think should cascade down from Project to Version to Link, but the problem is it doesn't seem to do that at all. The idea would be that deleting a project would also delete all the versions, which would delete all the links and clear their cached versions, but using $this->versions()->delete(); doesn't seem to actually call the delete() method in the Version model.
Any ideas on how to get this working as I expect it to?
class Project extends Eloquent {
protected $softDelete = true;
public function versions()
{
return $this->hasMany('Version');
}
public function delete()
{
$this->versions()->delete();
return parent::delete();
}
}
class Version extends Eloquent {
protected $softDelete = true;
public function project()
{
return $this->belongsTo('Project', 'project_id');
}
public function links()
{
return $this->hasMany('Link');
}
public function delete()
{
$this->links()->delete();
return parent::delete();
}
}
class Link extends Eloquent {
protected $softDelete = true;
public function version()
{
return $this->belongsTo('Version', 'version_id');
}
public function delete()
{
Cache::forget($this->id);
return parent::delete();
}
}
You want to use the models events instead of overriding the core functions. To quote the docs:
Eloquent models fire several events, allowing you to hook into various points in the model's lifecycle using the following methods: creating, created, updating, updated, saving, saved, deleting, deleted, restoring, restored.
What you want to do is hook into these. You can do this a couple of ways (see the docs here). Here's an example by setting a model boot method:
public static function boot()
{
parent::boot();
/**
* Deleting
* - Called before delete()
* - Passes the instance in as param
*/
static::deleting(function($project){
// Get the project versions ids as an array
$ids = $project->versions()->lists('id');
// Delete the versions
Version::whereIn('id', $ids)->delete();
});
}
You can then do the same in your other models. Hope that helps!
$this->versions()->delete(); as well as Version::whereIn('id', $ids)->delete(); does a delete() call on a query builder, not the eloquent models, which means model events don't get fired. Instead, you should do:
foreach($this->versions as $version) { $version->delete(); }