I want to fetch the image from database as a URL like http://127.0.0.1:8000/uploads/category/image.png
but the result I am getting is the name of the file not the URL.
This is the model class:
class CategoryModel extends Model
{
protected $table="tb_category";
public $timestamps = false;
protected $fillable = [
'id',
'name',
'image'
];
}
This is my controller class:
class CategoryController extends Controller
{
//
public function viewPage(){
return view('category');
}
public function saveCategory(Request $request){
$category = new CategoryModel();
$category->name=$request->input('name');
if($request->hasfile('image')){
$file=$request->file('image');
$extension = $file->getClientOriginalExtension();
$filename=time().'.'.$extension;
//$headers = array('Content-Type' => 'application/octet-stream');
$file->move('uploads/category/',$filename);
$category->image =$filename;
$category->_image =$filename;
}
else{
return $request;
$category->image='';
$category->_image=null;
}
$category->save();
return view('category')->with('category',$category);
}
public function getCategories(){
$res = CategoryModel::get(
['id','name','image'
]
);
return response()->json($res,200);
}
}
In your saveCategory method, you're storing $filename in 'image' column.
Change it to store path.
public function saveCategory(Request $request){
$category = new CategoryModel();
$category->name=$request->input('name');
if($request->hasfile('image')){
$file=$request->file('image');
$extension = $file->getClientOriginalExtension();
$filename=time().'.'.$extension;
$file->move('uploads/category/',$filename);
$category->image ='http://127.0.0.1:8000/uploads/category/'.$filename; //This is where you need to save absolute path of the image.
}
else{
return $request;
}
$category->save();
return view('category')->with('category',$category);
}
One more problem here is that you've written code after return $request; in else statement. Any code written after return is unreachable for the interpreter and will not be executed.
Related
Call to a member function getRealPath() on string error occurs while sending an email.
Controller
public function store(CareerRequest $request)
{
$requestData = $request->all();
$filenameWithExt = $request->file('resume')->getClientOriginalName();
$filename = pathinfo($filenameWithExt, PATHINFO_FILENAME);
$extension = $request->file('resume')->getClientOriginalExtension();
$fileNameToStore = $filename.'_'.time().'.'.$extension;
$request->file('resume')->storeAs('candidateResume', $fileNameToStore);
$requestData["resume"] =$fileNameToStore ;
Career::create($requestData);
return redirect()->back();
}
Mailable
class CareerMail extends Mailable
{
public $data;
public function __construct($data)
{
$this->data = $data;
}
public function build()
{
return $this->subject('Career - '. $this->data->subject)
->view('emails.career')
->attach($this->data['resume']->getRealPath(),
[
'as' => $this->data['resume']->getClientOriginalName(),
'mime' => $this->data['resume']->getClientMimeType(),
]);
}
}
Error on line
->attach($this->data['resume']->getRealPath(),
You are trying to use function getRealPath() on your $fileNameToStore, which is a string : $filename.'_'.time().'.'.$extension.
getRealPath() will only work on $request()->file('resume')->getRealPath().
If you want to get the information of your file, you should use Uploaded file instance instead or get the file instance you stored.
I am trying to update the database record but Laravel update() function is not working. I have fillable array in the model. but still, it is not working.
The Property Model:
class Property extends Model
{
use HasFactory;
protected $table = 'properties';
protected $primaryKey = 'proID';
public $timestamps = false;
protected $fillable = [ 'proID', 'ProStatus', 'ProPurpose', 'ProType', 'SubType', 'basePrice', 'unitPrice', 'Width', 'Length', 'LandArea','PropertyNumber', 'water', 'electricity', 'gas', 'severage', 'fk_Street', 'createdBy', 'delete'];
public function streets(){
return $this->belongsTo(Street::class,'fk_Street');
}
public function hasInstallments(){
return $this->hasMany(Installments::class,'proID');
}
The PropertyController:
public function destroy($id)
{
$property = Property::find($id);
$property->delete = true;
if($property->save()){
return response()->json(['success'=>true]);
}
}
the $property->update() always returns true but record does not update in database.
The method update() is for mass update wich require an array of attributes and bypass mutators.
public function destroy($id)
{
$property = Property::find($id);
$property->update(['delete' => 1]);
}
You might want to use save() instead
public function destroy($id)
{
$property = Property::find($id);
$property->delete = 1;
$property->save();
}
Both will update the record, you'll need to implement your method's return logic on top of this code but as for updating the record, I think you get the idea.
Your property table primary key is "proID"
public function destroy($id)
{
$property = Property::where('proID', $id)->first();
if($property->update(['delete' => 1])) {
return response()->json(['success' => true]);
}
}
Please what is wrong with this code...am a newbie and this task was given to complete, I watch YouTube and read online but I can seem to figure out how to store records in database ...
This is UserController
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Storage;
use App\User;
class UserController extends Controller {
/**
* #return $this
*/
public function showUsersWithProperty() {
$users = User::all()->filter(function($user) {
return $user->properties->count();
});
return view('settings.manageusers')
->with('users', $users);
}
public function editUser(Request $request, $id){
$users = User::all()->filter(function($user) {
return $user->properties->count();
});
$user = User::find($id);
return view('settings.manageusers')
->with('users', $users)->with('editId', $user->id);
}
public function updateUser(Request $request, $id){
$input = $request;
$user = User::find($input->id);
if ($input->hasFile('userPic')) {
$image = $input->file('userPic');
$realname = pathinfo($input->file('userPic')->getClientOriginalName(), PATHINFO_FILENAME);
$extension = $image->getClientOriginalExtension();
$new_name = time().".".$extension;
$image->storeAs('public/uploads', $new_name);
$path = Storage::url("uploads/" . $new_name);
$user->image_url = $path;
}
$user->first_name = $input->fname;
$user->last_name = $input->lname;
$user->pin = $input->changePin;
if($input->deactivate =="on" || $input->deleteUser =="on"){
$user->account_status = 0;
} else {
$user->account_status = 1;
}
$user->save();
return redirect()->route('manageUsers')->with('status', "User Updated");
} }
This my Route
Route::post('/settings/updateUser/',
'UserController#updateUser')->name("user.update");
Route::get('/settings/updateUser/',
'UserController#updateUser')->name("user.update");
I have trouble saving image or fileupload in laravel 5.4.
the admin is the only one allowed to register a user together with his/her image upon registering the user should receive a confirmation email on his inbox. for other related codes that I think that is related to this controller and the email function or method that could have messed up my code.
paste bin https://pastebin.com/h3Sj7sGN?
//UserController
<?php
namespace App\Http\Controllers;
use App\User;
use Illuminate\Http\Request;
use Illuminate\Support\Str;
use Mail;
use App\Mail\verifyEmail;
class UserController extends Controller
{
/**
* Display a listing of the resource.
*
* #return \Illuminate\Http\Response
*/
public function index()
{
$users = User::all();
//Load all users on the table and pass the users
$users = User::where(['archive'=>1])->orderBy('id')->get();
return view('usercrud.index')->with('users', $users);
}
public function create(Request $request)
{
//
if ($request->file('images') == null)
{
$file = "";
}else{
$filename = $request->images->getClientOriginalName();
$filesize = $request->images->getClientSize();
$file = $request->file('images')->storeAs('images', $filename);
$usersSt = new User;
$usersSt->filename = $filename;
$usersSt->filesize = $filesize;
$usersSt->position = $request->companyPos;
$usersSt->empid = $request->empid;
$usersSt->name = $request->fullname;
$usersSt->email = $request->email;
$usersSt->password = bcrypt($request->password);
$usersSt->roles = $request->role_id;
$usersSt->save();
}
$users = User::all();
$users = User::where(['archive'=>1])->orderBy('id')->get();
$thisUser = User::findOrFail($users->id);
$this->sendEmail($thisUser);
$thisUser = User::findOrFail($users->id);
dd($id);
$this->sendEmail($thisUser);
return redirect()->to('userIndex')->with('users', $users);
return $request->all();
}
public function verifyEmailFirst(Request $request)
{
//
return view('usercrud.verifyEmailFirst');
}
/* public function emailtoken(Request $request)
{
//
return
} */
/**
public function edit($id)
{
//
$users = User::where(['id'=>$id])->first();
// dd($users);
return view('usercrud.edit',compact('users'));
}
public function update(Request $request, $id)
{
//
$usersUp = new User;
$password = bcrypt($request->editpassword);
// dd($request->editcompanyPos);
$usersUp = User::where('id',$id)
->update(['position'=>$request->editcompanyPos,'name'=>$request-
>editfullname,'email'=>$request-
>editemail,'password'=>$password,'roles'=>$request->editrole_id]);
$users = User::all();
$users = User::where(['archive'=>1])->orderBy('id')->get();
return redirect()->to('userIndex')->with('users', $users);
}
public function destroy($id)
{
$userDel = User::where('id',$id)->update(['archive'=>0]);
$users = User::all();
$users = User::where(['archive'=>1])->orderBy('id')->get();
return redirect()->to('userIndex')->with('users', $users);
}
public function sendEmail($thisUser)
{
Mail::to($thisUser['email'])->send(new verifyEmail($thisUser));
}
public function sendEmailDone($email, $verifyToken)
{
$user = User::where(['email'=>$email, 'verifyToken'=>$verifyToken])-
>first();
//return $user;
if ($user){
return user::where(['email'=>$email, 'verifyToken'=>$verifyToken])-
>update(['status'=>'1','verifyToken'=>NULL]);
}else{
return 'user not found';
}
}
}
Do this
public function create(Request $request)
{
//
if ($request->file('images') == null)
{
$file = "";
}else{
$filename = $request->images->getClientOriginalName();
$filesize = $request->images->getClientSize();
$file = $request->file('images')->storeAs('images', $filename);
$usersSt = new User;
$usersSt->filename = $filename;
$usersSt->filesize = $filesize;
$usersSt->position = $request->companyPos;
$usersSt->empid = $request->empid;
$usersSt->name = $request->fullname;
$usersSt->email = $request->email;
$usersSt->password = bcrypt($request->password);
$usersSt->roles = $request->role_id;
$usersSt->save();
}
$this->sendEmail($usersSt);
return redirect()->to('userIndex')->with('users', $usersSt);
}
I'm using Laravel 4 and I'm trying to get the url bar to display the text url that is saved in the database instead of using the id.
This is my routes.php
Route::get('/{id}', function($id = 1){
if(is_numeric($id))
{
$page = Menu::find($id);
$action = 'content';
return App::make('HomeController')->$action($id);
} else {
$column = 'url';
$url = Seo::where($column, '=', $id)->get();
$action = 'show';
return App::make('HomeController')->$action($url[0]->id);
}
});
I'm also using a pivot table to link the menu to the seo.
Seo model
<?php
class Seo extends \Eloquent {
protected $fillable = array('url', 'meta_title', 'meta_description', 'keywords');
protected $guarded = array('id');
protected $table = 'seo';
public static $rules = array(
'title' => '',
'content' => '',
'image' => ''
);
public function menu(){
return $this->belongsToMany('Menu', 'menu_seo', 'seo_id', 'menu_id');
}
}
Menu model
<?php
class Menu extends \Eloquent {
protected $fillable = array('title', 'menu_id', 'image');
protected $guarded = array('id');
protected $table = 'menus';
public static $rules = array(
);
public function seo(){
return $this->belongsToMany('Seo', 'menu_seo', 'menu_id', 'seo_id');
}
}
HomeController
public function content($id)
{
$menus_child = Menu::where('menu_id', 0)->with('menusP')->get();
$menu = Menu::where('id', $id)->firstOrFail();
//dd($menu->frames);
return View::make('index', compact('menus_child'))->with('menu', $menu);
}
and then I call my views that references the menu like this
#foreach($menu->banner as $banners)
{{ $banners->title }}
#endforeach
I'm still not sure how you want to retrieve the correct URL associated to an id but that shouldn't really matter. The basic principle is just to fetch the URL and make a redirect:
Route::get('/{id}', function($id = 1){
if(is_numeric($id))
{
$page = Menu::find($id);
$url = 'foo'; // get correct URL somehow
return Redirect::to($url);
} else {
// ...
}
});