How to display other data when you select the supplier name in select tag in laravel - laravel

I'd like to select a name and output it properties in form automatically, how do I display its properties automatically?
<select id="suppliers_name" name="suppliers_name" class="form-control" data-plugin="select2">
#foreach ($supplier as $suppliers)
<option value="{{$suppliers->name}}">{{$suppliers->supplier_name}}</option>
#endforeach
</select>
</div>
</div>
<div class="form-group row">
<label for="Supplier/s Address" class="col-sm-2 col-form-label">Suppler/s Address:</label>
<div class="col-sm-10">
<input type="text" class="form-control" id="sup_address" name="sup_address" placeholder="Supplier's Address" value="{{$suppliers->supplier_address}}" >
</div>
</div>
<div class="form-group row">
<label for="Contact Person" class="col-sm-2 col-form-label">Contact Person:</label>
<div class="col-sm-10">
<input type="text" class="form-control" id="con_person" name="con_person" placeholder="Contact Person Name" value="{{$suppliers->contact_person_name}}" disabled>
</div>
</div>
I can select the suppliers name but I can't display its properties.
here's the controller:
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Po;
use App\Supplier;
class PoController extends Controller
{
/**
* Display a listing of the resource.
*
* #return \Illuminate\Http\Response
*/
public function index()
{
$po = Po::All()->name();
return view('/po.manage-po', compact('po'));
}
/**
* Show the form for creating a new resource.
*
* #return \Illuminate\Http\Response
*/
public function create()
{
$supplier = Supplier::All();
return view('/po.add-po', compact('supplier'));
}

Related

Laravel 8 Form Request Validation Redirect to Index page instead same page and show error

On localhost all is good, but when I deploy the application to the server not working. If form request validation fails instead of bringing me back to the same page and showing an error, it redirects me to the index page.
config.blade.php
<form method="POST" action="{{ route('config.update', $config->id) }}">
#csrf
#method('PUT')
<div class="form-group row">
<div class="col">
<label class="col-form-label">Name</label>
<input id="name" type="text" class="form-control" name="name" value="{{ $config->name }}" required>
</div>
</div>
<div class="form-group row mt-3">
<div class="col">
<label class="col-form-label text-md-right">Address</label>
<input id="address" type="text" class="form-control" name="address" value="{{ $config->address }}">
</div>
</div>
<div class="form-group row mt-3">
<div class="col">
<label class="col-form-label text-md-right">Phone</label>
<input id="phone" type="tel" class="form-control" name="phone" value="{{ $config->phone }}" required>
</div>
</div>
<div class="form-group row mt-3">
<div class="col">
<label class="col-form-label text-md-right">E-mail</label>
<input id="email" type="email" class="form-control" name="email" value="{{ $config->email }}" required>
</div>
</div>
<div class="form-group row mt-4 mb-0">
<div class="col-md-12">
<button type="submit" class="btn btn-primary button-full-width">Save changes</button>
</div>
</div>
</form>
web.php
Route::resource('/admin/config', 'Admin\ConfigController');
ConfigController
<?php
namespace App\Http\Controllers\Admin;
use App\Http\Controllers\Controller;
use App\Services\ConfigServices;
use App\Http\Requests\ConfigRequest;
use App\Models\Config;
class ConfigController extends Controller
{
protected $configServices;
public function __construct(ConfigServices $configServices) {
$this->middleware('auth');
$this->configServices = $configServices;
}
...
public function update(ConfigRequest $request, $id)
{
$config = $this->configServices->updateConfigById($request, $id);
return redirect()->back();
}
...
}
ConfigRequest - here is the problem
<?php
namespace App\Http\Requests;
use Illuminate\Foundation\Http\FormRequest;
class ConfigRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*
* #return bool
*/
public function authorize()
{
return true;
}
/**
* Get the validation rules that apply to the request.
*
* #return array
*/
public function rules()
{
return [
'name' => 'required|string|max:255',
'address' => 'nullable|string|max:255',
'phone' => 'required|regex:/^([0-9\s\-\+\(\)]*)$/|min:9|max:15',
'email' => 'required|email:rfc',
];
}
}
Form Request return to index page instead same page. On localhost working everything, but when I deploy the app to server a problem arises.
When data on form request validated correct return me back on the same page and show success, but when form request failing redirect mine for some reason to the index page.
A problem arises in Laravel 8, this code worked well in previous Laravel versions.
Can someone help me, please?
In your custom request you need:
/**
* The URI that users should be redirected to if validation fails.
*
* #var string
*/
protected $redirect = '/dashboard';
or
/**
* The route that users should be redirected to if validation fails.
*
* #var string
*/
protected $redirectRoute = 'dashboard';
You can find more in the docs.
In the docs for older versions of Laravel these properties don't exist.
Do you have error parts in your blade?
#if (count($errors) > 0)
<div class="alert alert-danger">
<ul>
#foreach ($errors->all() as $error)
<li>{{ $error }}</li>
#if ($message = Session::get('unique'))
asdsad
#endif
#endforeach
</ul>
</div>
#endif

How do I send email to multiple users from a single email form? laravel 8

In my application, I can send an email to a single user!
but I want to send to multiple users at the same time from a single form!
My class
class SendEvent extends Mailable {
use Queueable, SerializesModels;
public $data;
/**
* Create a new message instance.
*
* #return void
*/
public function __construct($data) {
$this->data = $data;
}
/**
* Build the message.
*
* #return $this
*/
public function build() {
return $this->markdown('emails.eventEmail');
}
}
This is the send() function below.
My controller
class EmailController extends Controller {
public function send(Request $request) {
$homeUrl = url('/');
$eventId = $request->get('event_id');
$eventTitle = $request->get('event_title');
$eventUrl = $homeUrl.'/'.'events/'.$eventId.'/'.$eventTitle;
$data = array(
'your_name'=>$request->get('your_name'),
'your_email'=>$request->get('your_email'),
'friend_name'=>$request->get('friend_name'),
'eventUrl'=>$eventUrl
);
$emailTo = $request->get('friend_email');
Mail::to($emailTo)->send(new SendEvent($data));
return redirect()->back()->with('message','Event link sent to '.$emailTo);
}
}
Here in the recipient address form, I can only add just one address!
My form in the model
<form action="{{route('mail')}}" method="POST">#csrf
<div class="modal-body">
<input type="hidden" name="event_id" value="{{$event->id}}">
<input type="hidden" name="event_title" value="{{$event->title}}">
<div class="form-goup">
<label>Your name * </label>
<input type="text" name="your_name" class="form-control" required="">
</div>
<div class="form-goup">
<label>Your email *</label>
<input type="email" name="your_email" class="form-control" required="">
</div>
<div class="form-goup">
<label>Person name *</label>
<input type="text" name="friend_name" class="form-control" required="">
</div>
<div class="form-goup">
<label>Person email *</label>
<input type="email" name="friend_email" class="form-control" required="">
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
<button type="submit" class="btn btn-primary">Mail this event</button>
</div>
</form>
How do I get my code to send to more than one email address from my form?
I think you can solve it by making $friend_name an array
<input type="email" name="friend_email[]" class="form-control" required="">
and then you may add a js function to append this input for more emails
something like that
$( "#form" ).append( "<input type="email" name="friend_email[]" class="form-control"
required="">
" );
and that's it.
The friend_email will be passed to the Mail as an array and every email in it will receive the mail

Trying to get property 'id' of non-object (View: E:\xampp\htdocs\mini_blog\resources\views\admin\posts\edit.blade.php)

Trying to get property 'id' of non-object
how to fix the bug, please explain to me, someone
edit.blade.php
#extends('layouts.app')
#section('content')
<div class="card">
<div class="card-header text-center">Edit Post : {{$posts->title}}</div>
<div class="card-body">
#if(count($errors)>0)
<ul class="list-group alert">
#foreach($errors->all() as $error)
<li class="list-group-item text-danger">
{{$error}}
</li>
#endforeach
</ul>
#endif
<form action="{{route('post.update',['id'=>$posts->id])}}" method="post" enctype="multipart/form-data">
{{csrf_field()}}
<div class="form-group">
<label for="title">Post Title</label>
<input type="text" name="title" placeholder="Enter" class="form-control" value="{{$posts->title}} ">
</div>
<div class="form-group">
<label for="image">Featured Image</label>
<input type="file" name="image" class="form-control">
</div>
<div class="form-group">
<label for="category">Select a Category</label>
<select name="category_id" id="category" class="form-control">
#foreach($categories as $cat)
<option value="{{$cat->id}}"
#if($posts->cat->id== $cat->id)
selected
#endif
>{{$cat->name}}</option>
#endforeach
</select>
</div>
<div class="form-group">
<label for="tag">Select Tags</label>
#foreach($tag as $tags)
<div class="checkbox">
<label><input type="checkbox" name="tags[]" value="{{$tags->id}}"
#foreach($posts->tags as $t)
#if($tags->id==$t->id)
checked
#endif
#endforeach
>{{$tags->tag}}</label>
</div>
#endforeach
</div>
<div class="form-group">
<label for="content">Description</label>
<textarea name="content" id="content" cols="5" rows="5" class="form-control"> {{$posts->content}}</textarea>
</div>
<div class="form-group">
<input type="submit" name="submit" value="Submit" class="btn btn-primary">
</div>
</form>
</div>
</div>
#endsection
PostController.php
#extends('layouts.app')
#section('content')
<div class="card">
<div class="card-header text-center">Edit Post : {{$posts->title}}</div>
<div class="card-body">
#if(count($errors)>0)
<ul class="list-group alert">
#foreach($errors->all() as $error)
<li class="list-group-item text-danger">
{{$error}}
</li>
#endforeach
</ul>
#endif
<form action="{{route('post.update',['id'=>$posts->id])}}" method="post" enctype="multipart/form-data">
{{csrf_field()}}
<div class="form-group">
<label for="title">Post Title</label>
<input type="text" name="title" placeholder="Enter" class="form-control" value="{{$posts->title}} ">
</div>
<div class="form-group">
<label for="image">Featured Image</label>
<input type="file" name="image" class="form-control">
</div>
<div class="form-group">
<label for="category">Select a Category</label>
<select name="category_id" id="category" class="form-control">
#foreach($categories as $cat)
<option value="{{$cat->id}}"
#if($posts->cat->id== $cat->id)
selected
#endif
>{{$cat->name}}</option>
#endforeach
</select>
</div>
<div class="form-group">
<label for="tag">Select Tags</label>
#foreach($tag as $tags)
<div class="checkbox">
<label><input type="checkbox" name="tags[]" value="{{$tags->id}}"
#foreach($posts->tags as $t)
#if($tags->id==$t->id)
checked
#endif
#endforeach
>{{$tags->tag}}</label>
</div>
#endforeach
</div>
<div class="form-group">
<label for="content">Description</label>
<textarea name="content" id="content" cols="5" rows="5" class="form-control"> {{$posts->content}}</textarea>
</div>
<div class="form-group">
<input type="submit" name="submit" value="Submit" class="btn btn-primary">
</div>
</form>
</div>
</div>
#endsection
Trying to get property 'id' of non-object when I select category its not selected and show id is non-object
PostController.php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Category;
use App\Post;
use App\Tag;
use Session;
class PostController extends Controller
{
/**
* Display a listing of the resource.
*
* #return \Illuminate\Http\Response
*/
public function index()
{
return view('admin.posts.index')->with('posts',Post::all());
}
/**
* Show the form for creating a new resource.
*
* #return \Illuminate\Http\Response
*/
public function create()
{
$categories=Category::all();
if($categories->count()==0){
Session::flash('info','You must have some categories before attempt post.');
return redirect()->back();
}
return view('admin.posts.create')->with('category',$categories)->with('tags',Tag::all());
}
/**
* Store a newly created resource in storage.
*
* #param \Illuminate\Http\Request $request
* #return \Illuminate\Http\Response
*/
public function store(Request $request)
{
$this->validate($request,[
'title'=>'required|max:255',
'image'=>'required|image',
'content'=>'required',
'category_id'=>'required',
'tags'=>'required'
]);
$images=$request->image;
$image_new_name=time().$images->getClientOriginalName();
$images->move('uploads/posts',$image_new_name);
$post=Post::create([
'title'=>$request->title,
'image'=>$request->image,
'content'=>$request->content,
'image'=>'uploads/posts/'.$image_new_name,
'category_id'=>$request->category_id,
'slug'=>str_slug($request->title)
]);
$post->tags()->attach($request->tags);
Session::flash('success','Post Created Successfully');
return redirect()->back();
}
/**
* Display the specified resource.
*
* #param int $id
* #return \Illuminate\Http\Response
*/
public function show($id)
{
//
}
/**
* Show the form for editing the specified resource.
*
* #param int $id
* #return \Illuminate\Http\Response
*/
public function edit($id)
{
$post=Post::find($id);
return view('admin.posts.edit')->with('posts',$post)
->with('categories',Category::all())
->with('tag',Tag::all());
}
/**
* Update the specified resource in storage.
*
* #param \Illuminate\Http\Request $request
* #param int $id
* #return \Illuminate\Http\Response
*/
public function update(Request $request, $id)
{
$this->validate($request,[
'title'=>'required',
'content'=>'required',
'category_id'=>'required',
]);
$post=Post::find($id);
if ($request->hasFile('image'))
{
$featured=$request->image;
$featured_new_name=time().$featured->getClientOriginalName();
$featured->move('uploads/posts',$featured_new_name);
$post->image='uploads/posts/'.$featured_new_name;
}
$post->title=$request->title;
$post->content=$request->content;
$post->category_id=$request->category_id;
$post->save();
$post->tags()->sync($request->tags);
Session::flash('success','Your Post Updated Successfully');
return redirect()->back();
}
/**
* Remove the specified resource from storage.
*
* #param int $id
* #return \Illuminate\Http\Response
*/
public function destroy($id)
{
$post=Post::find($id);
$post->delete();
Session::flash('success','Post was just trashed');
return redirect()->back();
}
public function trashed(){
$post=Post::onlyTrashed()->get();
return view('admin.posts.trashed')->with('posts',$post);
}
public function kill($id){
$post=Post::withTrashed()->where('id',$id)->first();
$post->forceDelete();
Session::flash('success','Post deleted permanently');
return redirect()->back();
}
public function restore($id){
$post=Post::withTrashed()->where('id',$id)->first();
$post->restore();
Session::flash('success','Post Restore ');
return redirect()->route('posts');
}
}
Make sure that categories, tags are not empty, also the Postrelationships are true.

How to insert into pivot table after form submission using attach?

I'm using Laravel eloquent and I'm trying to insert the selected user id from my form and the generated ticket id into my pivot table using attach but I don't know how to do this.
store function
public function store(Request $request
{
$ticket = new Ticket;
$ticket->organisation_name = $request['organisation_name'];
$ticket->postal_address = $request['postal_address'];
$ticket->physical_address = $request['physical_address'];
$ticket->description_brief = $request['description'];
$ticket->hours_dedicated = $request['hours'];
$ticket->commencement_date = $request['start_date'];
$ticket->due_date = $request['due_date'];
$ticket->client_id = $request['client_id'];
$ticket->save();
//trying to use attach here
return redirect('/home');
}
form
<form action="TicketsController#store" method="POST">
{{csrf_field() }}
<div class="form-group">
<label>Organisation Name:</label>
<input type="text" class="form-control" name="organisation_name" placeholder="Enter Organisation Name">
</div>
<div class="form-group">
<label>Postal address:</label>
<input type="text" class="form-control" name="postal_address" placeholder="">
</div>
<div class="form-group">
<label>Physical address:</label>
<input type="text" class="form-control" name="physical_address" placeholder="">
</div>
<div class="form-group">
<label>Client:</label>
<select class="form-control" name="client_id">
#foreach ($forms as $form)
<option>{{$form->client->client_id}}</option>
#endforeach
</select>
</div>
<div class="form-group">
<label>Description:</label>
<input type="text" class="form-control" name="description" placeholder="">
</div>
<div class="form-group">
<label>Hours:</label>
<input type="text" class="form-control" name="hours" placeholder="">
</div>
<div class="form-group">
<label>Start date:</label>
<input type="text" class="form-control" name="start_date" type="date" placeholder="">
</div>
<div class="form-group">
<label>Due date:</label>
<input type="text" class="form-control" name="due_date" type="date" placeholder="">
</div>
<div class="form-group">
<label>User:</label>
<select class="form-control" name="id">
#foreach ($users as $user)
<option>{{$user->id}}</option>
#endforeach
</select>
</div>
<input type="submit" name="submit" class="btn btn-primary">
</form>
User.php
class User extends Authenticatable
{
use Notifiable;
/**
* The attributes that are mass assignable.
*
* #var array
*/
protected $fillable = [
'name', 'email', 'password',
];
/**
* The attributes that should be hidden for arrays.
*
* #var array
*/
protected $hidden = [
'password', 'remember_token',
];
public function tickets(){
return$this->belongsToMany(Ticket::class,'ticket_user','ticket_id','id');
}
Ticket.php
class Ticket extends Model
{
public function client(){
return $this->belongsTo(Client::class,'client_id');
}
public function users(){
return $this->belongsToMany(User::class,'ticket_user','id','ticket_id');
}
protected $primaryKey = 'ticket_id';
public $timestamps = false;
}
As per the documentation you can simply pass the id you want to attach. After $ticket->save(); you can add the following:
$ticket->users()->attach($request->input('id'));
attach() will also work if you pass an array of ids, a model, a collection of ids or a collection of models.

Failed to save value select option

what's wrong in my project.
I want to save with select option, but the filed doesn't save data select option. it just save input text.
the project is taken different table, and the form just get the project_name and project_id. project_id will save in in table spent_times.
and the select option will save task_category in table spent_times
this my model
protected $table = 'spent_times';
protected $fillable = [
'task_category',
'story/meeting_name',
'assign',
'estimated_time',
'user_story',
'spent_time',
'percentage',
'lateness',
'index',
'project_id'
];
public function users() {
return $this->hasMany(User::class);
}
public function project() {
return $this->belongsTo(Project::class);
}
my create.blade.php
<form action="{{route('store')}}" method="POST">
#csrf
<div class="box-body">
<div class="form-group">
<label for="">Project *</label>
<select class="form-control select2" style="width: 100%;">
<option>Select One</option>
#foreach($projects as $id => $project)
<option value="{{$id}}">{{$project}}</option>
#endforeach
</select>
</div>
<div class="form-group">
<label for="">Story # Meeting Name *</label>
<input type="text" class="form-control" name="user_story">
</div>
<div class="form-group">
<label for="">Category *</label>
<select class="form-control select2" style="width: 100%;">
<option>Select One</option>
#foreach($task_categories as $category)
<option value="{{$category}}">{{$category}}</option>
#endforeach
</select>
</div>
<div class="form-group">
<label for="">Estimated *</label>
<input type="text" class="form-control" name="estimated_time">
</div>
</div>
<div class="box-footer">
<a href="{{route('index')}}">
<button type="submit" class="btn btn-primary col-md-12" style="border-radius : 0px;">SAVE</button>
</a>
</div>
</form>
my controller
public function create()
{
$spentimes = new SpentTime;
$project = new Project;
$projects = Project::select('project_name', 'id')->get();
return view('Ongoings.index', compact ('projects', 'task_categories', 'spentimes', 'project'));
}
/**
* Store a newly created resource in storage.
*
* #param \Illuminate\Http\Request $request
* #return \Illuminate\Http\Response
*/
public function store(Request $request)
{
dd($request->all());
$spentime = SpentTime::create([
'project_name' => request('project_name'),
'user_stroy' => request ('user_story'),
'task_category' => request('task_category'),
'estimated_time' => request('estimated_time')
]);
$spentime->save();
return redirect()->route('index');
}
error like this
error in web browser
please help me
You are expecting those fields from request in controller.
project_id
meeting_name
task_category
estimated_time
But You are just sending estimated_time,
other 3 fields are not present in you create.blade.php
In create.blade.php, category select input don't have any name.
that's why task_category is getting null from request('task_category'). But task_category field is not nullable in your database table.
Send form input properly as you are expecting in your controller. You can use dd($request->all()) to check.
I believe this is your task_category select box.
so this select box doesn't have a name attribute. that's the error.
Possible solution.
<div class="form-group">
<label for="">Category *</label>
<select name="task_category" class="form-control select2" style="width: 100%;">
<option>Select One</option>
#foreach($task_categories as $category)
<option value="{{$category}}">{{$category}}</option>
#endforeach
</select>
</div>

Resources