What can I do with this:
"Argument 1 passed to App\Policies\NotePolicy::update() must be an
instance of App\Note, instance of App\User given, called in
/Users/acny/Desktop/project/vendor/laravel/framework/src/Illuminate/Auth/Access/Gate.php
on line 481 (View:enter code here
/Users/acny/Desktop/project/resources/views/notes/edit.blade.php)"
NotePolicy.php
public function update(Note $note, User $user)
{
return $user->id == $note->user_id;
}
edit.blade.php (for show update button):
#can('update', $note)
<button type="submit" class="btn btn-primary">Update Note</button>
#endcan
If I write every thing instead of 'update' in edit.blade.php then I have not error but that's not what I want.
thank for your help
https://laravel.com/docs/5.6/authorization#writing-policies
Laravel passes the $user as the first argument. Switch your arguments around.
Related
I'm using Laravel and I am currently implementing the 'destroy' method in a resource controller as part of CRUD. The method should delete the specified record in my database.
The method is called by my blade.php file, which uses a form with the DELETE method and route('my-resources.destroy', $my-resource->id). I want the controller to return a string such as 'Deleted successfully!' so that I can display that to the client.
Here is my code:
In views/book/edit.blade.php:
<form method="DELETE" action="{{ route('books.destroy', $book->id) }}">
<div class="form-item center">
<button type="submit" class="btn-danger">Delete</button>
</div>
</form>
and in BookController.php:
public function destroy($id)
{
$book = Book::find($id);
$book->delete();
return redirect()->away('https://www.google.com');
}
I put a redirect to google.com just to see if the redirect works, but it doesn't. When I click the 'Delete' button, the url changes from http://127.0.0.1:8000/books/1/edit to http://127.0.0.1:8000/books/1? with that question mark at the end. What am I doing wrong?
I have another question: if I want to return the status, should I use something like
Route::get('/', function () {
return 'Deleted successfully';
});
or
$request->session()->flash('status', 'Task was successful!');
/** and some random return statement **/
Thanks!
Forms do not support the DELETE method so you need to use the Laravel #method helper to tell Laravel you want to use the DELETE verb. Additionally, you need to include the csrf token that Laravel expects are part of preventing cross-site request forgeries.
<form action="{{ route('books.destroy', $book->id) }}" method="POST">
#csrf
#method("DELETE")
... // Button/link for submit this form
</form>
You may need to define your route so that it accepts a DELETE request, unless you have defined a resourceful route:
Route::delete('/books/{id}', 'BookController#destroy')
->name('books.destroy'); // Laravel 7
Route::delete('/books/{id}', [\App\Http\Controllers\BookController::class, 'destroy'])
->name('books.destroy'); // Laravel 8
If you're using resourceful routes, they will be made available for you already:
Route::resource('/books', 'BookController'); // Laravel 7
Route::resource('/books', \App\Controllers\Http\BookController::class); // Laravel 8
For your destroy method, set a flash message to be sent back:
public function destroy(Book $id)
{
$id->delete();
return redirect('/')->with('success', 'Book deleted');
}
Then flash the message in your view:
#if (session('success'))
<p>{{ session('success') }}</p>
#endif
I can't to do this route works...
My Controller:
public function profissionais(Request $request, $id){
$profissionais = Vinculo::where('unidade_id', '=', $id)->get();
$profissionais = $id;
return view('relatorios.profissionais', compact('profissionais'));
}
My Form:
<form method="GET" action="{{route('relatorios.profissionais', 'id')}}">
<select class="js-example-basic-single" name="id" required>
#foreach($unidades as $unidade)
<option value="{{$unidade->id}}">{{$unidade->descricao}}</option>
#endforeach
</select>
<span class="input-group-btn">
<button class="btn btn-primary" type="submit">Listar</button>
</span>
</form>
web.php:
Route::get('/relatorios/profissionais/{id}', 'RelatorioController#profissionais')->name('relatorios.profissionais');
I like my route like this: /relatorios/profissionais/4 (4 is the ID) and the number 4 will the $id variable.
But the uri is like: relatorios/profissionais/id?id=4
Any help?
The second parameter of the route helper should be the value of the parameter, not the key.
{{route('relatorios.profissionais', 4)}}
Now, because you're setting this value from the form, you either need to use the request input instead of a route parameter or use javascript to modify the form action using a listener on the change event of your select element.
The reason you have /id is because of 'id' being the 2nd argument. The reason you have ?id=4 is because it is a form value, not a route parameter.
You're also overwriting $profissionais immediately after retrieving the collection
$profissionais = $id;
You can do this like:
Route::get('/relatorios/profissionais/{id?}', 'RelatorioController#profissionais')->name('relatorios.profissionais');
How to pass a value to hidden input ?
Create form :
#if (isset($id))
{!! Form::hidden('edition', $id) !!}
#endif
I got the form id by url like this :
Add Journal
( when I click Add Journal button it will shows a create form with edition id at the url)
and the controller is :
$id = $request->get('edition');
$journal = Edition::findOrFail($id)->journal()->create($input);
The result gave me this error "No query results for model [App\Edition]."
Usually, this is used in Blade templates.
Just pass the name and value to the method.
{{ Form::hidden('invisible', 'secret') }}
This creates a very simple element which looks like the following.
<input name="invisible" type="hidden" value="secret">
To add other attributes, pass a third argument to the method. This third argument must be an array.
{{ Form::hidden('invisible', 'secret', array('id' => 'invisible_id')) }}
Now the input has an id attribute.
<input id="invisible_id" name="invisible" type="hidden" value="secret">
Check out : Creating a Hidden Input Field
If still not work check you project have Laravel Collective installed
In controller method check
public function store(Request $request)
{
$name = $request->input('name');
}
you can do this with the help of blade
<input type="hidden" value="{{$user->id}}" name="user_id">
see docs here about old input
Route::post('/search/all/', function (Request $request) {
//...
$products = $query->paginate(15);
$data = ['products' => $products,
'oldinput' => $request->all()];
return view('inventory.search_products', $data);
});
in the view:
this works:
<input type="text" id="search_all" name="search_all" value="{{ $oldinput['search_all'] }}">
this is always empty:
<input type="text" id="search_all" name="search_all" value="{{ old('search_all') }}">
Just call flush in your controller then you can use old() helper function in your blade.
public function YourController(Request $request){
$request->flash();
return view('yourblade');
}
In blade file:-
<input id="lng" name="lng" value="{{old('lng')}}" type="hidden">
docs says you should flash() then call old() method.
flashing stores the previous request in the session. so it makes sense that old(search_all) doesn't work
I will suggest the following solution:
return view('inventory.search_products', $data)->withInput(\Input::all());
And in blade you can call as well \Input::old('search_all');.
Problem I want to call My controller function from my blade button and I face some errors.
(ErrorException in UrlGenerator.php line 603: Action
App\Http\Controllers\OrderController#Orderfun not defined. (View:
C:\Users\MalikTariq\Project\resources\views\admin.blade.php))
My controller function is:
public function Orderfun(){
// $data=Orders::all();
// foreach ($data as $key => $value) {
// echo "value is =".$value;
echo "My name is Malik ";
}
Route is:
Route::get('Order/Orderfun', 'AdminController#Orderfun');
My blade code is:
<button type="button" class="btn btn-danger" id="bt1">View Orders</button>
Use route method instead of action method in href attribute of a element in your view.