I just started with laravel media library. While uploading images to the database I am getting an error. I tried searching in documentation But I am not finding an answer for this.
the error which I am getting
Argument 1 passed to Spatie\MediaLibrary\MediaCollections\FileAdder::processMediaItem() must be an
instance of Spatie\MediaLibrary\HasMedia, instance of App\Service given, called in
C:\xampp\htdocs\Matheen\furniture_backend\vendor\spatie\
laravel-medialibrary\src\MediaCollections\FileAdder.php on line 372
controller
public function store(Request $request)
{
$service = Service::create([
'service_name' => $request->service_name
]);
$file = $request->file('image');
$service->addMedia($file)->toMediaCollection('services');
return redirect('services')->with('success','Service Added Successfully');
}
Model
use Illuminate\Database\Eloquent\Model;
use Spatie\MediaLibrary\MediaCollections\Models\Media;
use Spatie\MediaLibrary\InteractsWithMedia;
use Spatie\MediaLibrary\HasMedia;
class Service extends Model
{
use InteractsWithMedia;
protected $fillable = ['service_name'];
public function registerMediaCollections(): void
{
$this->addMediaCollection('services');
}
public function registerMediaConversions(Media $media = null): void
{
$this->addMediaConversion('thumbnail')
->width(1000)
->height(250);
}
}
in model class statement
you have: "class Service extends Model"
you need: "class Service extends Model implements HasMedia"
...
...
use Spatie\MediaLibrary\HasMedia;
class Service extends Model implements HasMedia
{
// code
}
..
i did this
composer require spatie/laravel-medialibrary:10.0.7
and problem solved
laravel 9
medialibrary 10.0.7
Related
I’m kind of new to Laravel and the whole API architecture, so my question may seem dumb at first.
My basic setup:
Laravel 8;
PHP 8;
routes\api.php
Route::post('/categories/',[ApiCategoriesInsertController::class, 'insertCategories'], function($insertCategoriesResults) {
return response()->json($insertCategoriesResults);
})->name('api.categories.insert');
\app\Http\Controllers\ApiCategoriesInsertController.php (created with php artisan make:controller)
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Route;
// Custom models.
use App\Models\CategoriesInsert;
class ApiCategoriesInsertController extends Controller
{
private mixed $ciAPI;
public function __construct(Request $req)
{
}
public function insertCategories(Request $req): array
{
$this->ciAPI = new CategoriesInsert(['testing'=>'debug']);
return [‘status’ => ‘OK’];
}
}
\app\Models\CategoriesInsert.php (created with php artisan make:model)
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class CategoriesInsert extends Model
{
use HasFactory;
public function __construct(array $objParameters)
{
}
}
When I make a post to http://localhost:8000/api/categories, Laravel logs the following error:
local.ERROR: Too few arguments to function App\Models\CategoriesInsert::__construct(), 0 passed in … Too few arguments to function App\\Models\\CategoriesInsert::__construct(), 0 passed in …
Anyone knows what’s wrong or missing in my architecture?
Thanks!
Make your model's constructor compatible with parent.
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class CategoriesInsert extends Model
{
use HasFactory;
public function __construct(array $attributes = [])
{
parent::__construct($attributes);
}
}
Also, notice, that when you call new CategoriesInsert(['testing'=>'debug']), you do not save the data in your database. Use:
$insert = new CategoriesInsert(['testing'=>'debug']);
$insert->save();
Or:
CategoriesInsert::create(['testing'=>'debug']);
you don't need to pass data to model
change your code to bellow code:
public function insertCategories(Request $req): array
{
CategoriesInsert::create(['testing' => 'debug']);
return [‘status’ => ‘OK’];
}
key of passed array is your filed name in database, and value stored data
also you should define fillable parameter in model
protected $fillable = [
'title',
'slug',
'priority',
];
I updated a backpack project from backpack 4.0 to 4.1 and followed the upgrade guide that is provided on the backpack site. Laravel still runs on 6.x and has not been upgraded lately.
The list and /edit update views are working as intended. Only when trying to open a show view (happens on all models), then the following error occurs:
Too few arguments to function Illuminate\Database\Eloquent\Model::created(), 0 passed in /var/www/vendor/backpack/crud/src/app/Library/CrudPanel/CrudPanel.php on line 330 and exactly 1 expected
I tried to follow the stack trace but I can't find the source that throws the error. I also tried to remove all the methods from one model and just keep construct() and setup() and even then the errors is still thrown.
Edit: The error occurs for all models, this is the code of one random model.
namespace App\Models;
use Backpack\CRUD\app\Models\Traits\CrudTrait;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
class Vereine extends KVUser
{
}
namespace App\Models;
use Backpack\CRUD\app\Models\Traits\CrudTrait;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
class KVUser extends Model
{
use CrudTrait;
use SoftDeletes;
protected $table = 'user';
public $incrementing = true;
/*
|--------------------------------------------------------------------------
| GLOBAL VARIABLES
|--------------------------------------------------------------------------
*/
protected $guarded = [];
public function __construct(array $attributes = [])
{
$this->creating([$this, 'onCreating']);
$this->updating([$this, 'onUpdating']);
parent::__construct($attributes);
}
public static function deleting($callback)
{
parent::deleting($callback);
$callback->update(['deleted' => 1]);
}
public function onCreating(\App\Models\KVUser $row)
{
// Placeholder for catching any exceptions
if (!\Auth::user()->id) {
return false;
}
$row->setAttribute('created_id', \Auth::user()->id);
}
public function onUpdating(\App\Models\KVUser $row)
{
// Placeholder for catching any exceptions
if (!\Auth::user()->id) {
return false;
}
$row->setAttribute('updated_id', \Auth::user()->id);
}
}
namespace App\Http\Controllers\Admin;
use App\Http\Requests\VereineRequest;
use App\Models\Vereine;
use Backpack\CRUD\app\Http\Controllers\CrudController;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;
class VereineCrudController extends CrudController
{
use \Backpack\CRUD\app\Http\Controllers\Operations\ListOperation;
use \Backpack\CRUD\app\Http\Controllers\Operations\CreateOperation;
use \Backpack\CRUD\app\Http\Controllers\Operations\UpdateOperation;
use \Backpack\CRUD\app\Http\Controllers\Operations\DeleteOperation;
use \Backpack\CRUD\app\Http\Controllers\Operations\ShowOperation;
public function __construct()
{
parent::__construct();
}
public function setup()
{
$this->crud->setModel('App\Models\Vereine');
$this->crud->setRoute(config('backpack.base.route_prefix') . '/vereine');
$this->crud->setEntityNameStrings('Verein', 'Vereine');
$this->crud->setListView('vendor/backpack/crud/vereine/vereine_list');
$this->crud->setShowView('vendor/backpack/crud/vereine/vereine_show');
}
}
Screenshot Error Message
I would really appreciate some help - thank you!
For clearly to identify error you should attached image. Thanks
I used laravel Auditor in a model and it works very well as following:
use Illuminate\Database\Eloquent\Model;
use OwenIt\Auditing\Contracts\Auditable;
class Contracts extends Model implements Auditable
{
use \OwenIt\Auditing\Auditable;
protected $fillable=['condatereceived'];
public function user()
{
return $this->belongsTo(User::class);
}
}
But I want to used it in the controller as :
public function updatecomplated(Request $request, $id,Contracts $contract ,Auditor $auditor)
{
Contracts::where('id', $id)
->update(['complated' => 50, 'conuploadby' => Auth::id(),'constatus' =>'Need To Active' ]);
if ($audit = $auditor->execute($contract)) {
$auditor->prune($contract);
}
return redirect()->back();
}
The code in controller give me error:
Call to undefined method OwenIt\Auditing\Facades\Auditor::execute()
any ideas to use auditor in the controller, please.
try this package its easy with good documentation
simply add this to your table
$table->auditable();
and this to your model
namespace App;
use Yajra\Auditable\AuditableTrait;
class User extends Model
{
use AuditableTrait;
}
thats it now simply get your auditor by calling
$user->creator // for who create
and
$user->updater //for who update data
for more information click here for check trait
Hope this helps
I'm working on laravel's project flyer and I'm continually getting this error from artisan tinker.
$flyer->photos()->create(['photo' => 'foo.jpg']);
BadMethodCallException with message 'Call to undefined method Illuminate
\Database\Query\Builder::photos()'
Here is my Flyer.php file:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Flyer extends Model
{
public function photos()
{
return $this->hasMany('App\Photo');
}
}
and here is my Photo.php file:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Photo extends Model
{
protected $table = 'flyer_photos';
protected $fillable = ['photo'];
public function flyer()
{
return $this->belongsTo('App\Flyer');
}
}
looks like the method photos() doesn't get recognised or something
From the error description, $flyer is not a Flyer object, it's an Illuminate \Database\Query\Builder object. That's your error. When creating $flyer, be sure to use a get() or first() after building your query.
I am trying to add a hasMany relation from my Project class to my Version class (Projects have many Versions).
I have these:
My Project class -
namespace App;
use Version;
use Jenssegers\Mongodb\Model as Model;
class Project extends Model
{
protected $fillable = ['title'];
public function versions()
{
return $this->hasMany('Version');
}
}
My Version class -
<?php
namespace App;
use Jenssegers\Mongodb\Model as Model;
class Version extends Model
{
protected $fillable = ['nickname'];
public function project()
{
return $this->belongsTo('Project');
}
}
And in a route -
public function createVersion(Request $request)
{
$projectID = $request->input('projectID');
$version = $request->input('version');
$project = Project::where('_id', $projectID)->first();
$version = new Version();
$version->projectID = $projectID;
$version->nickname = $version['nickname'];
$project->versions->save($version);
return $version;
}
I am getting the error - Class 'Version' not found - and it points to Model.php.
Am I missing adding a "use" somewhere, or am I missing something else entirely?
You have defined the class Version under the App namespace.
For that reason you must use
public function project()
{
return $this->belongsTo('App\Project');
}
in your Project class.