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

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.

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

Why is the content of the tag and category button not visible when creating a post in laravel?

I try to create blog web page in laravel. While creating the post, the contents of the tag and category button are not visible.
My post.blade.php;
#extends('admin.layouts.app')
#section('headSection')
<link rel="stylesheet" href="{{ asset('admin/plugins/select2/select2.min.css') }}">
#endsection
#section('main-content')
<!-- Content Wrapper. Contains page content -->
<div class="content-wrapper">
<!-- Content Header (Page header) -->
<section class="content-header">
<h1>
Text Editors
<small>Advanced form element</small>
</h1>
<ol class="breadcrumb">
<li><i class="fa fa-dashboard"></i> Home</li>
<li>Forms</li>
<li class="active">Editors</li>
</ol>
</section>
<!-- Main content -->
<section class="content">
<div class="row">
<div class="col-md-12">
<!-- general form elements -->
<div class="box box-primary">
<div class="box-header with-border">
<h3 class="box-title">Titles</h3>
</div>
#include('includes.messages')
<!-- /.box-header -->
<!-- form start -->
<form role="form" action="{{ route('post.store') }}" method="post" enctype="multipart/form-data">
{{ csrf_field() }}
<div class="box-body">
<div class="col-lg-6">
<div class="form-group">
<label for="title">Post Title</label>
<input type="text" class="form-control" id="title" name="title" placeholder="Title">
</div>
<div class="form-group">
<label for="subtitle">Post Sub Title</label>
<input type="text" class="form-control" id="subtitle" name="subtitle" placeholder="Sub Title">
</div>
<div class="form-group">
<label for="slug">Post Slug</label>
<input type="text" class="form-control" id="slug" name="slug" placeholder="Slug">
</div>
</div>
<div class="col-lg-6">
<br>
<div class="form-group">
<div class="pull-right">
<label for="image">File input</label>
<input type="file" name="image" id="image">
</div>
<div class="checkbox pull-left">
<label>
<input type="checkbox" name="status" value="1"> Publish
</label>
</div>
</div>
<br>
<div class="form-group" style="margin-top:18px;">
<label>Select Tags</label>
<select class="form-control select2 select2-hidden-accessible" multiple="" data-placeholder="Select a State" style="width: 100%;" tabindex="-1" aria-hidden="true" name="tags[]">
#foreach ($tags as $tag)
<option value="{{ $tag->id }}">{{ $tag->name }}</option>
#endforeach
</select>
</div>
<div class="form-group" style="margin-top:18px;">
<label>Select Category</label>
<select class="form-control select2 select2-hidden-accessible" multiple="" data-placeholder="Select a State" style="width: 100%;" tabindex="-1" aria-hidden="true" name="categories[]">
#foreach ($categories as $category)
<option value="{{ $category->id }}">{{ $category->name }}</option>
#endforeach
</select>
</div>
</div>
</div>
<!-- /.box-body -->
<div class="box">
<div class="box-header">
<h3 class="box-title">Write Post Body Here
<small>Simple and fast</small>
</h3>
<!-- tools box -->
<div class="pull-right box-tools">
<button type="button" class="btn btn-default btn-sm" data-widget="collapse" data-toggle="tooltip" title="Collapse">
<i class="fa fa-minus"></i></button>
</div>
<!-- /. tools -->
</div>
<!-- /.box-header -->
<div class="box-body pad">
<textarea name="body" style="width: 100%; height: 500px; font-size: 14px; line-height: 18px; border: 1px solid #dddddd; padding: 10px;" id="editor1"></textarea>
</div>
</div>
<div class="box-footer">
<input type="submit" class="btn btn-primary">
Back
</div>
</form>
</div>
<!-- /.box -->
</div>
<!-- /.col-->
</div>
<!-- ./row -->
</section>
<!-- /.content -->
</div>
<!-- /.content-wrapper -->
#endsection
#section('footerSection')
<script src="{{ asset('admin/plugins/select2/select2.full.min.js') }}"></script>
<script src="{{ asset('admin/ckeditor/ckeditor.js') }}"></script>
<script>
$(function () {
// Replace the <textarea id="editor1"> with a CKEditor
// instance, using default configuration.
CKEDITOR.replace('editor1');
});
</script>
<script>
$(document).ready(function() {
$(".select2").select2();
});
</script>
#endsection
I am stuck because everytime I submit a post with tags and category.
My edit blade.php file;
#extends('admin.layouts.app')
#section('headSection')
<link rel="stylesheet" href="{{ asset('admin/plugins/select2/select2.min.css') }}">
#endsection
#section('main-content')
<!-- Content Wrapper. Contains page content -->
<div class="content-wrapper">
<!-- Content Header (Page header) -->
<section class="content-header">
<h1>
Text Editors
<small>Advanced form element</small>
</h1>
<ol class="breadcrumb">
<li><i class="fa fa-dashboard"></i> Home</li>
<li>Forms</li>
<li class="active">Editors</li>
</ol>
</section>
<!-- Main content -->
<section class="content">
<div class="row">
<div class="col-md-12">
<!-- general form elements -->
<div class="box box-primary">
<div class="box-header with-border">
<h3 class="box-title">Titles</h3>
</div>
#include('includes.messages')
<!-- /.box-header -->
<!-- form start -->
<form role="form" action="{{ route('post.update',$post->id) }}" method="post" enctype="multipart/form-data">
{{ csrf_field() }}
{{ method_field('PATCH') }}
<div class="box-body">
<div class="col-lg-6">
<div class="form-group">
<label for="title">Post Title</label>
<input type="text" class="form-control" id="title" name="title" placeholder="Title" value="{{ $post->title }}">
</div>
<div class="form-group">
<label for="subtitle">Post Sub Title</label>
<input type="text" class="form-control" id="subtitle" name="subtitle" placeholder="Sub Title" value="{{ $post->subtitle }}">
</div>
<div class="form-group">
<label for="slug">Post Slug</label>
<input type="text" class="form-control" id="slug" name="slug" placeholder="Slug" value="{{ $post->slug }}">
</div>
</div>
<div class="col-lg-6">
<br>
<div class="form-group">
<div class="pull-right">
<label for="image">File input</label>
<input type="file" name="image" id="image">
</div>
<div class="checkbox pull-left">
<label>
<input type="checkbox" name="status" value="1" #if ($post->status == 1)
{{'checked'}}
#endif> Publish
</label>
</div>
</div>
<br>
<div class="form-group" style="margin-top:18px;">
<label>Select Tags</label>
<select class="form-control select2 select2-hidden-accessible" multiple="" data-placeholder="Select a State" style="width: 100%;" tabindex="-1" aria-hidden="true" name="tags[]">
#foreach ($tags as $tag)
<option value="{{ $tag->id }}"
#foreach ($post->tags as $postTag)
#if ($postTag->id == $tag->id)
selected
#endif
#endforeach
>{{ $tag->name }}</option>
#endforeach
</select>
</div>
<div class="form-group" style="margin-top:18px;">
<label>Select Category</label>
<select class="form-control select2 select2-hidden-accessible" multiple="" data-placeholder="Select a State" style="width: 100%;" tabindex="-1" aria-hidden="true" name="categories[]">
#foreach ($categories as $category)
<option value="{{ $category->id }}"
#foreach ($post->categories as $postCategory)
#if ($postCategory->id == $category->id)
selected
#endif
#endforeach
>{{ $category->name }}</option>
#endforeach
</select>
</div>
</div>
</div>
<!-- /.box-body -->
<div class="box">
<div class="box-header">
<h3 class="box-title">Write Post Body Here
<small>Simple and fast</small>
</h3>
<!-- tools box -->
<div class="pull-right box-tools">
<button type="button" class="btn btn-default btn-sm" data-widget="collapse" data-toggle="tooltip" title="Collapse">
<i class="fa fa-minus"></i></button>
</div>
<!-- /. tools -->
</div>
<!-- /.box-header -->
<div class="box-body pad">
<textarea name="body" style="width: 100%; height: 500px; font-size: 14px; line-height: 18px; border: 1px solid #dddddd; padding: 10px;" id="editor1">{{ $post->body }}</textarea>
</div>
</div>
<div class="box-footer">
<input type="submit" class="btn btn-primary">
Back
</div>
</form>
</div>
<!-- /.box -->
</div>
<!-- /.col-->
</div>
<!-- ./row -->
</section>
<!-- /.content -->
</div>
<!-- /.content-wrapper -->
#endsection
#section('footerSection')
<script src="{{ asset('admin/plugins/select2/select2.full.min.js') }}"></script>
<script src="{{ asset('admin/ckeditor/ckeditor.js') }}"></script>
<script>
$(function () {
// Replace the <textarea id="editor1"> with a CKEditor
// instance, using default configuration.
CKEDITOR.replace('editor1');
//bootstrap WYSIHTML5 - text editor
$(".textarea").wysihtml5();
});
</script>
<script>
$(document).ready(function() {
$(".select2").select2();
});
</script>
#endsection
My show blade php file;
#extends('admin.layouts.app')
#section('headSection')
<link rel="stylesheet" href="{{ asset('admin/plugins/datatables/dataTables.bootstrap.css') }}">
#endsection
#section('main-content')
<!-- Content Wrapper. Contains page content -->
<div class="content-wrapper">
<!-- Content Header (Page header) -->
<section class="content-header">
<h1>
Blank page
<small>it all starts here</small>
</h1>
<ol class="breadcrumb">
<li><i class="fa fa-dashboard"></i> Home</li>
<li>Examples</li>
<li class="active">Blank page</li>
</ol>
</section>
<!-- Main content -->
<section class="content">
<!-- Default box -->
<div class="box">
<div class="box-header with-border">
<h3 class="box-title">Title</h3>
<a class='col-lg-offset-5 btn btn-success' href="{{ route('post.create') }}">Add New</a>
<div class="box-tools pull-right">
<button type="button" class="btn btn-box-tool" data-widget="collapse" data-toggle="tooltip" title="Collapse">
<i class="fa fa-minus"></i></button>
<button type="button" class="btn btn-box-tool" data-widget="remove" data-toggle="tooltip" title="Remove">
<i class="fa fa-times"></i></button>
</div>
</div>
<div class="box-body">
<div class="box">
<div class="box-header">
<h3 class="box-title">Data Table With Full Features</h3>
</div>
<!-- /.box-header -->
<div class="box-body">
<table id="example1" class="table table-bordered table-striped">
<thead>
<tr>
<th>S.No</th>
<th>Title</th>
<th>Sub Title</th>
<th>Slug</th>
<th>Creatd At</th>
<th>Edit</th>
<th>Delete</th>
</tr>
</thead>
<tbody>
#foreach ($posts as $post)
<tr>
<td>{{ $loop->index + 1 }}</td>
<td>{{ $post->title }}</td>
<td>{{ $post->subtitle }}</td>
<td>{{ $post->slug }}</td>
<td>{{ $post->created_at }}</td>
<td><span class="glyphicon glyphicon-edit"></span></td>
<td>
<form id="delete-form-{{ $post->id }}" method="post" action="{{ route('post.destroy',$post->id) }}" style="display: none">
{{ csrf_field() }}
{{ method_field('DELETE') }}
</form>
<a href="" onclick="
if(confirm('Are you sure, You Want to delete this?'))
{
event.preventDefault();
document.getElementById('delete-form-{{ $post->id }}').submit();
}
else{
event.preventDefault();
}" ><span class="glyphicon glyphicon-trash"></span></a>
</td>
</tr>
#endforeach
</tbody>
<tfoot>
<tr>
<th>S.No</th>
<th>Title</th>
<th>Sub Title</th>
<th>Slug</th>
<th>Creatd At</th>
<th>Edit</th>
<th>Delete</th>
</tr>
</tfoot>
</table>
</div>
<!-- /.box-body -->
</div>
</div>
<!-- /.box-body -->
<div class="box-footer">
Footer
</div>
<!-- /.box-footer-->
</div>
<!-- /.box -->
</section>
<!-- /.content -->
</div>
<!-- /.content-wrapper -->
#endsection
#section('footerSection')
<script src="{{ asset('admin/plugins/datatables/jquery.dataTables.min.js') }}"></script>
<script src="{{ asset('admin/plugins/datatables/dataTables.bootstrap.min.js') }}"></script>
<script>
$(function () {
$("#example1").DataTable();
});
</script>
#endsection
My PostController.php
<?php
namespace App\Http\Controllers\Admin;
use App\Http\Controllers\Controller;
use App\Model\user\category;
use App\Model\user\post;
use App\Model\user\tag;
use Illuminate\Http\Request;
class PostController extends Controller
{
/**
* Display a listing of the resource.
*
* #return \Illuminate\Http\Response
*/
public function index()
{
$posts = post::all();
return view('admin.post.show',compact('posts'));
}
/**
* Show the form for creating a new resource.
*
* #return \Illuminate\Http\Response
*/
public function create()
{
$tags =tag::all();
$categories =category::all();
return view('admin.post.post',compact('tags','categories'));
}
/**
* Store a newly created resource in storage.
*
* #param \Illuminate\Http\Request $request
* #return \Illuminate\Http\Response
*/
public function store(Request $request)
{
return $request->all();
$this->validate($request,[
'title'=>'required',
'subtitle'=>'required',
'slug'=>'required',
'body'=>'required',
]);
$post = new post;
$post->title = $request->title;
$post->subtitle = $request->subtitle;
$post->slug = $request->slug;
$post->body = $request->body;
$post->status = $request->status;
$post->save();
$post->tags()->sync($request->tags);
$post->categories()->sync($request->categories);
return redirect(route('post.index'));
}
/**
* 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::where('id',$id)->first();
$tags =tag::all();
$categories =category::all();
return view('admin.post.edit',compact('tags','categories','post'));
}
/**
* 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',
'subtitle'=>'required',
'slug'=>'required',
'body'=>'required',
]);
$post = post::find($id);
$post->title = $request->title;
$post->subtitle = $request->subtitle;
$post->slug = $request->slug;
$post->body = $request->body;
$post->status = $request->status;
$post->tags()->sync($request->tags);
$post->categories()->sync($request->categories);
$post->save();
return redirect(route('post.index'));
}
/**
* Remove the specified resource from storage.
*
* #param int $id
* #return \Illuminate\Http\Response
*/
public function destroy($id)
{
post::where('id',$id)->delete();
return redirect()->back();
}
}
My tagcontroller.php
<?php
namespace App\Http\Controllers\Admin;
use App\Http\Controllers\Controller;
use App\Model\user\tag;
use Illuminate\Http\Request;
class TagController extends Controller
{
/**
* Display a listing of the resource.
*
* #return \Illuminate\Http\Response
*/
public function index()
{
$tags = tag::all();
return view('admin.tag.show',compact('tags'));
}
/**
* Show the form for creating a new resource.
*
* #return \Illuminate\Http\Response
*/
public function create()
{
return view('admin.tag.tag');
}
/**
* Store a newly created resource in storage.
*
* #param \Illuminate\Http\Request $request
* #return \Illuminate\Http\Response
*/
public function store(Request $request)
{
$this->validate($request,[
'name' => 'required',
'slug' => 'required',
]);
$tag = new tag;
$tag->name = $request->name;
$tag->slug = $request->slug;
$tag->save();
return redirect(route('tag.index'));
}
/**
* 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)
{
$tag= tag::where('id',$id)->first();
return view('admin.tag.edit',compact('tag'));
}
/**
* 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,[
'name' => 'required',
'slug' => 'required',
]);
$tag = tag::find($id);
$tag->name = $request->name;
$tag->slug = $request->slug;
$tag->save();
return redirect(route('tag.index'));
}
/**
* Remove the specified resource from storage.
*
* #param int $id
* #return \Illuminate\Http\Response
*/
public function destroy($id)
{
tag::where('id',$id)->delete();
return redirect()->back();
}
}
My categorycontroller.php;
<?php
namespace App\Http\Controllers\Admin;
use App\Http\Controllers\Controller;
use App\Model\user\category;
use Illuminate\Http\Request;
class CategoryController extends Controller
{
/**
* Display a listing of the resource.
*
* #return \Illuminate\Http\Response
*/
public function index()
{
$categories = category::all();
return view('admin.category.show',compact('categories'));
}
/**
* Show the form for creating a new resource.
*
* #return \Illuminate\Http\Response
*/
public function create()
{
return view('admin.category.category');
}
/**
* Store a newly created resource in storage.
*
* #param \Illuminate\Http\Request $request
* #return \Illuminate\Http\Response
*/
public function store(Request $request)
{
$this->validate($request,[
'name' => 'required',
'slug' => 'required',
]);
$category = new category;
$category->name = $request->name;
$category->slug = $request->slug;
$category->save();
return redirect(route('category.index'));
}
/**
* 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)
{
$category= category::where('id',$id)->first();
return view('admin.category.edit',compact('category'));
}
/**
* 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,[
'name' => 'required',
'slug' => 'required',
]);
$category = category::find($id);
$category->name = $request->name;
$category->slug = $request->slug;
$category->save();
return redirect(route('category.index'));
}
/**
* Remove the specified resource from storage.
*
* #param int $id
* #return \Illuminate\Http\Response
*/
public function destroy($id)
{
category::where('id',$id)->delete();
return redirect()->back();
}
}
How can I resolve this issue? And furthermore how can i do it?

How to display other data when you select the supplier name in select tag in 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'));
}

New “Metier” is created when editing a “Metier”

When I try to edit a "Metier", a new "Metier" is created and the old one stays the same. I want to crush the old "Metier" and create a new one just by editing. Here is my code in relation with the edit function.
Controller
public function edit($id)
{
$metier=Metier::find($id);
return view('metier.edit',['libelle_metier'=>$metier]);
}
/**
* Update the specified resource in storage.
*
* #param \Illuminate\Http\Request $request
* #param int $id
* #return \Illuminate\Http\Response
*/
public function update(Request $request, $id)
{
$metier=Metier::find($id);
return view('metier.edit',['libelle_metier'=>$metier]);
}
View
<div class="form-group">
<label for="">libelle Metier </label>
<input type="text" name ="libelle_metier" class="form-control"value ="
{{$libelle_metier->libelle_metier}}" >
</div>
<div class="form-group">
<input type="submit" value = "enregistrer" class="form-control btn btn-
primary">
</div>
route
Route::get('/metier', 'MetierController#index');
Route::get('/metier/create', 'MetierController#create');
Route::post('/metier', 'MetierController#store');
Route::get('/metier/{id}/show', 'MetierController#edit');
Route::get('/metier/{id}/edit', 'MetierController#edit');
Route::upd('/metier/{id}/update', 'MetierController#update');
Route::delete('/metier/{id}', 'MetierController#destroy')
MetierController.php
public function edit($id)
{
$metier=Metier::find($id);
return view('metier.edit',['libelle_metier'=>$metier]);
}
/**
* Update the specified resource in storage.
*
* #param \Illuminate\Http\Request $request
* #param int $id
* #return \Illuminate\Http\Response
*/
public function update(Request $request, $id)
{
$metier = Metier::find($id);
$metier->libelle_metier = $request->libelle_metier;
$metier->save();
return back();
}
edit.blade.php
#extends('Layouts/app')
#extends('Layouts.master')
#section('content')
<div class="container">
<div class="row">
<div class="col-md-10">
<h1>Modifier Metier </h1>
<form action=" {{url ('metier') }}" method="post">
{{csrf_field()}}
<div class="form-group">
<label for="">libelle Metier </label>
<input type="text" name ="libelle_metier" class="form-
control"value ="
{{$libelle_metier->libelle_metier}}" >
</div>
<div class="form-group">
<input type="submit" value = "enregistrer" class="form-control
btn btn-
primary">
</div>
</form>
</div>
</div>
#endsection
That's because you don't even try to update the DB record. Do something like this instead:
public function update(Request $request, $id)
{
Metier::where('id', $id)->update($request->all());
return back();
}
Or without using the mass assignment:
public function update(Request $request, $id)
{
$metier = Metier::find($id);
$metier->libelle_metier = $request->libelle_metier;
$metier->save();
return back();
}
Update
Thanks for sharing the whole form. You're also using POST method instead of PUT. Change the form URL and add this field to the form:
<form action="{{ url('metier/' . $libelle_metier->id . '/update') }}" method="post">
{{ method_field('PUT') }}
Then the update() method will be executed instead of store().
And change the route to put:
Route::put('/metier/{id}/update', 'MetierController#update');
Also, it's a good idea to use Route::resource instead of manually creating the same routes. It will allow you to avoid this kind of errors.
https://laravel.com/docs/5.6/helpers#method-method-field
add {{ method_field('PATCH') }} to your form and change action to named route and pass metier id to it.
#extends('Layouts/app')
#extends('Layouts.master')
#section('content')
<div class="container">
<div class="row">
<div class="col-md-10">
<h1>Modifier Metier </h1>
<form action="{{ route('metier.update', $libelle_metier->id) }}" method="post">
{{csrf_field()}}
{{ method_field('PATCH') }}
<div class="form-group">
<label for="">libelle Metier </label>
<input type="text" name ="libelle_metier" class="form-
control"value ="
{{$libelle_metier->libelle_metier}}" >
</div>
<div class="form-group">
<input type="submit" value = "enregistrer" class="form-control
btn btn-
primary">
</div>
</form>
</div>
</div>
#endsection
Route file
Route::patch('/metier/{id}/update', 'MetierController#update')->name('metier.update');
Hint: delete all these
Route::get('/metier', 'MetierController#index');
Route::get('/metier/create', 'MetierController#create');
Route::post('/metier', 'MetierController#store');
Route::get('/metier/{id}/show', 'MetierController#edit');
Route::get('/metier/{id}/edit', 'MetierController#edit');
Route::upd('/metier/{id}/update', 'MetierController#update');
Route::delete('/metier/{id}', 'MetierController#destroy')
and either add just it all as a single resource, so that all REST urls will be added in one shot.
this is how should be if its REST specification. read it
REST Resource Naming Guide and here
Route::resource('metier', 'MetierController');
or add it this way instead of resource
Route::get('/metier', 'MetierController#index')->name('metier.index');
Route::get('/metier/create', 'MetierController#create')->name('metier.create');
Route::post('/metier', 'MetierController#store')->name('metier.store');
Route::get('/metier/{id}', 'MetierController#show')->name('metier.show');
Route::get('/metier/{id}/edit', 'MetierController#edit')->name('metier.edit');
Route::patch('/metier/{id}', 'MetierController#update')->name('metier.update');
Route::delete('/metier/{id}', 'MetierController#destroy')->name('metier.destroy');
Learn about Resource Controllers
Controller
public function edit($id)
{
$metier=Metier::find($id);
return view('metier.edit',['libelle_metier'=>$metier]);
}
public function update(Request $request, $id)
{
// do some request validation
$metier=Metier::find($id);
$metier->update($request->all());
return redirect()->route('metier.show', $metier->id);
}
if you are having mass assignment error.
add protected $guarded = []; to the Metier model

Issues with Laravel Mailables

I'm trying to create a contact form in Laravel using Laravel 5.3, but I get this nasty error here:
ErrorException in helpers.php line 519:
htmlspecialchars() expects parameter 1 to be string, object given (View: /Applications/XAMPP/xamppfiles/htdocs/meps/resources/views/emails/contactemail.blade.php)
Here are the files that I was using:
The contact form
<div class="contact-form">
<form class="margin-clear" role="form" action="{{ url('/sendmail') }}" method="POST">
{{ csrf_field() }}
<div class="form-group has-feedback">
<label for="name">Name*</label>
<input type="text" class="form-control" id="name" name="name" placeholder="">
<i class="fa fa-user form-control-feedback"></i>
</div>
<div class="form-group has-feedback">
<label for="email">Email*</label>
<input type="email" class="form-control" id="email" name="email" placeholder="">
<i class="fa fa-envelope form-control-feedback"></i>
</div>
<div class="form-group has-feedback">
<label for="subject">Subject*</label>
<input type="text" class="form-control" id="subject" name="subject" placeholder="">
<i class="fa fa-navicon form-control-feedback"></i>
</div>
<div class="form-group has-feedback">
<label for="message">Message*</label>
<textarea class="form-control" rows="6" id="message" name="message" placeholder=""></textarea>
<i class="fa fa-pencil form-control-feedback"></i>
</div>
<input type="submit" value="Submit" class="btn btn-primary">
</form>
</div>
The Controller function
public function sendmail(Request $request, Mailer $mail) {
$mail->to('kaley36_aw#yahoo.com')->send(new ContactEmail($request->name, $request->email, $request->subject, $request->message));
$request->session()->flash('mail-sent', 'Your email has been sent.');
return redirect('/contact');
}
The Mailable class
class ContactEmail extends Mailable
{
use Queueable, SerializesModels;
public $name;
public $email;
public $subject;
public $message;
/**
* Create a new message instance.
*
* #return void
*/
public function __construct($name, $email, $subject, $message)
{
$this->name = $name;
$this->email = $email;
$this->subject = $subject;
$this->message = $message;
}
/**
* Build the message.
*
* #return $this
*/
public function build()
{
return $this->from($this->email)->view('emails.contactemail');
}
}
And here is the route
Route::post('sendmail', 'EmailController#sendmail');
You probably looking at the wrong view. The error points to
/views/emails/contactemail.blade.php
But this view has a <form> unless you are sending back to your users a form via e-mail, this looks a lot more like your contact form view and not your e-mail view. Something like:
/views/contact.blade.php (or whatever you have in there as a form)
As for the error, you must have a {{ $variable or functionCall() }} which is not receiving a string, but an object.
I figured it out. The issue was with the variable $message, Laravel wrapped it in an actual object called Message. All I had to do was change the variable name to $theMessage and it worked find.

Resources