I designed form to save data in Laravel
and this problem shows me the following error
Property [id] does not exist on this collection instance.
This is my Code
Controller
public function create ()
{
$post = Post::all();
return view ("post.create",compact('post'));
}
view create.blade.php
<form action="{{url()->action ('PostController#store', ['id'=>$post->id])}}" method="POST">
<input type="text" name="title" placeholder="Write Title">
<input type="text" name="body" placeholder="Write Body">
<input type="submit" value="Submit" name="submit">
</form>
I know the problem is here, but I don't know what I can do
'PostController#store', ['id'=>$post->id])}}" method="POST"
This will return the collection
$post = Post::all();
Instead, pass one object to the view by using
$post = Post::first();
or if u want to check particular post or any other condition you can use where clause to it..
eg: $post = Post::where('id',$user_id)->first(); // It will return single row...It will solve your problem
The problem is that you are sending a collection of Post models to your view. The $post variable contains a collection of Post models, which means that the collection will not have an id because it is a collection, not a single Post.
This:
$post = Post::all();
returns a collection.
Because this is a create method, you may wish to new up a Post model:
$post = new Post();
and add a few things to it and save it before sending to the view to get an id, OR, probably more useful: you can open the form without form-model binding since you don't actually have a Post model created yet -- just remove the ['id'=>$post->id] part since there is no post to have an id at the time the form is created.
You can make it look cleaner for the route.
web.php
Route::post('/myform/store', 'PostController#store')->name('post.store');
In your view, you can use the route name you just created.
<form action="{{ route('post.store', ['id' => $post->id]) }}" method="post">
But since you don't have column of 'id' in your Post then it returns the error you getting. Try creating a column 'id' first to resolve your problem.
I agree with #Watercayman on using model-binding instead, it's quick and makes the code more readable (and understandable too). Since Laravel quickly matches 'id' with unique id in the database. Take a look here for route parameters (how you can pass your data through URLs) and how to access your parameter here.
Using model binding will return you a collection.
public function store(Post $post)
Your $post variable is a collection so if you want to access your 'id' column you will do $post->id.
Related
I have a problem with id
i get error
Attempt to read property "id" on null (View: /resources/views/admin/Attendance/description.blade.php)
view code that show error
<form method="post" action="{{route('time.outd',['id'=>$attend->id])}}" enctype="multipart/form-data" >
controller code:
public function outd(Request $request, $id)
{
$request->validate([
'out'=>'',
'description'=>'required',
]);
$attend=Attendance::where('employee_id',$id)->orderBy('id','desc')->first();
$attend->description=$request->get('description');
$attend->update([
'out' => Carbon::now()
]);
$attend->save();
return redirect()->route('time.index')->with('success', ' success.');
}
I think you're just a little off here. '$attend->id you are setting the id to a null value because you haven't set it. From the controller that is passing the view of this form you need to pass that id to the front. So your collection of $attend is available on the front. Should be something like this in your controller action.
return view('index', compact('attend'));
Then in your form action just post to a non wildcard route...
action="{{route('time.outd')}}"
Then in your form elements put...
<input type="hidden" name="id" value="{{$attend->id}}">
Then in your controller where this is being posted to your id will be...
$request->id
Now you know if it is null on the back-end post that there was no record from the model and that you need to create a new one. If it has an id then you can go on about your day processing.
so i have a form that input data with <option>. the value is come from one database i provide. ( i can make the value from blade.php but the requirement require it from db). and the form will send the data to the main database. i've already make the view and the <option> work. but the trouble comes when i want to submit the data to the main database.
-the main db = blogs ( target column = 'sistem')
-the option db = sistems ( source column = 'nama')
the goal is the value of 'nama' passed to the 'sistem'
this is the view form
<form action="/" method="post" enctype="multipart/form-data">
// .......
// .......
<select name="sistem[]" id="tag_select">
<option value="0"> Tidak Ada </option>
#foreach ($sistems as $sistem)
<option value="{{$sistem->id}}"> {{$sistem->nama}}</option>
#endforeach
</select>
// .......
// .......
</form>
this is the store controller
public function store(Request $request)
{
// .........
// .........
$request -> sistem = array_unique(array_diff($request->sistem, [0]));
$blog -> sistem = $request -> sistem;
// .........
// .........
$blog -> save();
}
Like above and also are you shure that action "/" points to store method?
The save() method will save the changes that you make to your Eloquent model. Also you shouldn't need to edit the $request object to achieve the required result here.
Additionally you will need to ensure that this property on your model can be mass assigned (either in the $fillable array or excluded from the $guarded array on the $blog model).
$blog->sistem = array_unique(array_diff($request->sistem, [0]));
$blog->save();
I will show how i store things via post method and it works for me, but you must know that I am noob. Sory if what i writting is obvious for you, but cant do anything more :)
public function store(request $request)
{
$this->validatePost();
$post = new Post(request(
['title', 'excerpt','deadline']
));
$post->save();
return redirect(route('admin.posts'));
}
I am sort of new to the Laravel framework and I am building just a simple blog. I can create a blog, show a blog and show a overview of all blogs. Now I would like to delete a blog. So, I have created a delete button in my view with a route link which will pass also the id of the article. Then, in my routes file I specify a delete request and a controller method. In the method I find the id and try to delete the row with the id I specified in the route/view.
This doesn't work. Instead of activate the destroy/delete method it shows the article instead of deleting it and activates the show method instead of the delete method. Can somebody help me out, What do I wrong?
View.blade.php
<a href="{{route('nieuws.destroy', ['id' => $blog->id])}}" onclick="return confirm('Weet je dit zeker?')">
<i class="fa fa-trash"></i>
</a>
Route
Route::group(['middleware' => 'auth'], function () {
Route::get('/aanvragen', 'aanvragenController#index')->name('aanvragen.index');
Route::get('/logout' , 'Auth\LoginController#logout')->name('logout');
Route::get('/nieuws/toevoegen', 'blogController#create')->name('blogs.add');
Route::post('/nieuws/store', 'blogController#store')->name('nieuws.store');
Route::delete('/nieuws/{id}', 'blogController#destroy')->name('nieuws.destroy');
});
Route::get('/nieuws', 'blogController#index')->name('blogs.index');
Route::get('/nieuws/{blog}', 'blogController#show')->name('blogs.show');
Controller methods
Delete/Destroy
public function destroy($id) {
$blog = Blog::find($id);
$blog->delete();
return redirect('/nieuws');
}
Show
public function show(Blog $blog) {
dd('show');
return view('blogs.show', compact('blog'));
}
A delete() route requires you to POST your data.
HTML forms only supports GET and POST, other methods like DELETE, PUT, etc are not supported, that's why Laravel uses the _method to spoof methods which are not supported by HTML forms.
You do not want use GET in these cases, since someone can send a user the url (http://yoursite.com/blog/delete/1) in an IM or via email. The user clicks and the blog is gone.
Define your route as it would be when using resource controllers, so:
Route::delete('/nieuws/{id}', 'blogController#destroy')->name('nieuws.destroy');
And either use a form with the delete method:
// apply some inline form styles
<form method="POST" action="{{ route('nieuws.destroy', [$blog->id]) }}">
{{ csrf_field() }}
{{ method_field('DELETE') }}
<button type="submit">Delete</button>
</form>
Or do some javascript magic as the link SR_ posted in his comment on your OP.
One more thing, add some sort of validation in your destroy action. Right now when you provide a non-existing id or something else, you will get a 500 error, instead you want to have a 404.
public function destroy($id)
{
$blog = Blog::findOrFail($id);
$blog->delete();
return redirect('/nieuws');
}
I think you need to update your destroy function like:
public function destroy($id) {
$blog = DB::table('blog')->where('id',$id)->delete();
return redirect('/nieuws');
}
And update your view code like:
<a href="{{route('nieuws.destroy', [$blog->id])}}" onclick="return confirm('Weet je dit zeker?')">
<i class="fa fa-trash"></i>
</a>
Hope this work for you!
I'm also new to Laravel but I made it work through this way:
(I use 'Article' as the model's name and the resource "method" in the route stands for a bunch of useful routes including the route you wrote)
Controller:
public function destroy($id){
Article::find($id)->delete();
//$article = Article::find($id);
return redirect()->back()->withErrors('Successfully deleted!');
}
Route:
Route::resource('article','ArticleController');
However, I think the problem lies in the default definition of database's name of your model. Laravel will assume that you have a database named blogs since you have a model named "blog". Are you having the database's name right?
To use DELETE HTTP Verb, your form should consists of the POST method and settings the method_field('DELETE')
Example:
<form method="POST" action="{{ route('xxx.destroy', $xxx->id) }}">
{{ csrf_field }}
{{ method_field('DELETE') }}
</form>
As mentioned in the title I get the error "Property [id] does not exist on this collection instance." Only when I run the code online here are my relevant codes.
1-EmployeeController (browser tells me that the error is here the second line)
public function show(Employee $employee)
{
$employee = Employee::find ($employee);
$edocument = EDocument::where ('employee_id',$employee->id)->first();
return view ('employee.show')->withEmployee($employee)->withEdocument($edocument);
}
2-show.blade.php
<div class="jumbotron">
<h1>{{$employee->name}} ({{$employee->position}})</h1>
#if (isset($edocument))
Go To Employee Database Page
#else
<p class="lead bg-danger">Employee documents are not uploaded</p>
#endif
Create Employee Contract
if anyone can explain to me this error in more details that would be great also. thanks
ps.. this is my first laravel project (;
You use route model binding in your controller method to get the Employee model. But you also run a find, which would fail since you're passing the model instead of the id. Do as one of the codes shown below and don't mix them.
Do this if you want to use route model binding.
public function show(Employee $employee)
{
$edocument = EDocument::where ('employee_id', $employee->id)->first();
return view ('employee.show')->with(compact('employee', 'edocument'));
}
Do this if you want to pass the employee id and fetch the model in controller.
public function show($employee)
{
$employee = Employee::find($employee);
$edocument = EDocument::where ('employee_id', $employee->id)->first();
return view ('employee.show')->with(compact('employee', 'edocument'));
}
Maybe this can help you. Why don't you pass the information in the controller using -
return view('employee.show', ['employee' => $employee, 'edocument'=>$edocument]);
It worked for me. (Do not have to change anything in the show.blade .php)
I am trying to delete a category by clicking on a button
Blade:
<td class="center"><span class="glyphicon glyphicon-trash"></span></td>
Route:
Route::get('/deletecat/{name}','CategoryController#delete');
Controller:
public function delete($name)
{
category::find($name)->delete();
return Redirect::route('managecategory');
}
but I am getting an error while clicking on a button that
Call to a member function delete() on a non-object
Any help appreciated.
The ::find($id) method expects $id to be a number, the primary key of the row you want to find.
If you want to delete a row by name, you should use the following code:
category::where('name', $name)->delete();
The error is obviously because of the returned NON-OBJECT.
::find($name) returns an empty object or null
This is because the search was empty.
Make sure name is the primary key in order to use find.
Otherwise:
use ::where('name', $name)->delete();
or:
::where('name','=', $name)->delete();
*I pointed = because if you ever need to search different than = use >= <> and so on...
Secondly, it is not very important to use destroy. It is recommended but not a must-have!
In your view:
it has to be a form to submit a POST request like below:
<form action="/deletecat/{{$data->id}}" method="post">
#csrf
#method('DELETE')
<input type="submit" value="delete " >
</form>
Because this is POST Request has to use csrf and delete action.
In your route, it has be with delete() method,followwing with the controller.
Route::delete('/deletecat/{name}', 'CategoryController#destory');
In the controller:
public function destory($name)
{
category::find($name)->delete();
return Redirect::route('managecategory');
}
This is how laravel name format role: