I want delete two record from different table with one delete function. however it is only deleting one of the record even though I passed two different Ids.
this is my delete function
public function delete($id){
$user_id = auth() ->user()->id;
$card = Card::where('id', $id)->delete();
$actCity = city::where('id', $id)->delete();
return redirect('/home')->with('success', 'Post Removed');
this is my delete button
#if (!empty($cardd && $actCity))
{!!Form::open(['action'=>['PlanController#delete','id' =>$cardd[0], 'id'=>$actCity[0]],'method'=>'POST','class'=>''])!!}
#endif
{{Form::hidden('_method','DELETE')}}
{!! Form::submit('Delete', array(
'class' => 'btn btn-danger',
'onclick' => "if( ! confirm('Are you sure you want to delete your
package?')){return false;}"
)) !!} {!! Form::close() !!}
my route
Route::delete('delete/{id}', 'PlanController#delete');
You can not use same parameter name more than once,
Do something like this :
public function delete($id, $city_id){
$user_id = auth() ->user()->id;
$card = Card::where('id', $id)->delete();
$actCity = city::where('id', $city_id)->delete();
return redirect('/home')->with('success', 'Post Removed');
this is my delete button
#if (!empty($cardd && $actCity))
{!!Form::open(['route'=>['PlanController#delete','id' =>$cardd[0], 'city_id'=>$actCity[0]],'method'=>'POST','class'=>''])!!}
#endif
{{Form::hidden('_method','DELETE')}}
{!! Form::submit('Delete', array(
'class' => 'btn btn-danger',
'onclick' => "if( ! confirm('Are you sure you want to delete your
package?')){return false;}"
)) !!} {!! Form::close() !!}
my route
Route::delete('delete/{id}/{city_id}', 'PlanController#delete');
Related
I'm using Laravel and trying to build a gallery, i'm testing the upload of a file but i when i attach a file and click submit i can't get a positive outcome using the test set up. The code is below
GalleryController
// Store Gallery
public function store(Request $request){
// Get Request Input
$name = $request->input ('name');
$description = $request->input ('description', '');
$cover_image = $request->input ('cover_image');
$owner_id = 1;
// Check Image Upload
if($cover_image){
die ('YES');
} else {
die ('NO');
}
}
The form is set up as follows
{!! Form::open(array('action' => 'GalleryController#store', 'enctype' => 'multipart/form-data')) !!}
{!! Form::label ('name', 'Name') !!}
{!! Form::text ('name', $value = null, $attributes = ['placeholder' => 'Gallery Name', 'name' => 'name']) !!}
{!! Form::label ('description', 'Description') !!}
{!! Form::text ('name', $value = null, $attributes = ['placeholder' => 'Gallery Description', 'name' => 'Description']) !!}
{!! Form::label ('cover_image', 'Cover Image') !!}
{!! Form::file('cover_image') !!}
{!! Form::submit ('Submit', $attributes = ['class' => 'button']) !!}
{!! Form::close() !!}
Any help is appreciated
Thanks
Your form looks correct, it is likely your controller where you are retrieving the uploaded file.
As per the docs, to retrieve an uploaded file you should use $request->file():
$request->file('cover_image');
That link to the docs above also goes on to explain how you can check the file properly and store the file.
//use input facades.
use File;
use Illuminate\Support\Facades\Input;
public function store(Request $request){
// Get Request Input
$name = $request->input ('name');
$description = $request->input ('description', '');
$cover_image = $request->input ('cover_image');
$owner_id = 1;
// Check Image Upload
if(Input::hasFile('cover_image'){
die ('YES');
}
else {
die ('NO');
}
}
I'm learning Laravel. I'm trying to create a form that list the cars in a cars table and if clicked, sends into another form which is based on a DB query that returns the data of the chosen car (identified by $modelesc).
This form sends the data to a "orders" table. With the code that I have now, I'm not able to post the order on the order table. it gets the message: "MethodNotAllowedHttpException in RouteCollection.php line 233:"
This is the code
Web.php
Route::get('catalog', 'carController#catalog');
Route::get('orders/', 'CarController#orders')->name('orders');
CarController
function catalog() {
$cars = DB::table('cars')->get();
return view('catalog', compact('cars'));
}
function orders(Request $request) {
$modelesc = $request->modelesc;
$cars = DB::table('cars')->where('Model', $modelesc)->get();
$colours = DB::table('colours')->get()->pluck('Colour');
return view('orders', compact('cars', 'colours'));
}
Catalog.blade.php
#foreach($cars as $car)
{!! Form::open(array('action' => 'CarController#orders', 'method' => 'GET')) !!}
{!! Form::hidden('$modelesc', $car->Model) !!}
{!! Form::submit($car->Model) !!}
{!! Form::close() !!}
#endforeach
Orders.blade.php
#foreach($cars as $car)
{!! Form::open(['method' => 'POST']) !!}
{{ $car->Model }}
{!! Form::hidden('users_id', Auth::user()->id) !!}
{!! Form::hidden('Fabrication_date', date('Y-m-d')) !!}
{!! Form::select('Colour_id', $colours) !!}
{!! Form::hidden('Order_status_id', '1') !!}
{!! Form::submit('Ok') !!}
{!! Form::close() !!}
#endforeach
The table orders has following structure:
$table->increments('id');
$table->integer('users_id')->unsigned();
$table->foreign('users_id')->references('id')->on('users')->onDelete('cascade')->onUpdate('cascade');
$table->string('Model');
$table->date('Fabrication_date');
$table->integer('Colour_id')->unsigned();
$table->foreign('Colour_id')->references('id')->on('colours')->onDelete('cascade')->onUpdate('cascade');
$table->integer('Order_status_id')->unsigned();
$table->foreign('Order_status_id')->references('id')->on('order_status')->onDelete('cascade')->onUpdate('cascade');
$table->timestamps();
Update your orders function
public function orders(Request $request)
{
$modelesc = $request->modelesc;
$cars = DB::table('cars')->where('Model', $modelesc)->get();
...
}
Catalog.blade.php
Form::hidden('modelesc', $car->Model)
The value of your hidden input will be read by request->modelesc and you will endup with a URL that looks like this http://mysite.dev/orders?modelesc=toyota.
Some advice
You using a form to only submit a hidden input. No user input. It seems to me it would be a easier to use simple anchors <a></a>.
#foreach($cars as $car)
{{ $car->Model }}
#endforeach
Same result.
Make it nice with pretty URLs. Change your route definition
Route::get('orders/{modelesc}', 'CarController#orders')->name('orders');
And the anchor
{{ $car->Model }}
With named route
{{ $car->Model }}
And the function
public function orders($modelesc)
{
$cars = DB::table('cars')->where('Model', $modelesc)->get();
...
}
change your route to:
Route::get('orders/{model}', 'CarController#orders')->name('orders');
after this change your Controller to:
public function orders($model) {
$cars = DB::table('cars')->where('Model', $model)->get();
if(!count($cars)){
abort(404);
}
$colours = DB::table('colours')->get()->pluck('Colour');
//$status = DB::select('select * from order_status where id = ?', [1])->get(); //this variable is never used
return view('orders', compact('cars', 'colours'));
}
and now change your Catalog.blade.php
in your case you don't need a form for go in your order page
#foreach($cars as $car)
{{ $car->Model }}
#endforeach
when I try to delete specific file trough my controller I get an error
Sorry, the page you are looking for could not be found.
NotFoundHttpException
This is my controller:
public function destroyFile($file_name)
{
$file = storage_path('documents').'/'.$file_name;
Storage::delete($file);
return redirect('/documents');
}
My route:
Route::delete('documents/{file}','FilesController#destroyFile');
And my view:
{!! Form::open(['method' => 'DELETE', 'action' => ['FilesController#destroyFile', $file->name] ]) !!}
{!! Form::hidden('_method', 'DELETE') !!}
{!! Form::token() !!}
{!! Form::submit(trans('buttons.del-cat'),['class'=>'btn btn-danger user-delete push-right']) !!}
{!! Form::close() !!}
I had the same problem, everything was set right but it still didn't work.
Instead of Storage::delete() I used File::delete(), that fixed it.
Hope this works!
public function images(Request $request,$id){
$user_id=$request->session()->get('user_id');
$data=DB::table('company_images')->where('id',$id)->first();
//deleteing the image from database and server
DB::table('company_images')->where('id', '=', $id)->delete();
//delete the image form the server
$path=public_path("/assets/img/company/$data->image");
unlink($path);
return redirect("/company/$user_id/add_details")->with('delete','delete');
}
this is function i used to delete company images using unlink($path) this works well with me also the big note:you should write carefully the path to file you delete.
You must set public in filesystem.php :
'default' => env('FILESYSTEM_DRIVER', 'public')
instead of :
'default' => env('FILESYSTEM_DRIVER', 'local')
change local to public.
I have a model like this :
class Article extends Model {
protected $fillable = [
'title',
'body',
'img_profile', // store name of image
];
}
And a controller like this:
public function store(ArticleRequest $request){
$article = Auth::user()->articles()->create($request->all());
return redirect('articles');
}
And a form like this:
{!! Form::model($article = new \App\Article,['url' => 'articles','files' => true ]) !!}
{!! Form::text('title', null ) !!}
{!! Form::textarea('body', null) !!}
{!! Form::file ('img_profile', '') !!}
{!! Form::submit('Submit') !!}
{!! Form::close() !!}
When I submit the form, the img_profile field is stored default cached file which is /private/var/tmp/phpceBMP2.But I just want to update file_name in img_profile.
How can I change the value of the img_profile request before storing it in database?
You should use mutator for Article model this way:
public function setImgProfileAttribute($value)
{
$this->attributes['img_profile'] = '/private/var/tmp/phpceBMP2/'. $value;
}
However you should think if you really want to store the path in DB this way. What if you will want to change it in future? You will have to update all the records?
I have an issue with my the update method in my controller.
Normally everything works and it takes care of itself, but this time its not working.
My controller:
public function update($id)
{
$input = Input::all();
$validation = Validator::make($input, Vehicle::$rules, Vehicle::$messages);
if ($validation->passes())
{
$this->vehicle->update($id, $input);
return Redirect::route('admin.vehicles.index')->with('success', 'Car Updated');
}
return Redirect::back()
->withInput()
->withErrors($validation);
}
Repository:
public function update($id, $input)
{
print_r($id);
die();
}
This prints out:
{vehicle}
from the URI:
http://localhost/admin/vehicles/1/edit
My form:
{{ Form::open(['route' => 'admin.vehicles.update', 'class' => 'form-horizontal edit-vehicle-form', 'method' => 'PATCH']) }}
// inputs
<div class="form-group">
{{ Form::submit('Update Vehicle', ['class' => 'btn btn-success']) }}
</div>
{{ Form::close() }}
Route:
Route::resource('/admin/vehicles', 'VehiclesController');
Where is this going wrong? How can I get this form to send the ID not the word vehicle?