Reputation: 39
Argument 1 passed to App\Candidate::fileUpload() must be an instance of Illuminate\Http\Request, instance of Illuminate\Http\UploadedFile given, called in C:\xampp\htdocs\Laravel-voting-system\app\Candidate.php on line 40
please I don't know where am getting it wrong this is where I wrote the fill upload function(candidate.php)
The second image is the downward path of the same folder candidate.php
this my user.php for your view
<?php
namespace App;
use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;
class User extends Authenticatable
{
use Notifiable;
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'name', 'email', 'password','regno'
];
/**
* The attributes that should be hidden for arrays.
*
* @var array
*/
protected $hidden = [
'password', 'remember_token',
];
public function role(){
return $this->belongsTo('App\Role');
}
public function candidate(){
return $this->hasOne('App\Candidate');
}
public function registerVoter($name,$email,$password,$regno){
$newVoter = new User;
$newVoter->name = $name;
$newVoter->email = $email;
$newVoter->password = bcrypt($password);
$newVoter->regno = $regno;
$newVoter->role_id = 2;
$newVoter->save();
}
public static function addCandidate($studentId,$seat,$image){
$user = User::find($studentId);
(new Candidate)->add($user->name,$seat,$user->regno,$user->id,$image,);
}
And my AdminController.php
<?php
namespace App;
use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;
class User extends Authenticatable
{
use Notifiable;
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'name', 'email', 'password','regno'
];
/**
* The attributes that should be hidden for arrays.
*
* @var array
*/
protected $hidden = [
'password', 'remember_token',
];
public function role(){
return $this->belongsTo('App\Role');
}
public function candidate(){
return $this->hasOne('App\Candidate');
}
public function registerVoter($name,$email,$password,$regno){
$newVoter = new User;
$newVoter->name = $name;
$newVoter->email = $email;
$newVoter->password = bcrypt($password);
$newVoter->regno = $regno;
$newVoter->role_id = 2;
$newVoter->save();
}
public static function addCandidate($studentId,$seat,$image){
$user = User::find($studentId);
(new Candidate)->add($user->name,$seat,$user->regno,$user->id,$image,);
}
I really appreciate your efforts, Thanks in Advance.
Upvotes: 0
Views: 2919
Reputation: 17481
It looks like you've mixed a controller with a model.
See, the controller is meant to handle the request. The model is meant to persist entities and handle their relationships. Your Candidate
is neither.
Laravel has a type hinting system that will inject an instance of the hinted type in the controller method automagically. Therefore, if you had a controller in the likes of
<?php
use Illuminate\Http\Request;
class CandidateController {
public function fileUpload( Request $request ) {
...
}
}
And a route in the likes of
Route::post('candidate','CandidateController@fileUpload');
Then fileUpload
method would receive an instance of \Illuminate\Http\Request
Now, if you submitted a file from the frontend, the file would be in the request (as shown in your code)
$image = $request->file('image'); // 'image' is just the input name
So you should not call fileUpload
from another method. It's the other way around. The frontend sends the request, the controller handles the request and "extracts" the file which would then be persisted in the disk/cloud/wherever and its metadata sent to the DDBB and associated to the acting user
The controller moves the image (you're already doing that) to its intended path, then stores it in the model like
public function fileUpload( Request $request ) {
$image = $request->file('image');
$candidate = new App\Candidate();
$name = time().'.'.$image->getClientOriginalName();
$image->move(public_path("images"), $name);
$candidate->path = public_path('images').'/'.$name;
$candidate->save();
}
Since there are other fields, I guess you are sending them in the request too, like
$candidate->seat = $request->seat;
And the acting user, if you need it, should come from the auth helper (e.g. the session or token) to avoid a malicious visitor sending another user's id.
Upvotes: 1