Call to undefined method onlyTrashed() - laravel

anyone can help me?
how to resolve ->
BadMethodCallException
Call to undefined method App\ModalName::onlyTrashed()
my controller ->
public function destroy(Abc $abc)
{
$abc= Abc::destroy($abc->id)->get();
return redirect('/dir/abcdir')-> with('delete', 'aaa');
}

According to the docs, you need to use traits 'SoftDeletes'.
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
class Flight extends Model
{
use SoftDeletes;
}
PS:
Please post complete code of your controller and model.

For SoftDelete to work in Laravel Eloquent Model
You must use SoftDeletes Trait
Table schema should have $table->softDeletes(); which will add deleted_at column in the table.
Reference: Eloquent Soft-Delete

Related

Property does not exist in collection while using belongsToMany in laravel eloquent

I am using Laravel 8.X and eloquent to develop a web app.
I have a pivot table 'portal_event_users'
I am trying to add a belongsToMany relationship to the portal_users in the model of portal_event_users table.
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class EventUser extends Model
{
use HasFactory;
protected $table = 'portal_event_users';
public function users()
{
return $this->belongsToMany(User::class);
}
public function events()
{
return $this->belongsToMany(Event::class);
}
}
I have the following statements in the controller
$eventusersobj = \App\Models\EventUser::select('*')
->where('event_id', '=', $event_id)
->get();
$response = $eventusersobj->users->keyBy('id');
It is returning the following error
Property [users] does not exist on this collection instance.
Can someone please advice on how can i change this error?
Thanks in advance
As it returns a collection, you can use pluck()
$eventusersobj->pluck('users')->each(function($user){
$user->keyBy('id');
})

Is there a way to specify the pivot table for a has many relationship in Laravel?

I have a Company model that has a relationship with users. I have a pivot table called game_user generated with https://github.com/laracasts/Laravel-5-Generators-Extended by doing php artisan make:migration:pivot companies users . I have the following migration
public function up()
{
Schema::create('company_user', function (Blueprint $table) {
$table->unsignedBigInteger('company_id')->unsigned()->index();
$table->foreign('company_id')->references('id')->on('companies')->onDelete('cascade');
$table->unsignedBigInteger('user_id')->unsigned()->index();
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
$table->primary(['company_id', 'user_id']);
});
}
On my Company Model I made a employees function for the relationship
<?php
namespace App\Models;
use App\User;
use Illuminate\Database\Eloquent\Model;
class Company extends Model
{
public function employees()
{
return $this->hasMany(User::class);
}
}
And on my CompanyController I have the following
<?php
namespace App\Http\Controllers;
use App\Models\Company;
use Illuminate\Http\Request;
class CompanyController extends Controller
{
public function index($id)
{
return Company::find($id);
}
public function employees($id)
{
return Company::find($id)->employees;
}
}
But when requesting to it from postman with a get request.
http://localhost/laravel_applications/myapi/public/api/company/1/employees
I get a 500 internal server error. Shouldn't Laravel automatically detect the table? I'm quite sure it isn't detecting or even looking for my pivot table.
This is my route, I tested it by returning as string and that worked just fine. So the route is working.
Route::get('company/{id}/employees', 'CompanyController#employees');
Method hasMany should be used for one-to-many relation. You need to call belongsToMany method for a many-to-many relation.

How to use table without model in hasMany

is that possible to use direct table name in hasMany. for example
public function Permissions()
{
return $this->hasMany('TABLE NAME');
}
I have table named PERMISSION but don't have a model of this table, but i want to use this table in hasmany.
Relations in Laravel are a part of Eloquent ORM which is built on the use of models.
So, no, it is not possible to use relations without making models for them.
Just create a model for that table
php artisan make:model Permisson
then add the table name to the model class
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Permisson extends Model
{
protected $table = "table_name";
}
and add the relation
public function permissions()
{
return $this->hasMany(Permisson::class)
}
I hope it helps

How can i get result from model in laravel 5

Good day, i'm trying to get the result from my model that called with Mainmodel through my controller, my controller is MainController.
Here is my controller
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use app\Mainmodel;
class MainController extends Controller
{
function index(){
echo "Kok, direct akses sih?";
}
function get_menu(){
$menu = app\Mainmodel::request_menu();
dd($menu);
}
}
Here is my model
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Mainmodel extends Model
{
function request_menu(){
$menu = DB::table('menu')
->orderBy('[order]', 'desc')
->get();
return $menu;
}
}
my routes
Route::get('menu','MainController#get_menu');
with my script above i get this
FatalErrorException in MainController.php line 17: Class
'App\Http\Controllers\app\Mainmodel' not found
how can i fix this ? thanks in advance.
Note: I'm bit confuse with laravel. I'm using codeigniter before. And i have a simple question. In laravel for request to database should i use model ? or can i just use my controller for my request to database.
sorry for my bad english.
I would imagine it's because your using app rather than App for the namespace.
Try changing:
app\Mainmodel
To:
App\Mainmodel
Alternatively, you can add a use statement to the top of the class and then just reference the class i.e.:
use App\Mainmodel;
Then you can just do something like:
Mainmodel::request_menu();
The way you're currently using you models is not the way Eloquent should be used. As I mentioned in my comment you should create a model for each table in your database (or at least for the majority of use cases).
To do this run:
php artisan make:model Menu
Then in the newly created Menu model add:
protected $table = 'menu';
This is because Laravel's default naming convention is singular for the class name and plural for the table name. Since your table name is menu and not menus you just need to tell Laravel to use a different table name.
Then your controller would look something like:
<?php
namespace App\Http\Controllers;
use App\Menu;
class MainController extends Controller
{
public function index()
{
echo "Kok, direct akses sih?";
}
public function get_menu()
{
$menu = Menu::orderBy('order', 'desc')->get();
dd($menu);
}
}
Hope this helps!
You can solve it by different solution. The solution is you don't have to call request_menu(); you can get it in your controller.
MainController
use use Illuminate\Support\Facades\DB;
public function get_menu(){
$menu = DB::table('menu')
->orderBy('Your_Field_Name', 'DESC')
->get();
dd($menu);
}

Laravel Eloquent - Soft deleting is not working

I have added $table->softDeletes() to my migration file and have added the protected $softDelete = true; to my model, and I do have the deleted_at in my table. But whenever I call delete(), the entire row is deleted. Here is how I am calling my delete:
Schedule::whereId($id)->whereClientId($client_id)->delete();
What am I doing wrong?
In case somebody bumps into this question. You can still use SoftDeletes in Laravel 5. Just use the softdeletes trait.
namespace App;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes; //<--- use the softdelete traits
class YourModel extends Model
{
use SoftDeletes; //<--- use the softdelete traits
protected $dates = ['deleted_at']; //<--- new field to be added in your table
protected $fillable = [
];
}
You can view laravel docs or This great tutorial on how to softdelete
If you're overriding \Illuminate\Database\Eloquent\Model::boot for whatever reason, ensure you call parent::boot in the method so traits (like SoftDeletes) can be booted.

Resources