Laravel : Create Button returns the same view but blank and does not create any record - laravel

I am new to laravel and I am working on creating a CMS. I have created a view to create posts but after I input data and click create to submit, page refreshes and shows the same view but with no input and no record created, when it should show a flash message and return the posts index view.
Here is my VIEW HTML:
#section('content')
<div class="card card-default">
<div class="card-header">
{{ isset($post) ? 'Edit Post' : 'Create Post' }}
</div>
<div class="card-body">
<form action="{{ isset($post) ? route('posts.update', $post->id) : route('posts.store') }}" method="POST" enctype="multipart/form-data">
#csrf
#if (isset($post))
#Method('PUT')
#endif
<div class="form-group">
<label for="title">Title</label>
<input type="text" class="form-control" name="title" id="title" value="{{isset($post) ? $post->title : ""}}">
</div>
<div class="form-group">
<label for="description">Description</label>
<textarea name="description" id="description" cols="5" rows="3" class="form-control">{{isset($post) ? $post->description : ""}}</textarea>
</div>
<div class="form-group">
<label for="content">Content</label>
<input id="content" type="hidden" name="content" value="{{isset($post) ? $post->content : ""}}">
<trix-editor input="content"></trix-editor>
</div>
<div class="form-group">
<label for="published_at">Published at</label>
<input type="text" class="form-control" name="published_at" id="published_at" value="{{isset($post) ? $post->published_at : ""}}">
</div>
#if (isset($post))
<div class="form-group">
<img src="{{ asset('/storage/'.$post->image) }}" alt="" style="width: 100%">
</div>
#endif
<div class="form-group">
<label for="category">Category</label>
<select name="category" id="category" class="form-control">
#foreach($categories as $category)
<option value="{{$category->id}}">{{$category->title}}</option>
#endforeach
</select>
</div>
<div class="form-group">
<label for="image">Image</label>
<input type="file" class="form-control" name="image" id="image">
</div>
<div class="form-group">
<button type="submit" class="btn btn-success">
{{ isset($post) ? 'Update Post' : 'Create Post' }}
</button>
</div>
</form>
</div>
</div>
#endsection
Here is METHOD:
public function store(CreatePostRequest $request)
{
$image = $request->image->store('posts');
Post::create([
'title'=>$request->title,
'description'=>$request->description,
'image'=>$image,
'content'=>$request->content,
'published_at' => $request->published_at,
'category_id' => $request->category
]);
session()->flash('success', 'Post Created Successfully');
return redirect(route('posts.index'));
}
What am I doing wrong?
Thanks,

Related

Insert into database postgres with october cms

Hi all I am new into october cms and I am facing trouble that makes my head spinning around. I have a form that get data from user, I am using builder.
This is my form :
{% put sudah %}{% partial "expert/yes" %}{% endput %}
{% put styles %}
<link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
<link rel="stylesheet" href="https://fonts.googleapis.com/css2?family=Ubuntu:wght#300;400;500;600;700&display=swap">
{% endput %}
<div class="text-center bg-primary">
<h4 class="text-white my-auto py-5 title-menu">Expert Registration Form</h4>
</div>
<div class="opening" id="first-menu">
<p>Expert Registration Form</p>
<button onclick="changeMenu()" class="btn btn-primary">
Bergabung
<i class="fa fa-arrow-right" aria-hidden="true"></i>
</button>
</div>
<div class="tab-content d-none pb-4" id="second-menu">
<div class=" container">
<div class="head-menu">
<p>Have you registered yet ?</p>
</div>
<div class="p-2">
<div class="form-check my-3">
<input class="form-check-input" type="radio" name="radioPick" id="yes" value="yes" checked>
<label class="form-check-label" for="radioPick">Yes</label>
</div>
<div class="form-check my-3">
<input class="form-check-input" type="radio" name="radioPick" id="no" value="no">
<label class="form-check-label" for="radioPick">No</label>
</div>
</div>
<div class="btn-next mt-4">
<button onclick="secondMenu()" class="btn btn-primary">Next</button>
</div>
</div>
</div>
<div id="yes" class="d-none">
{% placeholder yes%}
</div>
{% put scripts %}
<script>
const changeMenu = () => {
$( "#first-menu" ).addClass('d-none');
$( "#second-menu" ).removeClass('d-none');
}
const secondMenu = () => {
let radio = $('input[name=radioPick]:checked').val()
$( "#second-menu" ).addClass('d-none');
if(radio === 'yes'){
$( "#yes" ).removeClass('d-none');
}
}
</script>
{% endput %}
Then if yes, form for yes appeared
this is yes form :
<form method="POST" action="" accept-charset="UTF-8" enctype="multipart/form-data" id="example" class="something">
<input type="hidden" name="handler" value="onSave">
{{ form_token() }}
{{ form_sessionKey() }}
<div class="tab-content py-4" id="second-menu">
<div class="container">
<div class="content-letter tab">
<p class="title-letter">Please Fill This Form</p>
<div class="content-letter tab">
<div class="mt-3">
<div class="form-group row">
<div class="col-12">
<label class="text-dark font-weight-bold">Nama</label>
<input type="text" class="form-control" placeholder="Name" name="name"
id="name" required>
</div>
</div>
<div class="form-group row">
<div class="col-12">
<label class="text-dark font-weight-bold">Phone</label>
<input type="number" class="form-control" placeholder="Phone" name="phone"
id="phone" required>
</div>
</div>
<div class="form-group row">
<div class="col-12">
<label class="text-dark font-weight-bold">Signature</label>
<div class="row">
<div class="col-9 col-md-10">
<div class="custom-file">
<input type="file" id="signature" class="custom-file input-file"
name="signature" accept="image/x-png,image/gif,image/jpeg">
<label id="label-sign" for="sign"
class="custom-file-label label-files">Upload Signature</label>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
<button
id="btn-okay"
type="submit"
data-request="onSave"
data-hotkey="ctrl+s, cmd+s"
data-load-indicator="Creating New..."
class="btn btn-primary">
Join
</button>
</div>
</div>
</form>
and in code section I wrote this function :
function onSave() {
$expert= new Expert();
$model = new \Models\Expert;
$expert->name = Input::get('name');
$expert->phone = Input::get('phone');
$expert->sign= Input::file('signature');
$expert->save();
return Redirect::back;
//or even this one
/*$nama = Input::get('name');
$phone = Input::get('phone');
$sign = Input::file('signature');
DB::table('expert')->insert([
'name' => $name,
'phone' => $phone,
'sign' => $sign
]);
return Redirect::back;*/
}
and not forget I attach model in expert model :
public $attachOne = [
'signature' => 'System\Models\File'
];
Please help me, what is wrong with my code ? Thank you
Check the documents out on working with models. Your php function should be:
use Author\Plugin\Models\Expert;
function onSave() {
$expert= new Expert;
$expert->name = Input::get('name');
$expert->phone = Input::get('phone');
$expert->sign = Input::file('signature');
$expert->save();
return Redirect::back;
}

Error "The PATCH method is not supported for this route. Supported methods: GET, HEAD, POST. " update method

Well hello there, I am trying to update a register of my table assistants, but when I push the button submit of the form, this error appear, I am using a table pivot between assistants and events, but i am only try to edit a assistant.This is the error
This is my code AssistantController.php file and the methods edit and update
edit and update methods
public function edit(Assistant $assistant)
{
#obtain id assistant
$assistant_id = $assistant->id;
#get data event of the assistant to pass to the form
$event = Assistant::find($assistant_id)->events()->get();
return view('assistants.edit',compact('assistant','event'));
}
public function update(Request $request, Assistant $assistant)
{
#updating
$assistant->update($request->all());
//$assistants->user()->associate(Auth::user());
//
#obtain id assistant
$assistant_id = $assistant->id;
#get data event of the assistant to pass to the form
$event = Assistant::find($assistant_id)->events()->get();
return redirect('assistants.index',compact('assistant','event'));
}
This is my form to assistants
input form assistants
#csrf
<div class="form-group">
<label for="id">Document:</label>
<small class="text-muted">Required(*)</small>
<input type="number" class="form-control form-control-sm " value="{{ old('id') ?? $assistant->id }}" name="id" autofocus>
<div>{{ $errors->first('id') }}</div>
</div>
<div class="form-group">
<label for="name">Name:</label>
<small class="text-muted">Required(*)</small>
<input type="text" class="form-control form-control-sm " value="{{ old('name') ?? $assistant->name }}" name="name" placeholder="First Name assistant">
<div>{{ $errors->first('name') }}</div>
</div>
<div class="form-group">
<label for="last_name">Last name:</label>
<small class="text-muted">Required(*)</small>
<input type="text" class="form-control form-control-sm " value="{{ old('last_name') ?? $assistant->last_name }}" name="last_name" placeholder="Last name assistant">
<div>{{ $errors->first('last_name') }}</div>
</div>
<div class="form-group">
<label for="phone">Phone:</label>
<small class="text-muted">Required(*)</small>
<input type="number" class="form-control form-control-sm " value="{{ old('phone') ?? $assistant->phone }}" name="phone">
<div>{{ $errors->first('phone') }}</div>
</div>
<div class="form-group">
<label for="email">Email:</label>
<small class="text-muted">Required(*)</small>
<input type="mail" class="form-control form-control-sm " value="{{ old('email') ?? $assistant->email }}" name="email">
<div>{{ $errors->first('email') }}</div>
</div>
<div class="form-group">
<label for="observations">Observations:</label>
<input type="text" class="form-control form-control-sm " value="{{ old('observations') ?? $assistant->observations }}"name="observations">
<div>{{ $errors->first('observations') }}</div>
</div>
This is my edit page
#extends('layouts.back')
#section('title','Edit assistants')
#section('content')
<div class="container p-4">
<div class="row">
<div class="card">
<div class="card-header">
<div class="card-title">
<h3>Edit details to assistant:</h3><br>
<h4><strong> {{$assistant->name}} {{$assistant->last_name}}</strong> </h4>
</div>
</div>
<div class="card-body">
<form action="/assistants" method="POST">
#method('PATCH')
#include('assistants.form')
<button type="submit" class="btn btn-primary">Update</button>
Cancel
</form>
</div>
<div class="card-footer">
</div>
</div>
</div>
</div>
#endsection
There are my routes
routes
Auth::routes();
Route::get('/', 'HomeController#index')->name('home');
Route::resource('events', 'EventController');
Route::resource('assistants', 'AssistantController');
Route::resource('certificates', 'CertificateController');
Route::resource('signers', 'SignerController');
I am beginner.
thank you all.
In your edit page, change your form action from
<form action="/assistants" method="POST">
to
<form action="/assistants/{{ $assistant->id }}" method="POST">
Without the assistant id in the action field, Laravel thinks you are trying to update the base assistants route, which is an invalid action for that route.

How to fetch data from database related to key using Laravel?

I am a beginner, and I want to show the value data into the input field related to the key but I am very confused that how can I show the value data into the input field so please if you have an idea please help me thanks.
Database table
Setting table https://ibb.co/jGFX4t2
I want to show a value data in this field, please see https://ibb.co/Mh08c9b
Settings Model
class Settings extends Model
{
protected $table="setting";
protected $fillable =['id','key','value'];
}
Controller
public function setting()
{
$setting=Settings::all();
return view('admin.setting.setting',compact('setting'));
}
HTML view
<form method="post" action="{{route('update.setting')}}" enctype="multipart/form-data" >
#csrf
<div class="card-box">
<div class="panel panel-heading">
<h3>Update Settings</h3>
</div>
<div class="col-lg-5">
<div class="mt-3">
<input type="file" name="logo_image" class="dropify" />
</div>
</div>
<div class="row">
<div class="col-lg-5 mt-3">
<div class="group-form">
<label>Contact Number*</label>
<input type="text" name="contact_number" value="{{ }}" class="form-control" >
</div>
</div>
<div class="col-lg-5 mt-3">
<div class="group-form">
<label>Contact Email *</label>
<input type="email" name="email" value="{{ }}" class="form-control" >
</div>
</div>
<div class="col-lg-10 mt-3">
<div class="group-form">
<label>Location *</label>
<input type="text" name="location" value="{{ }}" class="form-control" >
</div>
</div>
<div class="col-lg-5 mt-3">
<h3> Social Links:</h3>
<div class="group-form">
<label>Facebook *</label>
<input type="text" name="facebook" value="{{ }}" class="form-control" >
</div>
</div>
<div class="col-lg-5 mt-5">
<div class="group-form">
<label>Twitter *</label>
<input type="text" name="twitter" value="{{}}" class="form-control" >
</div>
</div>
<div class="col-lg-6 mt-3">
<div class="group-form">
<label>Linkedin *</label>
<input type="text" name="linkedin" value="{{}}" class="form-control" >
</div>
</div>
<div class="col-lg-7 mt-3">
<div class="group-form ">
<button type="submit" id="btnsubmit" class="btn btn-danger waves-effect waves-light col-lg-2">Save</button>
</div>
</div>
</div>
</div> <!-- end card-box -->
</form>
please use this code.
{{ $setting->Where('key', 'Contact_Email')->first()->value }}
How about something like:
{{ $setting->firstWhere('key', 'Contact_Number')->value; }}
{{ $setting->firstWhere('key', 'Contact_Email')->value; }}
{{ $setting->firstWhere('key', 'Location')->value; }}
etc
When you use Eloquent Model::all(), it returns a collection to the blade view.
Therefore you can use firstWhere to find the key value.

Error About Laravel Blade Foreach in Loop if else

There are problem my code about foreach and if condition. When I pulled else code block out of foreach loop there are no problem it is work. But I put in else code in loop. Else dont work and dont show anything.
My purpose, programme check to used user_id in total_bonuses datatable if ok I want to display only update form if no records user_id in total_bonuses I want to show store forum and user add to info. It seems to complicated.
My controller code is below
public function show($lang=null, $id)
{
$bonuses = DB::table('reservations')
->select(DB::raw('SUM(bonus) as total_bonus, user_id'))
->where('user_id', '=', $id)
->groupBy('user_id')
->get();
$dolars = DB::table('reservations')
->select(DB::raw('SUM(dolar) as dolar, user_id'))
->where('user_id', '=', $id)
->groupBy('user_id')
->get();
$confirmeds = DB::table('reservations')
->select(DB::raw('SUM(confirmation) as confirmed, user_id'))
->where('confirmation', '=', 1)
->where('user_id', '=', $id)
->groupBy('user_id')
->get();
$user = DB::table('users')->find($id);
$totals = DB::table('total_bonuses')->where('user_id', '=', $id)->get();
return view('wallet.show', compact('totals', 'user', 'bonuses', 'dolars', 'confirmeds'));
}
View Code is below
#foreach ($totals as $total)
#if ($total->new_dollar !== NULL)
<div class="col-12"><a href="javascript:void(0)" class="link"><i class="mdi mdi-check-circle text-success"></i> <font class="font-medium"> {!! $total->new_dollar !!}
Remaining Dollar</font></a></div>
#endif
</div>
</center>
</div>
<div>
</div>
</div>
</div>
<!-- Column -->
<!-- Column -->
<div class="col-lg-8 col-xlg-9 col-md-7">
<div class="card">
#if (count($total->user_id) === 1)
<br>
<br>
<label class="col-md-12"><h4> Paid Dollar</h4></label>
<form class="form-horizontal form-material" method="POST" action="{{ route ('total.update', ['lang' => App::getLocale(), 'id' => $total->id]) }}">
{{ csrf_field() }}
<input type="hidden" name="_method" value="PATCH">
<input type="hidden" name='id' value='{!! $total->id !!}'>
<input type="hidden" name='user_id' value='{!! $user->id !!}'>
<div class="form-group">
<label class="col-md-12">Total Dollar</label>
<div class="col-md-12">
<input name="old_dollar" type="text" value="{!! $total->new_dollar !!}" class="form-control form-control-line" readonly>
</div>
</div>
<div class="form-group">
<label class="col-md-12">Paid Dollar</label>
<div class="col-md-12">
<input name="paid_dollar" type="text" class="form-control form-control-line">
</div>
</div>
<div class="form-group">
<label class="col-md-12">Note (Optionally! Not Required)</label>
<div class="col-md-12">
<input name="bonus_note" type="text" class="form-control form-control-line">
</div>
</div>
<input name="moderator_id" type="hidden" value="{{ Auth::user()->id }}">
<div class="form-group">
<div class="col-sm-12">
<button class="btn btn-success" type="submit">{{ __('profile.update')}}</button>
</div>
</div>
</form>
#else
<form class="form-horizontal form-material" method="POST" action="{{ route ('total.store', ['lang' => App::getLocale()]) }}">
{{ csrf_field() }}
<input type="hidden" name='user_id' value='{!! $user->id !!}'>
<div class="form-group">
<label class="col-md-12">Total Dollar</label>
<div class="col-md-12">
<input name="old_dollar" type="text" value="#foreach ($dolars as $dolar)
{{ $dolar->dolar }}
#endforeach" class="form-control form-control-line" readonly>
</div>
</div>
<div class="form-group">
<label class="col-md-12">Paid Dollar</label>
<div class="col-md-12">
<input name="paid_dollar" type="text" class="form-control form-control-line">
</div>
</div>
<div class="form-group">
<label class="col-md-12">Note (Optionally! Not Required)</label>
<div class="col-md-12">
<input name="bonus_note" type="text" class="form-control form-control-line">
</div>
</div>
<input name="moderator_id" type="hidden" value="{{ Auth::user()->id }}">
<div class="form-group">
<div class="col-sm-12">
<button class="btn btn-success" type="submit">{{ __('profile.update')}}</button>
</div>
</div>
</form>
#endif
#endforeach
Best wishes.
The problem in your provided view is with the if condition. Here you are trying to count the user id which is not possible:-
#if (count($total->user_id) === 1)
Change the above code to
#if (count($total) === 1)

Stripe form not submitting

My stripe form is not submitting. When I press submit it goes to page cannot be displayed. I replaced the route code to resolve to an echo "test"; and the post request shows the echo. Any help would be appreciated. This code is from a tutorial http://www.sitepoint.com/selling-downloads-stripe-laravel/
Route
Route::get('/buy/{id}', function($id)
{
$download = Download::find($id);
return View::make('buy', array('download' => $download));
});
Route::post('/buy/{id}', function($id)
{
Stripe::setApiKey(Config::get('laravel-stripe::stripe.sk_test_aaaaaaaaaaaaaaaaaaaaaaaa'));
$download = Download::find($id);
$token = Input::get('stripeToken');
// Charge the card
try {
$charge = Stripe_Charge::create(array(
"amount" => $download->price,
"currency" => "gbp",
"card" => $token,
"description" => 'Order: ' . $download->name)
);
// If we get this far, we've charged the user successfully
Session::put('purchased_download_id', $download->id);
return Redirect::to('confirmed');
} catch(Stripe_CardError $e) {
// Payment failed
return Redirect::to('buy/'.$id)->with('message', 'Your payment has failed.');
}
});
View
#extends('layouts.default')
#section('content')
<h1>Your Order</h1>
<h2>{{ $download->name }}</h2>
<p>£{{ ($download->price/100) }}</p>
<form action="" method="POST" id="payment-form" role="form">
<input type="hidden" name="did" value="{{ $download->id }}" />
<div class="payment-errors alert alert-danger" style="display:none;"></div>
<div class="form-group">
<label>
<span>Card Number</span>
<input type="text" size="20" data-stripe="number" class="form-control input-lg" />
</label>
</div>
<div class="form-group">
<label>
<span>CVC</span>
<input type="text" size="4" data-stripe="cvc" class="form-control input-lg" />
</label>
</div>
<div class="form-group">
<label>
<span>Expires</span>
</label>
<div class="row">
<div class="col-lg-1 col-md-1 col-sm-2 col-xs-3">
<input type="text" size="2" data-stripe="exp-month" class="input-lg" placeholder="MM" />
</div>
<div class="col-lg-1 col-md-1 col-sm-2 col-xs-3">
<input type="text" size="4" data-stripe="exp-year" class="input-lg" placeholder="YYYY" />
</div>
</div>
</div>
<div class="form-group">
<button type="submit" class="btn btn-primary btn-lg">Submit Payment</button>
</div>
</form>
#stop

Resources