Laravel PayPal tutorial into real world scenario - php

I have been following the tutorial on http://laravelcode.com/post/how-to-integrate-paypal-payment-gateway-in-laravel-54
I'm now trying to use this in an an application i'm developing but i'm quite new to PHP/Laravel so trying to get these paypal functions into my own Controller/forms that I have already built.
I have a controller "BookingsController" which has a form on ../bookings/create that when the EU presses the submit button it will enter the info into the DB, which works perfect, and then runs the PayPal bits to take the EU to the PayPal checkout which is where i've come unstuck.
My Controller:-
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Market;
use App\Stall;
use App\Booking;
use Auth;
use GuzzleHttp\Transaction;
class BookingsController extends Controller
{
/**
* Display a listing of the resource.
*
* #return \Illuminate\Http\Response
*/
public function index()
{
//
}
/**
* Show the form for creating a new resource.
*
* #return \Illuminate\Http\Response
*/
public function create()
{
$market = Market::where('is_active', true)->orderBy('name')->pluck('name','id');
$stall = Stall::pluck('name','id')->all();
return view ('bookings.create', compact('market','stall'));
}
/**
* 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',
'email' => 'email',
'address' => 'required',
'phone' => 'required',
]);
//Uncomment the below line to test form values
// return $request->all();
$booking = new Booking;
$booking->name = $request->input('name');
$booking->email = $request->input('email');
$booking->address = $request->input('address');
$booking->phone = $request->input('phone');
$booking->market_id = $request->input('market_id');
$booking->stall_id = $request->input('stall_id');
$booking->itemtype = $request->input('itemtype');
$booking->clothesrail = $request->input('clothesrail');
$booking->businessname = $request->input('businessname');
$booking->insurance = $request->input('insurance');
//Get the stall cost
//$stallPrice = Stall::pluck('cost')->where('id', '=', $booking->stall_id);
$stallPrice = 15;
//Check if the user is logged in. If so then submit the user_id
if (Auth::check())
{
$booking->user_id = auth()->user()->id;
}
//return $stallPrice;
//$booking->save();
//Redirect user based on logged in session_status
if (Auth::check())
{
return redirect('/dashboard')->with('success', 'Stall Booked!');
}
return redirect('/confirm')->with('success', 'Stall Booked!');
}
/**
* 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)
{
$booking = Booking::find($id);
return view('bookings.edit')->with('booking', $booking);
}
/**
* 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',
'email' => 'email',
'address' => 'required',
'phone' => 'required',
]);
$booking = Booking::find($id);
$booking->name = $request->input('name');
$booking->email = $request->input('email');
$booking->address = $request->input('address');
$booking->phone = $request->input('phone');
$booking->market_id = $request->input('market_id');
$booking->stall_id = $request->input('stall_id');
$booking->itemtype = $request->input('itemtype');
$booking->clothesrail = $request->input('clothesrail');
$booking->businessname = $request->input('businessname');
$booking->insurance = $request->input('insurance');
//Check if the user is logged in. If so then submit the user_id
if (Auth::check())
{
$booking->user_id = auth()->user()->id;
}
$booking->save();
return redirect('/admin/dashboard')->with('success', 'Booking Updated');
}
/**
* Remove the specified resource from storage.
*
* #param int $id
* #return \Illuminate\Http\Response
*/
public function destroy($id)
{
$booking = Booking::find($id);
$booking->delete();
return redirect('/admin/dashboard')->with('success', 'Booking Removed');
}
}
My View with the form:-
#extends('layouts.app')
#section('content')
<div class="row">
<div class="col-md-8 col-md-offset-2">
<div class="panel panel-default">
<div class="panel-heading">Book a Stall</div>
<div class="alert alert-info" role="alert">Clothes Rails are not provided. You will need to provide this yourself.</div>
<div class="panel-body">
{!!Form::open(['action' => 'BookingsController#store','method' => 'POST'])!!}
#if (Auth::check())
{{Form::bsText('name',Auth::user()->name)}}
{{Form::bsText('email',Auth::user()->email)}}
{{Form::bsText('address',Auth::user()->address)}}
{{Form::bsText('phone',Auth::user()->phone)}}
#else
{{Form::bsText('name','',['placeholder' => 'Name'])}}
{{Form::bsText('email','',['placeholder' => 'Email'])}}
{{Form::bsText('address','',['placeholder' => 'Address'])}}
{{Form::bsText('phone','',['placeholder' => 'Phone'])}}
#endif
<div class="form-group">
{!! Form::label('market_id', 'Date:') !!}
{!! Form::select('market_id', $market , null, ['class'=>'form-control'])!!}
</div>
<div class="form-group">
{!! Form::label('stall_id', 'Type of stall:') !!}
{!! Form::select('stall_id', $stall , null, ['id' => 'stall_id', 'class'=>'form-control'])!!}
</div>
{{Form::bsText('itemtype','',['placeholder' => 'Type of items to sell'])}}
<div class="form-group">
{!! Form::label('clothesrail', 'Clothes Rail?:') !!}
{!! Form::label('clothesrail', 'Yes') !!}
{!! Form::radio('clothesrail', 1, ['class'=>'form-control'])!!}
{!! Form::label('clothesrail', 'No') !!}
{!! Form::radio('clothesrail', 0, ['class'=>'form-control'])!!}
</div>
<div class="form-group">
{!! Form::label('businessname', 'Business Name:') !!}
{!! Form::text('businessname', null, ['id' => 'businessname', 'class'=>'form-control hidden'])!!}
</div>
<div class="form-group">
{!! Form::label('insurance', 'Public Liability Insurance??:') !!}
{!! Form::label('insurance', 'Yes') !!}
{!! Form::radio('insurance', 1, ['id' => 'insurance', 'class'=>'form-control'])!!}
{!! Form::label('insurance', 'No') !!}
{!! Form::radio('insurance', 0, ['id' => 'insurance', 'class'=>'form-control'])!!}
</div>
{{Form::bsSubmit('Submit')}}
{!! Form::close() !!}
</div>
</div>
</div>
</div>
#endsection
NOTES:
The submit button calls the "store" method in the controller
I can't get the Stall Price to pull from the user's selection into the variable but I'll deal with that later as I can fix that, i'm just working with a fixed value variable to get it working.

Just change:
$stallPrice = Stall::pluck('cost')->where('id', '=', $booking->stall_id);
TO
$stallPrice = DB::table('your_stall_table')->find($booking->stall_id)->cost;
Here, you need to your_stall_table with your actual stall table name.

Related

Create view in one controller and store data in other controller laravel

I'm new to Laravel and I'm trying to store a form. I created the view with the House controller but now I want to store the data in the view with the Booking controller. But when I click the button nothing happens.
My question is if it is possible to make a view with one controller and store it with another controller or maybe there is an other solution.
I also want to use the id of the house to store. How do I get that in the other controller as well?
Web Route
<?php
use Illuminate\Support\Facades\Route;
/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/
Route::get('/', [\App\Http\Controllers\HouseController::class, 'index']);
Route::get('house/{house}', [\App\Http\Controllers\HouseController::class, 'show']);
Route::post('house/{house}', [\App\Http\Controllers\BookingController::class, 'store']);
Route::get('rental', [\App\Http\Controllers\HouseController::class, 'getUserHouses']);
Route::get('rental/new', [\App\Http\Controllers\HouseController::class, 'create']);
Route::post('rental/new', [\App\Http\Controllers\HouseController::class, 'store']);
Route::get('rental/edit/{house}', [\App\Http\Controllers\HouseController::class, 'edit']);
Route::put('rental/edit/{house}', [\App\Http\Controllers\HouseController::class, 'update']);
Auth::routes();
Booking controller
<?php
namespace App\Http\Controllers;
use App\Models\Booking;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
class BookingController extends Controller
{
/**
* Display a listing of the resource.
*
* #return \Illuminate\Http\Response
*/
public function index()
{
//
}
/**
* Show the form for creating a new resource.
*
* #return \Illuminate\Http\Response
*/
public function create()
{
//
}
/**
* Store a newly created resource in storage.
*
* #param \Illuminate\Http\Request $request
* #return \Illuminate\Http\Response
*/
public function store(Request $request)
{
$newBooking = Booking::create([
'user_id' => Auth::id(),
'house_id' => $request->id,
'begin' => $request->begin,
'end' => $request->end,
'status' => 0
]);
return redirect('/');
}
/**
* Display the specified resource.
*
* #param \App\Models\Booking $booking
* #return \Illuminate\Http\Response
*/
public function show(Booking $booking)
{
//
}
/**
* Show the form for editing the specified resource.
*
* #param \App\Models\Booking $booking
* #return \Illuminate\Http\Response
*/
public function edit(Booking $booking)
{
//
}
/**
* Update the specified resource in storage.
*
* #param \Illuminate\Http\Request $request
* #param \App\Models\Booking $booking
* #return \Illuminate\Http\Response
*/
public function update(Request $request, Booking $booking)
{
//
}
/**
* Remove the specified resource from storage.
*
* #param \App\Models\Booking $booking
* #return \Illuminate\Http\Response
*/
public function destroy(Booking $booking)
{
//
}
}
House controller
<?php
namespace App\Http\Controllers;
use App\Models\house;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use App\Helper\Imageable;
use DB;
class HouseController extends Controller
{
/**
* Display a listing of the resource.
*
* #return \Illuminate\Http\Response
*/
public function index()
{
$houses = House::all();
return view('/home', [
'houses' => $houses
]);
}
/**
* Display a listing of the houses the owner has
*
* #return \Illuminate\Http\Response
*/
public function getUserHouses()
{
$houses = DB::table('houses')
->where('user_id', '=', Auth::id())
->get();
return view('/rental/rental', [
'houses' => $houses
]);
}
/**
* Show the form for creating a new resource.
*
* #return \Illuminate\Http\Response
*/
public function create()
{
return view('rental/new');
}
/**
* Store a newly created resource in storage.
*
* #param \Illuminate\Http\Request $request
* #return \Illuminate\Http\Response
*/
public function store(Request $request)
{
$path = Imageable::storeMedia($request);
$request->online === 'on' ? $online = 1 : $online = 0;
$newHouse = House::create([
'title' => $request->title,
'price_per_night' => $request->price,
'summary' => $request->summary,
'place' => $request->place,
'country' => $request->country,
'user_id' => Auth::id(),
'online' => $online,
'image' => $path,
]);
return redirect('rental');
}
/**
* Display the specified resource.
*
* #param \App\Models\house $house
* #return \Illuminate\Http\Response
*/
public function show(house $house)
{
return view(
'/house',
[
'house' => $house
]
);
}
/**
* Show the form for editing the specified resource.
*
* #param \App\Models\house $house
* #return \Illuminate\Http\Response
*/
public function edit(house $house)
{
return view(
'rental/edit',
[
'house' => $house
]
);
}
/**
* Update the specified resource in storage.
*
* #param \Illuminate\Http\Request $request
* #param \App\Models\house $house
* #return \Illuminate\Http\Response
*/
public function update(Request $request, house $house)
{
$path = Imageable::storeMedia($request);
$request->online === 'on' ? $online = 1 : $online = 0;
$house->update([
'title' => $request->title,
'price_per_night' => $request->price,
'summary' => $request->summary,
'place' => $request->place,
'country' => $request->country,
'online' => $online,
'image' => $path,
]);
return redirect('rental/edit/' . $house->id);
}
/**
* Remove the specified resource from storage.
*
* #param \App\Models\house $house
* #return \Illuminate\Http\Response
*/
public function destroy(house $house)
{
//
}
}
View
#extends('layouts.app')
#section('content')
<div class="container">
<div class="row">
<div class="col-12">
<h1 class="display-one ">{{ $house->title }}</h1>
<p class=".text-light">{{ $house->place }}, {{ $house->country }}</p>
</div>
</div>
<div class="row mt-5">
<div class="col-sm-6">
<img src="{{ asset("img/houses/$house->image") }}" alt="{{ $house->title }}" class="img-fluid" />
</div>
<div class="col-sm-6">
<div class="form-group">
<label for="exampleFormControlSelect1">Kies een datum en reserveer direct</label>
<form method="POST" action="">
#csrf
<input type="date" name="begin">
<input type="date" name="end">
<div class="col-md-12 bg-light mt-3">
<button type="button" class="btn btn-warning ml-2">Vraag aan</button>
</div>
</form>
</div>
</div>
</div>
<div class="row mt-3">
<div class="col-sm-6">
<p class="display-one ">{{ $house->summary }}</p>
</div>
<div class="col-sm-6">
<h2 class="display-one ">Aangeboden door</h2>
<p>Prijs per nacht €{{ $house->price_per_night }}</p>
</div>
</div>
</div>
#endsection
First of all, nothing happens when you click the form submit button because it is currently type="button" and in order this button to play role of submission button it must be type="submit". You can do whatever you want with Laravel. If you want your form to hit another controller method you can simply specify that in your form tag. Like so:
Imagine this is a form inside a view that is rendered by HouseController
<form method="POST" action="{{ url('/save/from/booking/controller') }}">
// ....
</form>
And now on form submission inside a view that is rendered by HouseController, you will actually hit a route that is BookingController responsive for. And here is your route that is being hit by the form
Route::post('/save/from/booking/controller', [BookingController::class, 'store']);

Laravel route to add in pivot table works function in model with controller not

Route:
Route::get('/addItemToOrder/{orderId}/{itemId}/{qt}', function ($orderId, $itemId, $qt) {
$order = Order::find($orderId);
$item = Item::find($itemId);
$order->items()->attach($item, ['qt' => $qt]);
});
WORKS Fine.
Pivot model
class ItemOrder extends Pivot
{
//
protected $fillable = [
'order_id',
'item_id',
'qt'
];
static public function create($orderId, $itemId, $qt)
{
$order = Order::find($orderId);
$item = Item::find($itemId);
$order->items()->attach($item, ['qt' => $qt]);
}
Controller
class ItemOrderController extends Controller
{
/**
* Display a listing of the resource.
*
* #return \Illuminate\Http\Response
*/
public function index()
{
//
$itemOrders = ItemOrder::all();
return view('itemOrder.index', compact('itemOrders'));
}
/**
* Show the form for creating a new resource.
*
* #return \Illuminate\Http\Response
*/
public function create()
{
//
$items = Item::pluck('name', 'id');
$orders = Order::pluck( 'account_id', 'id');
$qt = 0;
return view('itemOrder.create', compact('items', 'orders', 'qt'));
}
/**
* Store a newly created resource in storage.
*
* #param \Illuminate\Http\Request $request
* #return \Illuminate\Http\Response
*/
public function store(ItemOrdersRequest $request)
{
//
ItemOrder::create($request->all());
return redirect('/itemOrder');
}
Request
class ItemOrdersRequest 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 [
//
'order_id'=>'required',
'item_id'=>'required',
'qt'=>'required'
];
}
}
Blade create
{!! Form::open(['action' =>'App\Http\Controllers\ItemOrderController#store', 'method' => 'post', 'enctype' => 'multipart/form-data']) !!}
{!! form::label('order_id', 'Order_id: ') !!}
{!! form::select('order_id',$orders) !!}
{!! form::label('item_id', 'Item_id: ') !!}
{!! form::select('item_id',$items, null) !!}
{!! form::label('quantity', 'Quantity: ') !!}
{!! form::number('qt',$qt) !!}
{!! form::submit('Add Item to Order') !!}
{!! Form::close() !!}
Error is
Too few arguments to function App\Models\ItemOrder::create(), 1 passed
in
C:\Users\ma\ecom-laravel\app\Http\Controllers\ItemOrderController.php
on line 50 and exactly 3 expected
Laravel Framework 8.38.0
Help please. Thank you.
Instead of $request->all() you have to pass 3 params to ItemOrder::create.
You have defined create method in your model. Better rename to a different name to avoid conflicts between Laravel's default method names.
public function store(ItemOrdersRequest $request)
{
ItemOrder::create($request->order_id,$request->item_id,$request->qt);
return redirect('/itemOrder');
}

Returning select option value when editing in laravel

I am trying to edit a course. Now there is a drop down select field where you choose the program that course belongs to. I want to be able to return the program to the select field when i click on the edit button. Right now i am getting a Invalid argument supplied for foreach() error when i click on the edit button. How can i fix this and be able to get the value into the select field?
My code:
blade snippet
{!! Form::open(['action'=>['CoursesController#update', $course->id], 'method'=>'POST']) !!}
<div class="form-group row justify-content-center">
{{Form::label('program_code', 'Program Code')}}
<div class="col-md-4">
{!! Form::select('program_code', $course->program_code, ['class'=>'form-control']) !!}
</div>
</div>
<div class="form-group row justify-content-center">
{{Form::label('course_code', 'Course Code')}}
<div class="col-md-4">
{{Form::text('course_code',$course->course_code,['class'=>'form-control', 'placeholder'=>'Course Code'])}}
</div>
</div>
<div class="form-group row justify-content-center">
{{Form::label('course_name', 'Course Name')}}
<div class="col-md-4">
{{Form::text('course_name',$course->course_name,['class'=>'form-control', 'placeholder'=>'Course Name'])}}
</div>
</div>
<div class="form-group row justify-content-center">
{{Form::hidden('_method', 'PUT')}}
{{Form::submit('Add Program', ['class'=>'btn btn-success'])}}
</div>
{!! Form::close() !!}
controller
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Course;
use App\Program;
class CoursesController extends Controller
{
/**
* Display a listing of the resource.
*
* #return \Illuminate\Http\Response
*/
public function index()
{
$courses = Course::all();
return view('courses.index')->with('courses', $courses);
}
/**
* Show the form for creating a new resource.
*
* #return \Illuminate\Http\Response
*/
public function create()
{
// $programs = Program::all();
// return view('courses.create')->with('programs', $programs);
$programs = Program::all();
$select = [];
foreach($programs as $program){
$select[$program->program_code] = $program->program_name;
}
return view('courses.create', compact(['programs','select']));
}
/**
* Store a newly created resource in storage.
*
* #param \Illuminate\Http\Request $request
* #return \Illuminate\Http\Response
*/
public function store(Request $request)
{
$this->validate($request, [
'program_code' => 'required',
'course_code' => 'required',
'course_name' => 'required'
]);
$course = new Course;
$course->program_code = $request->input('program_code');
$course->course_code = $request->input('course_code');
$course->course_name = $request->input('course_name');
$course->save();
return redirect('/home/courses')->with('success', 'Course Successfully addded!');
}
/**
* 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)
{
$course = Course::find($id);
return view('courses.edit')->with('course', $course);
}
/**
* 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, [
'program_code' => 'required',
'course_code' => 'required',
'course_name' => 'required'
]);
$course = Course::find($id);
$course->program_code = $request->input('program_code');
$course->course_code = $request->input('course_code');
$course->course_name = $request->input('course_name');
$course->save();
return redirect('/home/courses')->with('success', 'Course Successfully updated!');
}
/**
* Remove the specified resource from storage.
*
* #param int $id
* #return \Illuminate\Http\Response
*/
public function destroy($id)
{
//
}
}
For Forms select method parameter sequence is
name
list of items
default select value
extra parameters
You need to change code from
{!! Form::select('program_code', $course->program_code, ['class'=>'form-control']) !!}
to
{!! Form::select('program_code',["Option1","Option2","Option3",..], $course->program_code, ['class'=>'form-control']) !!}

Unable to upload image in Laravel: "The "C:\xampp\tmp\php38A9.tmp" file does not exist or is not readable."

So I'm trying to upload an image on update() function and it keeps giving me "C:\xampp\tmp\php38A9.tmp" file does not exist or is not readable." error. Following is my code:
EditForm.blade.php (Form with the Image Input):
{!! Form::model(Auth::user(),array('route'=>['profile.update',Auth::user()->id],'method'=>'PUT','files'=>'true')) !!}
<div class="form-group form-row">
<div class="col">
{!! Form::text('fname',null,['class'=>'form-control','placeholder'=>'Enter First Name']) !!}
</div>
<div class="col-5">
<div class="custom-file">
{!! Form::file('img',['class'=>'custom-file-input']) !!}
{!! Form::label('Choose Avatar',null,['class'=>'custom-file-label']) !!}
</div>
</div>
</div>
<div class="form-group">
{!! Form::text('lname',null,['class'=>'form-control','placeholder'=>'Enter Last Name']) !!}
</div>
<div class="form-group">
{!! Form::email('email',null,['class'=>'form-control','placeholder'=>'Enter Email']) !!}
</div>
<div class="form-group">
{!! Form::password('password',['class'=>'form-control','placeholder'=>'Enter Student Password']) !!}
</div>
<div class="form-group">
{!! Form::text('name',null,['class'=>'form-control','placeholder'=>'Enter Student Username']) !!}
</div>
<div class="form-group">
{!! Form::number('rollno',null,['class'=>'form-control','placeholder'=>'Enter Roll Number']) !!}
</div>
<div class="form-group">
{!! Form::select('class', [
'1st' => '1st',
'2nd' => '2nd',
'3rd' => '3rd',
'4th' => '4th',
'5th' => '5th',
'6th' => '6th',
'7th' => '7th',
'8th' => '8th',
'9th' => '9th',
'10th' => '10th',],
null, ['class'=>'custom-select','placeholder' => 'Choose Student Class']); !!}
</div>
<div class="form-group py-4">
{!! Form::submit('Create',['type'=>'submit','class'=>'btn btn-danger btn-block']) !!}
</div>
{!! Form::close() !!}
ProfileController.php:
class ProfileController extends Controller
{
/**
* Display a listing of the resource.
*
* #return \Illuminate\Http\Response
*/
public function index()
{
$users = User::all();
return view('myprofile',compact('users'));
}
/**
* Show the form for creating a new resource.
*
* #return \Illuminate\Http\Response
*/
public function create()
{
//
}
/**
* Store a newly created resource in storage.
*
* #param \Illuminate\Http\Request $request
* #return \Illuminate\Http\Response
*/
public function store(Request $request)
{
//
}
/**
* 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(User $user)
{
$user = User::all();
return view('editprofile',compact('user'));
}
/**
* Update the specified resource in storage.
*
* #param \Illuminate\Http\Request $request
* #param int $id
* #return \Illuminate\Http\Response
*/
public function update(User $user, FileRequest $request)
{
if($request->hasfile('img')){
//getting the file from view
$image = $request->file('img');
$image_size = $image->getClientSize();
//getting the extension of the file
$image_ext = $image->getClientOriginalExtension();
//changing the name of the file
$new_image_name = rand(123456,999999).".".$image_ext;
$destination_path = public_path('/images');
$image->move($destination_path,$new_image_name);
//saving file in database
$user->image_name = $new_image_name;
$user->image_size = $image_size;
$user->save();
}
$user = Auth::user()->update($request->only(
'fname',
'lname',
'name',
'email',
'password',
'rollno',
'class',));
return redirect()->route('profile.index');
}
/**
* Remove the specified resource from storage.
*
* #param int $id
* #return \Illuminate\Http\Response
*/
public function destroy($id)
{
//
}
}
FileRequest.php (Request to validate file types):
class FileRequest 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 [
'img' => 'mimes:jpeg,gif,png'
];
}
}
Edit Button (This is the button that the user clicks to get to EditProfile.blade.php):
{{ link_to_route('profile.edit','Edit Profile',[Auth::user()->id],['class'=>'btn btn-danger btn-block']) }}
So, when I upload the image and click Edit, it just gives me the error (I've attached a pictue of the error for everyone to see). Please let me know what I'm doing wrong here. Feel free to ask me to show more code if required.
I recently faced this problem and to fix this i used below method.
First go to your config/filesystems.php and inside disks array replace the local with below
'local' => [
'driver' => 'local',
'root' => public_path(),
],
Nad then in controller you can use it like below
if ($request->img) {
$file = $request->File('img');
$ext = $user->username . "." . $file->clientExtension();
$file->storeAs('images/', $ext);
$user->image_name = $ext;
}
I've been facing this problem for a while, for some reason, the below fix helps me. If you're on windows, it could be because of symlink problem. Try this:
php artisan config:cache
php artisan storage:link it doesn't matter if you've already linked it. These commands and then try re-uploading it again. I hope this helps.

Laravel: Trouble with update db after click button

I have problem: I build an app and I have 2 tables in my db: habits and users. The User has a field called points and after click a button 'Add point' I want to increment that value. I added a method in HabitsController called addPoints and button in a view, but I get an error message: MethodNotAllowedHttpException No message. There is my code.
HabitsController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Habit;
use App\User;
use DB;
use App\Quotation;
class HabitsController extends Controller
{
/**
* Create a new controller instance.
*
* #return void
*/
public function __construct()
{
$this->middleware('auth');
}
/**
* Display a listing of the resource.
*
* #return \Illuminate\Http\Response
*/
public function index()
{
//store in $habits all posts of current user
$habits = DB::table('habits')->where('user_id', auth()->id())->get();
return view('habits.index')->with('habits', $habits);
}
/**
* Show the form for creating a new resource.
*
* #return \Illuminate\Http\Response
*/
public function create()
{
return view('habits.create');
}
/**
* 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',
'description'=>'required',
'difficulty'=>'required'
]);
$habit = new Habit;
$habit->name = $request->input('name');
$habit->description = $request->input('description');
$habit->difficulty = $request->input('difficulty');
$habit->NumberOfCompleted = 0;
$habit->user_id = auth()->user()->id;
$habit->save();
return redirect('/habits')->with('success', 'Habit created');
}
/**
* Display the specified resource.
*
* #param int $id
* #return \Illuminate\Http\Response
*/
public function show($id)
{
$single = Habit::find($id);
return view('habits.show')->with('single', $single);
}
/**
* Show the form for editing the specified resource.
*
* #param int $id
* #return \Illuminate\Http\Response
*/
public function edit($id)
{
$single = Habit::find($id);
return view('habits.edit')->with('single', $single);
}
/**
* 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',
'description'=>'required',
'difficulty'=>'required'
]);
$habit = Habit::find($id);
$habit->name = $request->input('name');
$habit->description = $request->input('description');
$habit->difficulty = $request->input('difficulty');
$habit->NumberOfCompleted = 0;
$habit->save();
return redirect('/habits')->with('success', 'Habit created');
}
/**
* Remove the specified resource from storage.
*
* #param int $id
* #return \Illuminate\Http\Response
*/
public function destroy($id)
{
$single = Habit::find($id);
$single->delete();
return redirect('/habits')->with('success', 'Habit deleted');
}
public function addPoint(Request $request, $id)
{
$habit = User::find($id);
$habit->points += 1;
$habit->save();
return redirect('/habits')->with('success', 'Habit created');
}
}
show.blade.php
#extends('layouts/app')
#section('content')
<div class="row single">
<div class="col-sm-8 col-sm-offset-2 showSingleHabit">
<h2>{{$single->name}}</h2>
<p>{{$single->description}}</p>
<p>{{$single->difficulty}}</p>
<p>{{$single->NumberOfCompleted}}</p>
Edit
{!!Form::open(['action' => ['HabitsController#destroy', $single->id], 'method' => 'POST'])!!}
{{Form::hidden('_method', 'DELETE')}}
{{Form::submit('Delete', ['class' => 'btn btn-danger'])}}
{!!Form::close()!!}
{!!Form::open(['action' => ['HabitsController#update', $single->id], 'method' => 'POST'])!!}
{{Form::hidden('_method', 'POST')}}
{{Form::submit('Add point', ['class' => 'btn btn-primary'])}}
{!!Form::close()!!}
</div>
</div>
#endsection
routes/web.php
Route::get('/', 'PagesController#index');
Route::get('/about', 'PagesController#about');
Route::resource('habits', 'HabitsController');
Auth::routes();
Route::get('/dashboard', 'DashboardController#index');
I will be grateful for any help. :)
Change
{{Form::hidden('_method', 'POST')}}
To
{{Form::hidden('_method', 'PUT')}}

Categories