So, what i`m trying to do here is to save an image to an specific user that is logged in. and it gives me this error
<?php
namespace App\Http\Controllers\Auth;
use Auth;
use Illuminate\Http\Request;
use App\Models\ProfileEmployee;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Validator;
class ImageUploadController extends Controller
{
public function index()
{
$profilasdeimage = ProfilasdeEmployee::where('uid', Auth::user()->id)->first();
return vieasdw('editareemasdployee', compact('profileiasdmage'));
}
public function store(Requasdest $request)
{
$emplasdoyee = ProfileEasdmployee::where('uiasdd', Autasdh::user()->id);
if ($request->hasfile('imasdage')){
$file = $request->file('image');
$exteasdnsion = $file->getClientOriginalExtension();
$fileasdname = md5(time()).'.'.$extension;
$fiasdle->move('public/imaginasdeprofil',$filename);
$empasdloyee->imagasde=$filename;
} else {
return $request;
$emplasdoyee->imasdage='';
}
$emplasdoyee->save(); --->> this is the problem
return view('imageuplasdoad')->with('profasdileimage',$emplasdoyee);
}
}
i want to use this database table to fill the 'image' using the id provided from table users as uid in this table
protected $filasdlable = [
'iasdd', 'uiasdd', 'fasdirst_nasdame', 'lasdast_nasdame','phasdone', 'casdv', 'imasdage', 'addasdress', 'ciasdty',
];
Add first() to your query or use find:
$employee = ProfileEmployee::where('uid', Auth::user()->id)->first();
Related
I'm trying to request the $year off my form in my show method but it's returning null. I'm logging time entries and I want to query the times by years like this: $times = Time::where('user_id', $id)->whereYear('start_day', $request->year)->get();. This query works inside of my getReports method, but not in my show method which Is where I want it to work. If I dd($request->all()) inside of my show method It just returns an empty array. I'm not sure how to fix this, I believe it has to do with my route getReport. Is there a way to pass those values into the show method? I just need the $request->year to work so I can query properly.
Routes:
Route::resource('admin/reports', 'Admin\ReportController', [ 'as' => 'admin'])->middleware('admin');
Route::post('admin/reports/getReport', 'Admin\ReportController#getReport')->name('admin.getReport')->middleware('admin');
Form POST:
<form method="POST" action="{{ route('admin.getReport') }}">
Controller:
<?php
namespace App\Http\Controllers\Admin;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use App\Http\Helper\ReportHelper;
use App\Time;
use App\User;
use App\Category;
use App\Offtime;
use Carbon\Carbon;
use Auth;
use Date;
use PDF;
use Session;
class ReportController extends Controller
{
public function create() {
$users = User::all();
$times = Time::all();
$years = array();
$length_times = count($times);
for($i=0;$i<$length_times;$i++){
$year = Carbon::parse($times[$i]->start_day)->format('Y');
array_push($years, $year);
}
$years = array_unique($years);
return view('admin.report.create', compact('users', 'years'));
}
public function show($id, Request $request) {
$user = User::find($id);
if (!$user) {
return false;
}
$times = Time::where('user_id', $id)->get();
return view ('admin.report.show', compact('user','times')
}
public function getReport(Request $request) {
$form_user = $request->form_user;
$year = $request->year;
if (!$form_user) {
return abort(404);
}
$user = User::find($form_user);
if ($user) {
return redirect()->route('admin.reports.show', $user->id);
}
return abort(404);
}
}
For default show uses GET method and you are trying to make a POST.
getReport you defined as post, that's why it's working.
More information about route resource
https://laravel.com/docs/5.7/controllers
In this case, I prefer to use Route Model Binding
public function show(User $user) {
$times = Time::where('user_id', $user->id)->get();
return view ('admin.report.show', compact('user','times')
}
This will handel if the user does not exist.
I want to be able to restrict users to only be able to see their own profiles in my laravel project. So when a user wants to access their profile, they would go to the url followed by /userprofile/{id}. But as of right now, any user can type in the specific id of a different user and access their profile. So if I'm logged in as the first user to register, I would have an id of 1. But I only want to be able to access my profile. If I try to type in id 2 or 3 in the url I want it to kick me back to the homepage. Any idea how I could accomplish this? Using some sort of middleware perhaps?
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
use DB;
use App\User;
use App\Salutation;
use App\Http\Requests\UserRequest;
use Auth;
class HomeController extends Controller
{
public function index(){
$users_info = User::all();
return view('userprofile.index', compact("users_info"));
}
public function show($user_info){
$user_info = User::find($user_info);
return view('userprofile.show', compact("user_info"));
}
public function create(){
return view('userprofile.create');
}
public function store(UserRequest $request){
$formData = $request->all();
User::create($formData);
return redirect('userprofile');
}
public function edit($user_info) {
$user_info = User::findOrFail($user_info);
return view('userprofile.edit', compact("user_info"));
}
public function update(UserRequest $request, $user_info){
$formData = $request->all();
$user_info = User::findOrFail($user_info);
$user_info->update($formData);
return redirect('userprofile');
}
public function __construct(){
$this->middleware('auth', ['only' =>['create', 'edit',
'destroy']]);
}
}
just compare the current user with param id
example:
public function getProfile(Request $request, $id)
{
if(Auth::id() == $id) {
// valid user
$user_info = Auth::user();
return view('userprofile.show', compact("user_info"));
} else {
//not allowed
}
}
I am trying to send a mail with a attachment. Its based on a var from my controller. But I don't know how to pass the content of the variable from my controller to my mail class. My mail view has no problem with using the vars. I need the var in the build method to pick the right file from my directory I generated in my controller.
This returns the following
Missing argument 1 for App\Mail\Aanvraag::build()
Mail class
<?php
namespace App\Mail;
use Illuminate\Bus\Queueable;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Support\Facades\App;
use App\Http\Controllers\aanvragenController;
class Aanvraag extends Mailable
{
use Queueable, SerializesModels;
public $aanvraag;
public function __construct($aanvraag)
{
$this->aanvraag = $aanvraag;
}
/**
* Build the message.
*
* #return $this
*/
public function build()
{
return $this->view('mails.aanvraag')->attach(url('/exports/'. $aanvraag->naam .'sheet.xls'));
}
}
Controller
<?php
namespace App\Http\Controllers;
use App\User;
use Illuminate\Http\Request;
use App\Aanvragen;
use Illuminate\Support\Facades\App;
use Illuminate\Support\Facades\Auth;
use App\Mail\Aanvraag;
class aanvragenController extends Controller
{
public function __construct()
{
$this->middleware('auth');
}
public function index() {
if (Auth::id() == 1) {
$aanvragen = Aanvragen::all();
} else {
$userid = Auth::id();
$aanvragen = Aanvragen::where('user_id', $userid)
->orderBy('id')
->take(10)
->get();
}
return view('aanvragen.index', compact('aanvragen'));
}
public function create() {
return view('aanvragen.create');
}
public function store() {
/* $this->validate(request(), [
'studentnummer' => 'unique:aanvragen'
]);*/
$aanvraag = new Aanvragen;
$aanvraag->naam = request('naam');
$aanvraag->studentnummer = request('studentnummer');
$aanvraag->email = request('email');
$aanvraag->telefoonnummer = request('telefoonnummer');
$aanvraag->stagebedrijf = request('stagebedrijf');
$aanvraag->contactpersoon = request('contactpersoon');
$aanvraag->emailContact = request('emailContact');
$aanvraag->telefoonContact = request('telefoonContact');
$aanvraag->begindatum = request('begindatum');
$aanvraag->einddatum = request('einddatum');
$aanvraag->user_id = Auth::id();
$aanvraag->save();
$data = array (
array('Naam', 'Studentnummer', 'Email', 'Telefoonnummer', 'Stagebedrijf', 'Contactpersoon', 'Email contactpersoon'
, 'Telefoon contactpersoon', 'begindatum', 'einddatum'),
array($aanvraag->naam, $aanvraag->studentnummer, $aanvraag->email, $aanvraag->telefoonnummer, $aanvraag->stagebedrijf,
$aanvraag->contactpersoon, $aanvraag->emailContact, $aanvraag->telefoonContact, $aanvraag->begindatum, $aanvraag->einddatum
)
);
$attachment = $aanvraag->naam;
\Excel::create($aanvraag->naam.'sheet', function ($excel) use ($data) {
$excel->sheet('Sheet1', function ($sheet) use ($data) {
$sheet->fromArray($data);
});
})->save();
\Mail::to('johndoe#info.com')->send(new Aanvraag($aanvraag));
return redirect('/');
}
}
i have created a method to fetch only the soft deleted lessons in my LessonsController
i'm not getting what should be the route my lessoncontroller
<?php
namespace App\Http\Controllers;
use Response;
use App\lesson;
use Illuminate\Http\Request;
use App\Acme\Transformers\LessonTransformer;
use Illuminate\Support\Facades\Input;
class LessonsController extends ApiController
{
protected $lessonTransformer;
function __construct(LessonTransformer $lessonTransformer)
{
$this->lessonTransformer = $lessonTransformer;
}
//fetch all and pass a metadata 'data'
public function index()
{
$lessons = Lesson::all();
return $this->respond([
'data' => $this->lessonTransformer->transformCollection($lessons->all())
]);
}
//delete a lesson by id
public function destroy($id)
{
$dlesson = Lesson::find(input::get('id'));
if(! $dlesson) {
return $this->respondNotFound();
}
$dlesson->delete();
return $this->respondDeleted('Lesson deleted successfully');
}
public function deletedLessons()
{
$deleted_lessons = Lesson::onlyTrashed()->get();
return $this->respond([
'data' => $this->lessonTransformer->transformCollection($lessons->all())
]);
}
}
i have tried with a deleted record like
http://localhost:8000/api/v1/lessons/11
Thank You
Make sure:
You've used softDeletes() method in migration and executed this migration
You're using SoftDeletes trait in the model
You've added deleted_at to $dates property in the model
https://laravel.com/docs/5.3/eloquent#soft-deleting
After doing all that your query will work just fine and will return only soft deleted lessons:
$deleted_lessons = Lesson::onlyTrashed()->get();
I want to retrieve this.
all plan.
all plan without Trial Day.
all plan only trial day.
but i don't know the procedure for take a filter on api Stripe.
https://stripe.com/docs/api#plans
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use Stripe\Plan;
use Stripe\Stripe;
class PlanController extends Controller {
public function index(Request $request)
{
Stripe::setApiKey(env('STRIPE_SECRET'));
$param = ['limit' => 20];
$opt = ['livemode' => false];
$plans = Plan::all($param,$opt);
return plans;
}
}
After you fetch all, you can filter it through php
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use Stripe\Plan;
use Stripe\Stripe;
class PlanController extends Controller {
public function index(Request $request)
{
Stripe::setApiKey(env('STRIPE_SECRET'));
$param = ['limit' => 20];
$opt = ['livemode' => false];
$all_plans = Plan::all($param,$opt);
$trial_plans = [];
$no_trial_plans = []
foreach ($all_plans['data'] as $plan) {
if ($plan['trial_period_days'] == null) {
$no_trial_plans[] = $plan;
}
else {
$trial_plans[] = $plan;
}
}
return [$all_plans, $trial_plans, $no_trial_plans];
}
}