I've store in my application (LARAVEL 5.4) and I want to share some download links in bottom of my product body but to hide it from everyone except who paid for that specific product.
Here is explanation of how I get user data and their request for specific product:
Form of their request:
<form class="form-horizontal" action="{{route('buy_course')}}" method="POST" id="contact_form">
{{ csrf_field() }}
<input name="user_id" value="{{ Auth::user()->id }}" class="form-control" type="hidden">
<input name="course_id" value="{{ $course->id }}" class="form-control" type="hidden">
<div class="form-group">
<label class="col-md-3 control-label">UserName</label>
<div class="col-md-9 inputGroupContainer">
<div class="input-group">
<span class="input-group-addon"><i class="fa fa-user"></i></span>
<input name="username" value="{{ Auth::user()->username }}" class="form-control" type="text" readonly>
</div>
</div>
</div>
<!-- Text input-->
<div class="form-group">
<label class="col-md-3 control-label">E-Mail</label>
<div class="col-md-9 inputGroupContainer">
<div class="input-group">
<span class="input-group-addon"><i class="fa fa-envelope"></i></span>
<input name="email" value="{{ Auth::user()->email }}" class="form-control" type="text" readonly>
</div>
</div>
</div>
<div class="form-group">
<label class="col-md-3 control-label">Course</label>
<div class="col-md-9 inputGroupContainer">
<div class="input-group">
<span class="input-group-addon"><i class="fa fa-graduation-cap"></i></span>
<input name="course" value="{{ $course->course_name }}" class="form-control" type="text" readonly>
</div>
</div>
</div>
<div class="form-group">
<label class="col-md-3 control-label" for="type">Type</label>
<div class="col-md-9 inputGroupContainer">
<div class="input-group">
<span class="input-group-addon"><i class="fa fa-file-code-o"></i></span>
<select class="form-control" id="type" name="type">
<option value="">Select Type</option>
<option value="Download Link">Download Link</option>
<option value="Physical Disk">Physical Disk</option>
</select>
</div>
</div>
</div>
#if (!empty($course->extra_description))
<div class="form-group">
<label class="col-md-3 control-label" for="type">{{ $course->extra_title }}</label>
<div class="col-md-9 inputGroupContainer">
<div class="input-group">
<span class="input-group-addon"><i class="fa fa-file-code-o"></i></span>
<select class="form-control" id="type" name="extra_price">
<option value="">Do you need extra?</option>
<option value="{{$course->extra_price}}">Yes - Add {{$course->extra_price}} Rp</option>
<option value="0">No</option>
</select>
</div>
</div>
</div>
#endif
<div class="form-group">
<label class="col-md-3 control-label">Note to seller</label>
<div class="col-md-9 inputGroupContainer">
<div class="input-group">
<span class="input-group-addon"><i class="fa fa-comment-o"></i></span>
<textarea name="note" id="text" placeholder="Your note to seller here..." class="form-control" rows="8" ></textarea>
</div>
<h6 class="pull-right" id="count_message"></h6>
</div>
</div>
<div class="text-center">
<input type="submit" class="btn btn-block btn-success" value="Send">
</div>
</form>
The controller:
public function postbuycourse(Request $request) {
$this->validate($request, array(
'user_id' => 'required',
'username' => 'required',
'email' => 'required|email',
'course' => 'required',
'note' => 'sometimes|max:500',
'type' => 'required',
));
DB::table('purchases')->insert([
'user_id' => $request->user_id,
'course_id' => $request->course_id,
'note' => $request->note,
'type' => $request->type,
'status' => 0,
'invoice_nu' => str_random(15),
'created_at' => Carbon::now()->format('Y-m-d H:i:s'),
'updated_at' => Carbon::now()->format('Y-m-d H:i:s'),
]);
$data = array(
'username' => $request->username,
'email' => $request->email,
'course' => $request->course,
'note' => $request->note,
'type' => $request->type,
);
Mail::send('emails.buycourse', $data, function($message) use ($data) {
$message->from($data['email']);
$message->to('xxxxxx#xxxxxx.com');
$message->subject($data['course']);
});
$id = $request->course_id;
$course = Course::findOrFail($id);
Mail::to($request->user())->send(new CourseReceived($course));
Session::flash('flash_message', 'Your Order was sent. Our sell team will contact you shortly.');
return redirect()->back();
}
And here is how I show them their order and their order status that if is paid or not:
#if (Auth::user()->purchase->count() )
<h4><i class="fa fa-graduation-cap"></i> Courses Orders</h4>
<table class="mt-20 table table-bordered table-hover table-responsive">
<thead>
<tr class="bg-primary">
<th class="text-center">ID</th>
<th class="text-center">Invoice Number</th>
<th class="text-center">Course Name</th>
<th class="text-center">Note</th>
<th class="text-center">Sum</th>
<th class="text-center">Status</th>
<th class="text-center">Time</th>
<th class="text-center">Expire</th>
<th class="text-center">Options</th>
</tr>
</thead>
<tbody class="text-center">
#foreach ($purchases as $purchase)
<tr>
<td id="primary">{{$purchase->id}}</td>
<td>{{$purchase->invoice_nu}}</td>
<td>{{$purchase->course->course_name}}</td>
<td>
#if(!empty($purchase->note))
{{ $purchase->note }}
#else
-
#endif
</td>
<td>
#if( ! empty($purchase->course->course_disscount))
<span class="text-primary">Price: <del>{{$purchase->course->course_price}}</del></span><br>
<span class="text-danger">disscount: {{$purchase->course->course_disscount}}</span>
<hr>
<span class="text-success">Total: {{ number_format($purchase->course->course_price - $purchase->course->course_disscount, 0) }} Rp</span>
#else
{{ number_format($purchase->course->course_price, 0) }} Rp
#endif
</td>
<td>
#if($purchase->status == 0)
<span class="text-danger">Waiting Payment</span>
#else
<span class="text-success">Paid</span>
#endif
</td>
<td>{{ $purchase->created_at->format('d, M, Y | h:i A') }}</td>
<td>
#if($purchase->status == 0)
<span class="text-danger">-</span>
#else
<span class="text-success">{{ $purchase->created_at->format('d, M, Y | h:i A') }}</span>
#endif
</td>
<td>
Payment
<i class="fa fa-pencil"></i> Edit
{!! Form::open(['method' => 'DELETE', 'route' => ['userscourses.destroy', $purchase->id] ]) !!}
{!! Form::submit('Delete', ['class' => 'btn btn-sm btn-danger btn-block mt-20']) !!}
{!! Form::close() !!}
</td>
</tr>
#endforeach
</tbody>
</table>
Thanks.
You can use laravel Gate and policies features for achieving that, You can read the documentation once.
Related
i am a beginner of laravel. i ran into the problem with Route [student.update] not defined. using laravel 7. when run the laravel project. what i tried so far i attached below.
i attached the controller and view and route file below i don't what was a problem.
Controller
public function edit(Student $student)
{
return view('edit')->with('student',$student);
}
public function update(Request $request, Student $student)
{
Student::update([
'name' => $request->name,
'phone' => $request->phone,
'address' => $request->address,
'created_at' => now(),
]);
return redirect()->route('student.index')->with('success', 'Student has been Updatedddd');
}
edit.blade.php
<form action="{{ route('student.update',$student->id) }}" method="POST">
#csrf
<div class="row">
<div class="col-xs-12 col-sm-12 col-md-12">
<div class="form-group">
<strong>Name:</strong>
<input type="text" name="name" value="{{ $student->name }}" class="form-control" placeholder="Name">
</div>
</div>
<div class="col-xs-12 col-sm-12 col-md-12">
<div class="form-group">
<strong>Phone:</strong>
<input class="form-control" name="phone" value="{{ $student->phone }}" placeholder="Phone"></textarea>
</div>
</div>
<div class="col-xs-12 col-sm-12 col-md-12">
<div class="form-group">
<strong>Address:</strong>
<input class="form-control" name="address" value="{{ $student->address }}" placeholder="Address"></textarea>
</div>
</div>
<div class="col-xs-12 col-sm-12 col-md-12 text-center">
<button type="submit" class="btn btn-primary">Submit</button>
</div>
</div>
</form>
index.blade.php
#extends('layout')
#section('content')
<div class="row">
<div class="col-lg-12 margin-tb">
<div class="pull-left">
<h2>Laravel 7 CRUD Example from scratch - ItSolutionStuff.com</h2>
</div>
<div class="pull-right">
<a class="btn btn-success" href="{{ route('student.create')}}"> Create New Student</a>
</div>
</div>
</div>
#if ($message = Session::get('success'))
<div class="alert alert-success">
<p>{{ $message }}</p>
</div>
#endif
<table class="table table-bordered">
<tr>
<th>No</th>
<th>Name</th>
<th>Phone</th>
<th>Address</th>
<th width="280px">Action</th>
</tr>
#foreach ($students as $student)
<tr>
<td>{{ $student->id }}</td>
<td>{{ $student->name }}</td>
<td>{{ $student->phone }}</td>
<td>{{ $student->address }}</td>
<td>
<a class="btn btn-primary" href="{{ route('student.edit',$student->id) }}">Edit</a>
<button type="submit" class="btn btn-danger">Delete</button>
</td>
</tr>
#endforeach
</table>
{!! $students->links() !!}
#endsection
Routes
Route::get('/students/{student}', 'StudentController#edit')->name('student.edit');
Route::get('/students/{student}', 'StudentController#update')->name('student.update');
Your update route is defined as a get route while your edit form is trying to submit a post request to the route
You should ideally have the update route defined as a PUT or PATCH route. And if you are using Laravel 8.x, then you should have FQCN for the controllers
//import use statements at the top
//use Illuminate\Support\Facades\Route;
//use App\Http\Controllers\StudentController;
Route::match(['PUT', 'PATCH'], '/students/{student}', [StudentController::class, 'update'])->name('student.update');
And then make a PUT or PATCH submit request from edit.blade.php
<form action="{{ route('student.update',$student->id) }}" method="POST">
#csrf
#method('PUT') //Method spoofing
<div class="row">
<div class="col-xs-12 col-sm-12 col-md-12">
<div class="form-group">
<strong>Name:</strong>
<input type="text" name="name" value="{{ $student->name }}" class="form-control" placeholder="Name">
</div>
</div>
<div class="col-xs-12 col-sm-12 col-md-12">
<div class="form-group">
<strong>Phone:</strong>
<input class="form-control" name="phone" value="{{ $student->phone }}" placeholder="Phone"></textarea>
</div>
</div>
<div class="col-xs-12 col-sm-12 col-md-12">
<div class="form-group">
<strong>Address:</strong>
<input class="form-control" name="address" value="{{ $student->address }}" placeholder="Address"></textarea>
</div>
</div>
<div class="col-xs-12 col-sm-12 col-md-12 text-center">
<button type="submit" class="btn btn-primary">Submit</button>
</div>
</div>
</form>
And change the controller method
public function update(Request $request, Student $student)
{
$student->update([
'name' => $request->name,
'phone' => $request->phone,
'address' => $request->address,
'created_at' => now(),
]);
return redirect()->route('student.index')->with('success', 'Student has been Updatedddd');
}
I'm trying a Laravel 5.6 request validation.I face several problems
1.Cannot view validation Messages in view.
2.After adding validation code data not insert to the database.(before adding validation it's work)
3 How can I validation dropdown status ?(Should select Active/Inactive)
designation.blade.php
<form action="{{url('./designation/store')}}" method="POST">
<div class="form-row">
<div class="form-group col-md-7">
<label for="inputDesignation">Designation</label>
<input type="text" name="designation" class="form-control" id="inputDesignation">
</div>
<div class="form-group col-md-5">
<label for="inputStatus_Designation">Status</label>
<select name="status" id="inputStatus_Designation" class="form-control">
<option selected>Select Status</option>
<option >Active</option>
<option >Inactive</option>
</select>
</div>
</div>
<button type="submit" class="btn btn-success" id="btn_add_designation">Add</button>
{{ csrf_field() }}
</form>
DesignationController.php
public function store(Request $request)
{
//Validation the Data
$validatedData = $request->validate([
'designation_type' => ['required','max:255'],
'status' => ['required'],
],
[
'designation_type.required' => 'Designation is required',
'designation_type.max' => 'Designation should not be greater than 255 characters.',
]);
//Data Insert into database
$data =[
'designation_type'=>$request->input('designation'),
'status'=>$request->input('status')
];
DB::table('designation')->insert($data);
return redirect('/designation');
}
Please help me to solve this !!
try following code
public function store(Request $request)
{
//Validation the Data
$validatedData = $request->validate([
'designation_type' => ['required','max:255'],
'status' => ['required'],
],
[
'designation_type.required' => 'Designation is required',
'designation_type.max' => 'Designation should not be greater than 255 characters.',
]);
if($validatedData->fails()) {
return Redirect::back()->withErrors($validatedData);
}
//Data Insert into database
$data =[
'designation_type'=>$request->input('designation'),
'status'=>$request->input('status')
];
DB::table('designation')->insert($data);
return redirect('/designation');
}
<form action="{{url('./designation/store')}}" method="POST">
<div class="form-row">
<div class="form-group col-md-7">
<label for="inputDesignation">Designation</label>
<input type="text" name="designation" class="form-control" id="inputDesignation">
#if($errors->has('designation'))
<div class="error">{{ $errors->first('designation') }}</div>
#endif
</div>
<div class="form-group col-md-5">
<label for="inputStatus_Designation">Status</label>
<select name="status" id="inputStatus_Designation" class="form-control">
<option selected>Select Status</option>
<option >Active</option>
<option >Inactive</option>
</select>
#if($errors->has('status'))
<div class="error">{{ $errors->first('status') }}</div>
#endif
</div>
</div>
<button type="submit" class="btn btn-success" id="btn_add_designation">Add</button>
{{ csrf_field() }}
</form>
public function store(Request $request)
{
$this->validate($request,[
'name' => 'required'
]);
$category = new Category();
$category->name = $request->name;
$category->slug = str_slug($request->name);
$category->save();
Toastr::success('Category Successfully Saved','Success');
return redirect()->route('admin.category.index');
}
// blade
<form method="POST" action="{{ route('admin.category.store') }}">
#csrf
<div class="form-group form-float">
<div class="form-line">
<input value="{{ old('name') }}" name="name" type="text" id="category_name" class="form-control">
<label class="form-label">{{ __('Name') }}</label>
</div>
</div>
<br>
{{ __('BACK') }}
<button type="submit" class="btn btn-primary m-t-15 waves-effect">{{ __('SUBMIT') }}</button>
</form>
After refer this https://laravel.com/docs/5.6/validation#named-error-bags I did few changes in the code and it's help to solved errors.
In designation.blade.php added
#if ($errors->any())
<div class="alert alert-danger">
<ul>
#foreach ($errors->all() as $error)
<h6>{{ $error }}</h6>
#endforeach
</ul>
</div>
#endif
In DesignationController.php
for status dropdown validation.
'status' => 'required|not_in:0',
for data insert database part
DB::table('designation')->insert($validatedData);
Full Code
designation.blade.php
<div class="row">
<div class="col-md-2"></div>
<div class="col-md-8">
#if ($errors->any())
<div class="alert alert-danger">
<ul>
#foreach ($errors->all() as $error)
<h6>{{ $error }}</h6>
#endforeach
</ul>
</div>
#endif
</div>
<div class="col-md-2"></div>
</div>
<div class="row">
<div class="col-md-4"></div>
<div class="col-md-4">
<form action="{{url('./designation/store')}}" method="POST">
<div class="form-row">
<div class="form-group col-md-7">
<label for="inputDesignation">Designation</label>
<input type="text" name="designation_type" class="form-control" id="inputDesignation">
</div>
<div class="form-group col-md-5">
<label for="inputStatus_Designation">Status</label>
<select name="status" id="inputStatus_Designation" class="form-control">
<option selected value="">Select Status</option>
<option value="Active">Active</option>
<option value="Inactive">Inactive</option>
</select>
</div>
</div>
{{-- <button type="submit" class="btn btn-primary">Sign in</button> --}}
<button type="submit" class="btn btn-success" id="btn_add_designation">Add</button>
{{ csrf_field() }}
</form>
</div>
<div class="col-md-4"></div>
</div>
DesignationController.php
public function store(Request $request)
{
$validatedData = $request->validate([
'designation_type' => 'required|max:255',
'status' => 'required|not_in:0',
],
[
'designation_type.required' => 'Designation is required !!',
'designation_type.max' => 'Designation should not be greater than 255 characters.',
'status.required' => 'Status is required !!'
]);
DB::table('designation')->insert($validatedData);
return redirect('/designation');
}
Thank You Guys spend your valuable time to help me....!!!
I want to send an attachment with email. Till now I am able to send the attachment with mail but the problem is when I am downloading attachment it will show HTML code of my login page.
Please help me to figure it out why attachment show loging page data
Below is my code for compose.blade
<form class="form-horizontal" method="post" action="{{ action('ServiceRequestEmailController#store') }}" enctype="multipart/form-data">
<div class="mt20 clearfix text-right action-btn-btm">
<button class="btn btn-primary">Send</button>
<a class="btn btn-danger" href="{{ url('superadmin/service-request/view/' . $ServiceRequest->id) }}">Discard</a>
<div class=" border-bottom"></div>
</div>
{{ csrf_field() }}
<input type="hidden" name="sr_id" value="{{ $ServiceRequest->id }}">
<input type="hidden" name="parent_id" value="{{ $mail->getParentId() }}" />
<input type="hidden" name="action" value="{{ $mail->getAction() }}" />
<div class="form-group">
<label class="col-form-label example-text-input col-sm-4 col-md-2" for="email">Service Request:</label>
<div class="col-sm-8 col-md-6"><span>{{ $ServiceRequest->id }}</span></div>
</div>
<div class="form-group">
<label class="col-form-label example-text-input col-sm-4 col-md-2">Consultant Name:</label>
<div class="col-sm-8 col-md-6"> <span>{{ $ServiceRequest->name }}</span> </div>
</div>
<div class="form-group">
<label class="col-form-label example-text-input col-sm-4 col-md-2">Consultant Code:</label>
<div class="col-sm-8 col-md-6"> <span>{{ $ServiceRequest->mca_no }}</span> </div>
</div>
<div class="form-group">
<label class="col-form-label example-text-input col-sm-4 col-md-2" for="from">From:</label>
<div class="col-sm-8 col-md-6">
<select class="form-control" name="from">
#foreach ( $mail->from() as $email )
<option>{{ $email }}</option>
#endforeach
</select>
</div>
</div>
<div class="form-group autocomplete-to">
<label class="col-form-label example-text-input col-sm-4 col-md-2" for="to">To:</label>
<div class="col-sm-8 col-md-6">
<input type="text" class="form-control" name="to" id="to" value="{{ old('to') ?: $mail->to() }}" />
#if ($errors->has('to.*'))
<p class="error-msg">{{ $errors->first('to.*') }}</p>
#endif
</div>
</div>
<div class="form-group autocomplete-to">
<label class="col-form-label example-text-input col-sm-4 col-md-2" for="cc">Cc:</label>
<div class="col-sm-8 col-md-6">
<input type="text" class="form-control" name="cc" id="cc" value="{{ old('cc') ?: $mail->cc() }}" />
#if ($errors->has('cc.*'))
<p class="error-msg">{{ $errors->first('cc.*') }}</p>
#endif
</div>
</div>
<div class="form-group autocomplete-to">
<label class="col-form-label example-text-input col-sm-4 col-md-2" for="bcc">Bcc:</label>
<div class="col-sm-8 col-md-6">
<input type="text" class="form-control" name="bcc" id="bcc" value="{{ old('bcc') ?: $mail->bcc() }}" />
#if ($errors->has('bcc.*'))
<p class="error-msg">{{ $errors->first('bcc.*') }}</p>
#endif
</div>
</div>
<div class="form-group">
<label class="col-form-label example-text-input col-sm-4 col-md-2" for="subject">Subject:</label>
<div class="col-sm-8 col-md-6">
<input type="text" class="form-control" name="subject" value="{{ old('subject') ?: $mail->getSubject() }}" />
#if ($errors->has('subject'))
<p class="error-msg">{{ $errors->first('subject') }}</p>
#endif
</div>
</div>
<div class="form-group">
<label class="col-form-label example-text-input col-sm-4 col-md-2" for="template">Email Template:</label>
<div class="col-sm-8 col-md-6">
<select class="form-control" id="template" v-model="template" v-on:change="loadTemplate()">
<option value="">None</option>
#foreach ($templates as $template)
<option value="{{ $template->id }}">{{ $template->name }}</option>
#endforeach
</select>
</div>
</div>
<div class="form-group">
<label class="col-form-label example-text-input col-sm-4 col-md-2" for="content">Email Text:</label>
<div class="col-sm-8 col-md-9">
<main>
<div class="adjoined-bottom">
<div class="grid-container">
<div class="grid-width-100">
<textarea name="content" id="editor1" rows="10" cols="80"><?php echo nl2br($mail->getBody()); ?></textarea>
</div>
</div>
</div>
</main>
#if ($errors->has('content'))
<p class="error-msg">{{ $errors->first('content') }}</p>
#endif
</div>
</div>
<div class="form-group">
<label class="col-form-label example-text-input col-sm-4 col-md-2" for="email">File Upload:</label>
<div class="col-sm-8 col-md-6">
<div class="attachments">
<input type="file" name="attachments[]" class="form-control" />
</div>
<div class="clearfix">
<button type="button" class="btn btn-primary" data-action="add-more-attachment">Add More</button>
</div>
</div>
</div>
<div> </div>
#if ($mail->getAction() == 'FORWARD' && ($mailModel = $mail->getInstance()))
<div class="gen-info mt20">
<div class="col-md-12">
<h3 class="subheading icon-open-close" data-toggle="collapse" data-target="#locale-setting">Attachments</h3>
</div>
<div class="col-md-12 collapse in" id="locale-setting">
<div class="clearfix">
<table class="table table-striped">
<thead>
<tr>
<th>Action</th>
<th>File Name</th>
</tr>
</thead>
<tbody>
#if (($attachments = $mailModel->attachments) && !$attachments->isEmpty())
#foreach ($attachments as $attachment)
<tr>
<td><a target="_blank" href="{{ url('public/attachments/' . $attachment->filename) }}" target="_blank">Preview</a></td>
<td>{{ $attachment->original_filename }}</td>
</tr>
#endforeach
#else
<tr>
<td colspan="2" align="center">No Attachments Found</td>
</tr>
#endif
</tbody>
</table>
</div>
</div>
</div>
#endif
<div class="form-group">
<label class="col-form-label example-text-input col-sm-4 col-md-2" for="email"></label>
<div class="col-sm-8 col-md-6">
<button class="btn btn-primary">Send</button>
<a class="btn btn-danger" href="{{ url('superadmin/service-request/view/' . $ServiceRequest->id) }}">Discard</a>
</div>
</div>
</form>
and code for controller
public function compose(ServiceRequest $ServiceRequest)
{
$templates = MailTemplate::where('org_id', $ServiceRequest->org_id)->get();
$mail = new Compose($ServiceRequest);
return view('admin.ServiceRequestEmails.compose')
->with(compact('ServiceRequest', 'mail', 'templates'));
}
public function reply(MailModel $mail)
{
$ServiceRequest = $mail->ServiceRequest;
$templates = MailTemplate::where('org_id', $ServiceRequest->org_id)->get();
return view('admin.ServiceRequestEmails.compose')
->with([
'templates' => $templates,
'mail' => new Reply($mail),
'ServiceRequest' => $ServiceRequest,
]);
}
public function replyToAll(MailModel $mail)
{
$ServiceRequest = $mail->ServiceRequest;
//print_r($ServiceRequest);
$templates = MailTemplate::where('org_id', $ServiceRequest->org_id)->get();
//print_r($templates);
//die();
return view('admin.ServiceRequestEmails.compose')
->with([
'templates' => $templates,
'mail' => new ReplyToAll($mail),
'ServiceRequest' => $ServiceRequest,
]);
}
public function forward(MailModel $mail)
{
$ServiceRequest = $mail->ServiceRequest;
$templates = MailTemplate::where('org_id', $ServiceRequest->org_id)->get();
return view('admin.ServiceRequestEmails.compose')
->with([
'templates' => $templates,
'mail' => new Forward($mail),
'ServiceRequest' => $ServiceRequest,
]);
}
public function store(Request $request)
{
$this->request = $request;
(new \App\Modicare\Validators\ServiceRequestEmailValidator($this->request))
->validate();
$ServiceRequest = ServiceRequest::find($request->sr_id);
$senderName = $this->getSenderNameByEmail($request->from);
$this->mail = MailModel::create([
'sr_id' => $request->sr_id,
'parent_id' => $request->parent_id,
'sender_name' => $senderName,
'from' => $request->from,
'to' => $request->to,
'cc' => $request->cc,
'bcc' => $request->bcc,
'subject' => $request->subject,
'body' => $request->content,
'mca_no' => $ServiceRequest->mca_no,
'sent_at' => date('Y-m-d H:i:s'),
'status' => 'SENT',
'type' => 'OUT'
]);
if ($request->parent_id && ($mail = MailModel::find($request->parent_id))) {
$mail->status = 'REPLIED';
$mail->save();
}
$mailSender = MailFacade::to(explode(',', $request->to));
if ($request->cc) {
$mailSender->cc(explode(',', $request->cc));
}
if ($request->bcc) {
$mailSender->bcc(explode(',', $request->bcc));
}
if (!empty($request->attachments) && is_array($request->attachments))
$files = $this->saveAttachments($request->attachments);
$this->handleForwardMail();
$mailSender->send(new ServiceRequestReply($this->mail));
return redirect('superadmin/service-request/view/' . $request->sr_id);
}
private function getSenderNameByEmail($email)
{
// $organizationEmail = OrganizationEmail::where('email', $email) -> orderBy ('id', 'desc')
//;
$organizationEmail = OrganizationEmail::where('email', $email)
->first();
if ($organizationEmail) {
return $organizationEmail->organization->getName();
}
return Auth::User()->getName();
}
public function view(MailModel $mail)
{
if ($mail->status == 'NEW') {
$mail->status = 'SEEN';
$mail->save();
}
return view('admin.ServiceRequestEmails.view', compact('mail'));
}
public function print(MailModel $mail)
{
return view('admin.ServiceRequestEmails.print', compact('mail'));
}
private function handleForwardMail()
{
if ($this->request->action == 'FORWARD' &&
($parent = $this->mail->parent)) {
foreach ($parent->attachments as $attachment) {
$newAttachment = $attachment->replicate();
$newAttachment->ref_id = $this->mail->id;
$newAttachment->save();
}
}
}
private function saveAttachments(array $attachments)
{
$files = [];
foreach ($attachments as $key => $attachment) {
$files[$key] = [
'newFilename' => $attachment->store('attachments', 'public'),
'originalFilename' => $attachment->getClientOriginalName(),
];
Attachment::create($attachment->getRealPath(),[
'type' => 'MAIL',
'ref_id' => $this->mail->id,
'original_filename' => $attachment->getClientOriginalName(),
'filename' => basename($files[$key]['newFilename']),
'extn' => $attachment->getClientOriginalExtension(),
'mime_type' => $attachment->getClientMimeType(),
'size' => $attachment->getClientSize(),
]);
}
}
I had a Laravel script installed
Now I want to go to the services list page, but with the "Trying to get the property 'name' of non-object" encountered.
please help me
My error:
Trying to get property 'name' of non-object (View: /mylocalhost/core/resources/views/admin/service.blade.php)
in line 36:
category->name); ?>
service.blade.php code:
#section('page_name', 'Services')
<a class="btn btn-outline-success btn-lg" href="#" data-toggle="modal" data-target="#addService"><i
class="fa fa-plus-circle"></i> Add Service</a>
#endsection
#section('body')
<ul class="orders-ul">
<li class="text-color"><a class="btn btn-outline-secondary" href="{{ route('api.service') }}" target="_blank">API Services List</a></li>
</ul>
<div class="row">
#include('admin.layouts.flash')
<div class="col-md-12">
<div class="tile">
<h3 class="tile-title">Services</h3>
<table class="table table-hover">
<thead>
<tr>
<th>Serial</th>
<th>Name</th>
<th>Category</th>
<th>Price per 1K</th>
<th>Min</th>
<th>Max</th>
<th>Order Response</th>
<th>Status</th>
<th>Action</th>
</tr>
</thead>
<tbody>
#foreach($items as $key => $item)
<tr>
<td>{{ $key+1 }}</td>
<td>{{ $item->name }}</td>
<td>{{ $item->category->name }}</td>
<td>{{ $item->price_per_k }}</td>
<td>{{ $item->min }}</td>
<td>{{ $item->max }}</td>
<td>{{ $item->service_id == 0? 'Manual' : 'API' }}</td>
<td>
{!! $item->status == 0 ? '<b style="color:#d35400">Inactive</b>' : '<b style="color:#16a085">Active</b>' !!}
</td>
<td>
<button class="btn btn-outline-info service_edit_btn"
data-toggle="modal"
data-target="#editService"
data-route="{{ route('service.update', $item->id) }}"
data-category="{{ $item->category_id }}"
data-name="{{ $item->name }}"
data-price="{{ $item->price_per_k }}"
data-min="{{ $item->min }}"
data-max="{{ $item->max }}"
data-service_id="{{ $item->service_id }}"
data-details="{{ $item->details }}"
data-status="{{ $item->status }}">
<i class="fa fa-edit`enter code here`"></i></button>
<button class="btn btn-outline-danger service_dlt_btn"
data-toggle="modal"
data-target="#delService"
data-route="{{ route('service.delete', $item->id) }}">
<i class="fa fa-trash"></i></button>
</td>
</tr>
#endforeach
</tbody>
</table>
</div>
</div>
</div>
<div class="modal fade" id="addService" tabindex="-1" role="basic" aria-hidden="true">
<div class="modal-dialog modal-lg">
<div class="modal-content">
<div class="modal-header">
<h4><b>Add Service</b></h4>
</div>
<div class="modal-body">
<div class="portlet light bordered">
<div class="portlet-body form">
<form role="form" action="{{ route('service.store') }}" method="post">
#csrf
<div class="form-body">
<div class="form-group">
<label for=""><b>Category</b></label>
<select name="category_id" class="form-control form-control-lg">
#foreach($categories as $category)
<option value="{{ $category->id }}">{{ $category->name }}</option>
#endforeach
</select>
</div>
<div class="form-group">
<label for=""><b>Name</b></label>
<input type="text" name="name" class="form-control form-control-lg">
</div>
<div class="form-group">
<label for=""><b>Price per 1k</b></label>
<div class="input-group">
<input type="text" name="price_per_k" class="form-control form-control-lg">
<div class="input-group-append">
<span class="input-group-text">{{ $general->currency_symbol }}</span>
</div>
</div>
</div>
<div class="row">
<div class="form-group col-md-6">
<label for=""><b>Min</b></label>
<input type="text" name="min" class="form-control form-control-lg">
</div>
<div class="form-group col-md-6">
<label for=""><b>Max</b></label>
<input type="text" name="max" class="form-control form-control-lg">
</div>
</div>
<div class="form-group">
<label for=""><b>Details</b></label>
<textarea class="form-control" name="details" rows="8"></textarea>
</div>
<div class="form-group">
<label for=""><b>service Id (If order process through API)</b></label>
<input type="text" name="service_id" class="form-control form-control-lg">
</div>
<div class="form-group">
<label for=""><b>Status</b></label>
<input data-toggle="toggle" data-onstyle="success" data-offstyle="danger"
data-on="Active" data-off="Inactive" data-width="100%" type="checkbox" name="status" value="1">
</div>
</div>
<div class="tile-footer">
<button class="btn btn-primary btn-block btn-lg" type="submit"><i
class="fa fa-fw fa-lg fa-check-circle"></i>Save
</button>
</div>
</form>
</div>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-danger" data-dismiss="modal">Cancel</button>
</div>
</div>
<!-- /.modal-content -->
</div>
<!-- /.modal-dialog -->
</div>
<div class="modal fade" id="editService" tabindex="-1" role="basic" aria-hidden="true">
<div class="modal-dialog modal-lg">
<div class="modal-content">
<div class="modal-header">
<h4><b>Edit Service</b></h4>
</div>
<div class="modal-body">
<div class="portlet light bordered">
<div class="portlet-body form">
<form role="form" action="" method="post"
id="serviceEditdForm">
#csrf
#method('put')
<div class="form-body">
<div class="form-group">
<label for=""><b>Category</b></label>
<select name="category_id" id="category_id" class="form-control form-control-lg">
#foreach($categories as $category)
<option value="{{ $category->id }}">{{ $category->name }}</option>
#endforeach
</select>
</div>
<div class="form-group">
<label for=""><b>Name</b></label>
<input type="text" name="name" id="name" class="form-control form-control-lg">
</div>
<div class="form-group">
<label for=""><b>Price per 1k</b></label>
<div class="input-group">
<input type="text" name="price_per_k" id="price_per_k" class="form-control form-control-lg">
<div class="input-group-append">
<span class="input-group-text">{{ $general->currency_symbol }}</span>
</div>
</div>
</div>
<div class="row">
<div class="form-group col-md-6">
<label for=""><b>Min</b></label>
<input type="text" name="min" id="min" class="form-control form-control-lg">
</div>
<div class="form-group col-md-6">
<label for=""><b>Max</b></label>
<input type="text" name="max" id="max" class="form-control form-control-lg">
</div>
</div>
<div class="form-group">
<label for=""><b>Details</b></label>
<textarea class="form-control" name="details" id="details" rows="8"></textarea>
</div>
<div class="form-group">
<label for=""><b>service Id (If order process through API)</b></label>
<input type="text" name="service_id" id="service_id" class="form-control form-control-lg">
</div>
<div class="form-group">
<label for=""><b>Status</b></label>
<input data-toggle="toggle" data-onstyle="success" data-offstyle="danger"
data-on="Active" data-off="Inactive" data-width="100%" type="checkbox" name="status" id="status" value="1">
</div>
</div>
<div class="tile-footer">
<button class="btn btn-primary btn-block btn-lg" type="submit"><i
class="fa fa-fw fa-lg fa-check-circle"></i>Save
</button>
</div>
</form>
</div>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-danger" data-dismiss="modal">Cancel</button>
</div>
</div>
<!-- /.modal-content -->
</div>
<!-- /.modal-dialog -->
</div>
<div class="modal fade" id="delService">
<div class="modal-dialog modal-dialog-centered">
<div class="modal-content">
<div class="modal-body">
<form action="" method="post" id="serviceDelForm">
#csrf
#method('delete')
<h3 class="d-flex justify-content-center">Are you want to delete this Service?</h3>
<div class="tile-footer d-flex justify-content-center">
<button class="btn btn-primary" type="submit"><i class="fa fa-fw fa-lg fa-check-circle"></i>Confirm
</button>
<a class="btn btn-danger" href="#" data-dismiss="modal"><i
class="fa fa-fw fa-lg fa-times-circle"></i>Cancel</a>
</div>
</form>
</div>
</div>
</div>
</div>
#endsection
#section('scripts')
<script type="text/javascript">
$(document).ready(function () {
$(document).on('click', '.service_edit_btn', function () {
var route = $(this).data('route');
var category = $(this).data('category');
var name = $(this).data('name');
var price = $(this).data('price');
var min = $(this).data('min');
var max = $(this).data('max');
var service_id = $(this).data('service_id');
var details = $(this).data('details');
var status = $(this).data('status');
$('#category_id').val(category).attr('selected', 'selected');
$('#name').val(name);
$('#price_per_k').val(price);
$('#min').val(min);
$('#max').val(max);
$('#service_id').val(service_id);
$('#details').val(details);
$('#serviceEditdForm').attr('action',route);
if(status == 1){
$('#status').bootstrapToggle('on');
}else{
$('#status').bootstrapToggle('off');
}
});
$(document).on('click', '.service_dlt_btn', function () {
var route = $(this).data('route');
$('#serviceDelForm').attr('action', route);
});
});
</script>
#endsection
And service list controller in Admin controller:
public function service(){
$categories = Category::orderBy('name')->get();
$items = Service::orderBy('category_id')->paginate(10);
return view('admin.service', compact('items', 'categories'));
}
public function serviceStore(Request $request){
$this->validate($request, [
'category_id' => 'required',
'name' => 'required|unique:services,name',
'price_per_k' => 'required',
'min' => 'required',
'max' => 'required',
'details' => 'required',
], [
'category_id.required' => 'Category name is required',
'price_per_k.required' => 'price per 1k is required',
]);
$excp = $request->except('_token', 'status', 'service_id');
$status = $request->status == 1? 1:0;
if(isset($request->service_id)){
$service_id = $request->service_id;
}else{
$service_id = 0;
}
$sev = Service::create($excp + ['status' => $status, 'service_id' => $service_id]);
$users = User::all();
foreach ($users as $user){
$servicePrice = new ServicePrice();
$servicePrice->category_id = $sev->category_id;
$servicePrice->service_id = $sev->id;
$servicePrice->user_id = $user->id;
$servicePrice->price = $sev->price_per_k;
$servicePrice->save();
}
session()->flash('success', 'Service Stored successfully');
return back();
}
public function serviceUpdate(Request $request, $id){
$this->validate($request, [
'category_id' => 'required',
'name' => 'required',
'price_per_k' => 'required',
'min' => 'required',
'max' => 'required',
'details' => 'required',
], [
'category_id.required' => 'Category name is required',
'price_per_k.required' => 'price per 1k is required',
]);
$excp = $request->except('_token', 'status', 'service_id');
$status = $request->status == 1? 1:0;
if(isset($request->service_id)){
$service_id = $request->service_id;
}else{
$service_id = 0;
}
Service::findOrFail($id)->update($excp + ['status' => $status, 'service_id' => $service_id]);
session()->flash('success', 'Service Updated successfully');
return back();
}
public function serviceDelete($id){
Service::findOrFail($id)->delete();
session()->flash('success', 'Service deleted successfully');
return back();
}
This means your item don't have category, So you can do something like this.
<td>
#if(!empty($item->category)) {{ $item->category->name }} #endif
</td>
Other way to solve this problem using php7 is to use null coalescing operator which:
It returns its first operand if it exists and is not NULL; otherwise it returns its second operand.
<td>{{ $item->category->name ?? '' }}</td>
I have a form in my Laravel 4 web app that is refusing to submit to the database. Each time i try to submit, the page just reloads and i see no error messages even in the laravel log. I have spent two days trying to figure out what the problem is as I can't seem to see anything wrong with the code.
Any help will be appreciated.
/**** THE FORM VIEW ***/
<div class="container">
<div class="row">
#if(Session::has('success'))
<div class="alert alert-success">
<button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">×</span></button>
{{Session::get('success')}}
</div>
#elseif(Session::has('fail'))
<div class="alert alert-danger">
<button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">×</span></button>
{{Session::get('fail')}}
</div>
#endif
</div>
#include('partials.admin-navbar')
<div class="admin_profile_content">
<form class="" method="post" action=" {{URL::route('postSubmitCompetition')}}">
<div class="" style="width:80%; margin:auto;">
<div class="panel panel-default">
<div class="panel-heading">Organization </div>
<div class="panel-body">
<div class="form-group">
<label for="" class="col-sm-2 control-label">Select the Organization hosting this competition </label>
<div class="col-sm-10">
{{ Form::select('organization', $organizations, null, ['class' => 'form-control']) }}
<p class="text-danger">
#if($errors->has('organization'))
{{ $errors->first('organization') }}
#endif
</p>
</div>
</div>
</div>
</div>
<div class="panel panel-default">
<div class="panel-heading">Competition name </div>
<div class="panel-body">
<div class="form-group">
<label for="" class="col-sm-2 control-label">Competition name </label>
<div class="col-sm-10">
<input type="text" class="form-control" id="" name="competition_name">
<p class="text-danger">
#if($errors->has('competition_name'))
{{ $errors->first('competition_name') }}
#endif
</p>
</div>
</div>
</div>
</div>
<div class="panel panel-default">
<div class="panel-heading">Prizes </div>
<div class="panel-body">
<div class="form-group">
<label for="" class="col-sm-2 control-label">Total prize pool</label>
<div class="col-sm-10">
<input type="text" class="form-control" id="" name="total_prize">
</div>
</div>
<div class="form-group" style="padding-top:41px;">
<label for="" class="col-sm-2 control-label">Number of prize-winning places</label>
<div class="col-sm-1">
Top
</div>
<div class="col-sm-2">
<input type="text" class="form-control" id="" name="number_of_winning_places">
<p class="text-danger">
#if($errors->has('number_of_winning_places'))
{{ $errors->first('number_of_winning_places') }}
#endif
</p>
</div>
<div class="col-sm-3">
competitors will win a prize
</div>
</div>
</div>
</div>
<div class="panel panel-default">
<div class="panel-heading">Timeline </div>
<div class="panel-body">
<div class="form-group">
<label for="" class="col-sm-2 control-label"> Start and end date </label>
<div class="col-sm-3">
<input type="text" class="form-control" id="competition_start_date" name="competition_start_date">
<p class="text-danger">
#if($errors->has('competition_start_date'))
{{ $errors->first('competition_start_date') }}
#endif
</p>
<input type="hidden" name="hidden_start_date" id="hidden_start_date">
</div>
<div class="col-sm-2">
to
</div>
<div class="col-sm-3">
<input type="text" class="form-control" id="competition_end_date" name="competition_end_date">
<p class="text-danger">
#if($errors->has('competition_end_date'))
{{ $errors->first('competition_end_date') }}
#endif
</p>
<input type="hidden" name="hidden_end_date" id="hidden_end_date">
</div>
</div>
</div>
</div>
<div class="panel panel-default">
<div class="panel-heading">Competition details </div>
<div class="panel-body">
<div class="form-group">
<label for="" class="col-sm-2 control-label"> Competition details </label>
<div class="col-sm-10">
<textarea class="form-control" name="competition_details" id="competition_details" rows="10"> </textarea>
<p class="text-danger">
#if($errors->has('competition_details'))
{{ $errors->first('competition_details') }}
#endif
</p>
</div>
</div>
</div>
</div>
<div class="panel panel-default">
<div class="panel-heading">Competition Status </div>
<div class="panel-body">
<div class="form-group">
<label for="" class="col-sm-2 control-label"> Set competition status </label>
<div class="col-sm-10">
<select class="form-control" name="competition_status" id="competition_status">
<option value="0">Coming Soon</option>
<option value="1">Live </option>
</select>
</div>
</div>
</div>
</div>
<div class="panel panel-default">
<div class="panel-heading">Competition data </div>
<div class="panel-body">
<div class="form-group">
<label for="" class="col-sm-2 control-label"> Upload the data for the competition </label>
<div class="col-sm-10">
<input type="file" id="competition_data_1" name="competition_data_1">
<p class="help-block"> Data file/folder 1</p>
</div>
</div>
</div>
</div>
{{ Form::token() }}
<div class="panel panel-default">
<div class="panel-heading">Upload Competition </div>
<div class="panel-body">
<div class="form-group">
<div class="col-sm-10">
<input type="submit" class="btn btn-primary btn-lg btn-block" id="submit_competition" value="Submit">
</div>
</div>
</div>
</div>
</div>
</form>
<hr>
/* the controller method */
public function postSubmitCompetition()
{
$validator = Validator::make(Input::all(), array(
'organization' => 'required',
'competition_name' => 'required',
'prize_winning_places' => 'required',
'competition_start_date' => 'required',
'competition_end_date' => 'required',
'competition_details' => 'required',
'status' => 'required'
));
if($validator->fails())
{
return Redirect::route('getSubmitCompetition')->withErrors($validator)->withInput();
}
else
{
$competition = new Competition();
$competition->hosting_organization_id = Input::get('organization');
$competition->competition_name = Input::get('competition_name');
$competition->total_prize_pool = Input::get('total_prize');
$competition->prize_winning_places = Input::get('number_of_winning_places');
$competition->start_date = Input::get('competition_start_date');
$competition->end_date = Input::get('competition_end_date');
$competition->competition_details = Input::get('competition_details');
$competition->status = Input::get('competition_status');
if($competition->save())
{
return Redirect::route('getSubmitCompetition')->with('success', 'You have successfully created this competition');
}
else
{
return Redirect::route('getSubmitCompetition')->with('fail', 'An error occurred while creating that competition. Please contact sys admin');
}
}
}
/* the route */
Route::post('/admin/submit-a-competition', array('uses' => 'CompetitionController#postSubmitCompetition', 'as' => 'postSubmitCompetition'));
Problem
Laravel never saves your data because the validator fails.
$validator = Validator::make(Input::all(), array(
'organization' => 'required',
'competition_name' => 'required',
'prize_winning_places' => 'required',
'competition_start_date' => 'required',
'competition_end_date' => 'required',
'competition_details' => 'required',
'status' => 'required'
));
You require prize_winning_places and status but there are no input fields for these two in the view.
The missing input fields:
Field name: status - Problem: You made a select with the name: competition_status instead of: status
<select class="form-control" name="competition_status" id="competition_status">
<option value="0">Coming Soon</option>
<option value="1">Live </option>
</select>
Field name: prize_winning_places - Problem: You made an input field with the name: ** number_of_winning_places **instead of prize_winning_places
<input type="text" class="form-control" id="" name="number_of_winning_places">
Solution
Change the $validator variable to:
$validator = Validator::make(Input::all(), array(
'organization' => 'required',
'competition_name' => 'required',
'number_of_winning_places' => 'required',
'competition_start_date' => 'required',
'competition_end_date' => 'required',
'competition_details' => 'required',
'competition_status' => 'required'
));
Check this line:
<form class="" method="post" action=" {{URL::route('postSubmitCompetition')}}">
here form action is the route name not the function name. Like:
Route::post('/save_data', array('uses' => 'CompetitionController#postSubmitCompetition', 'as' => 'postSubmitCompetition'));
It will route your route to the function.