i'm trying to upload some files via form to my db and also in the storage of my project
I did the following code on my homepage :
<x-layout>
#if (session('message'))
<div class="alert alert-success">{{session('message')}}</div>
#endif
<div class="container vh-100">
<div class="row h-100 w-100 align-items-center">
<div class="offset-3 col-6">
<form method="POST" action="{{route('transfer.submit')}}" class="card" enctype="multipart/form-data">
#csrf
<div class="border w-100" id="fileWrapper">
<div class="mb-3 w-100 h-100">
<input type="file" class="form-control w-100 h-100 fileInput" id="fileupload" name="files[]" multiple >
</div>
</div>
<div class="mb-3">
<label for="exampleInputPassword1" class="form-label">Invia file a </label>
<input type="email" class="form-control" id="exampleInputPassword1" name="recipient_mail">
</div>
<div class="mb-3">
<label for="exampleInputPassword1" class="form-label">La tua mail</label>
<input type="email" class="form-control" id="exampleInputPassword1" name="sender_mail">
</div>
<div class="mb-3">
<input type="text" class="form-control" id="title" name="title">
</div>
<div class="mb-3">
<textarea name="message" cols="50" rows="10"></textarea>
</div>
<button type="submit" class="btn btn-primary">Submit</button>
</form>
</div>
</div>
</div>
</x-layout>
Then i done the following in my model :
protected $fillable = [
'recipient_mail',
'sender_mail',
'title',
'message',
'files[]'
];
and the following in my controller :
public function transferSubmit(TransferRequest $request){
$transfer = Transfer::create([
'sender_mail'=>$request->input('sender_mail'),
'recipient_mail'=>$request->input('recipient_mail'),
'title'=>$request->input('title'),
'message'=>$request->input('message'),
'files'=>$request->file('files[]')->store('public/files'),
]);
return redirect(route('home'))->with('message', 'File inviato con successo');
}
I havo also created the POST route and completed the migrations but, when i try to submit the form i get the following error :
Error Call to a member function store() on null
After this i tried the dd($request) ro check the data that i was actually passing to the Trasnfer class and i found that it is receiving correctly every data including the array of files.
Is there anybody that can help me to understand why i'm getting that error?
Thank you so much
You want store multiple files. And you will get an array. Then you have to iteratrate over your file array like that.
$files = [];
if($request->hasfile('files[]'))
{
foreach($request->file('files[]') as $file)
{
$files => $file->store('public/files'),
}
}
Important Note:
And don't forget the symlink before working with the Laravel storage.
php artisan storage:link
Updated
You iterate first then you have the file array which contains the paths to the images. you can then pass that to your model.
A little note: data coming from a form should always be validated.
public function transferSubmit(TransferRequest $request){
$files = [];
if($request->hasfile('files[]'))
{
foreach($request->file('files[]') as $file)
{
$files => $file->store('public/files'),
}
}
$transfer = Transfer::create([
'sender_mail'=>$request->input('sender_mail'),
'recipient_mail'=>$request->input('recipient_mail'),
'title'=>$request->input('title'),
'message'=>$request->input('message'),
'files'=> $files;
return redirect(route('home'))->with('message', 'File inviato con successo');
}
Related
I am new to programming especially laravel. I am trying to make a CRUD and have already added example data in prequel (using Docker). I can see the data, but when I´m trying to create new posts with a form I get Code 419 page expired. I know that´s normal and the solution is to add #csrf to the form. But after doing this I get 403 Forbidden. I tried a lot but can´t find a solution to fix it.
I would be really happy if someone could help me fix my problem.
Here is my create.blade.php
#section('content')
<div class="container">
<div class="row justify-content-center">
<div class="col-md-12">
<div class="card">
<div class="card-header">{{ __('Alle Gerichte') }}</div>
<div class="card-body">
<form action = "/recipe" method="POST">
#csrf
<div class="form-group">
<label for="name">Name</label>
<input type="text" class="form-control" id="name" name="name">
</div>
<div class="form-group">
<label for="beschreibung">Beschreibung</label>
<textarea class="form-control" id="beschreibung" name="beschreibung" rows="5"></textarea>
</div>
<input class="btn btn-primary mt-4" type="submit" value="absenden">
</form>
<a class="btn btn-primary btn-sm mt-3 float-right" href="/recipe"><i class="fas fa-circle-up"></i>Zurück</a>
</div>
</div>
</div>
</div>
</div>
#endsection
hi is that you have created validation rules
in StoreRecipeRequest
do that
public function authorize()
{
return true;
}
Controller Code will be like this:
public function store(StoreRecipeRequest $request)
{
//dd($request);
$recipe = new Recipe( [
'name' => $request->name,
'beschreibung' => $request->beschreibung,
]);
$recipe->save();
return redirect('/recipe');
}
Also if it's not solved. Then let's try it.
public function store(StoreRecipeRequest $request)
{
$recipe = new Recipe();
$recipe->name = $request->name;
$recipe->beschreibung = $request->beschreibung;
$recipe->save();
return redirect('/recipe');
}
also, can you add in your Recipe Model?
protected $fillable = [
'name',
'beschreibung',
];
I am trying to get hold of Laravel framework and got stuck while retrieving the values from the page in a Database.
These are the snippets of my controller, routes, view part which might help in understanding what I am doing
web.php (for routes):
<?php
use Illuminate\Support\Facades\Route;
Route::resource('posts', 'PostController');
Postcontroller.php (just a snippet)
<?php
public function create()
{
//
return view('posts.create');
}
public function store(Request $request)
{
//
$this->validate($request , array(
'title' => 'required',
'body' => 'required'
));
$post = new Post;
$post->title = $request->title;
$post->body = $request->body;
$post->save();
return redirect()->route('posts.show', $post->id);
}
create.blade.php(for view)
<div class="row">
<div class="col-mid-8 col-md-offset-2" >
<h1>Create New Post</h1>
<hr>
<form route='posts.store' >
#csrf
<div class="form-group">
<label for="exampleInputEmail1">Email address</label>
<input type="email" class="form-control" id="exampleInputEmail1" aria-describedby="emailHelp">
<small id="emailHelp" class="form-text text-muted">We'll never share your email with anyone else.</small>
</div>
<div class="form-group">
<label for="exampleInputPassword1">Password</label>
<input type="password" class="form-control" id="exampleInputPassword1">
</div>
<div class="form-group form-check">
<input type="checkbox" class="form-check-input" id="exampleCheck1">
<label class="form-check-label" for="exampleCheck1">Check me out</label>
</div>
<button type="submit" class="btn btn-primary">Submit</button>
</form>
</div>
</div>
The problem with your code is that you don't have name properties on your input, so nothing gets passed to your backend code.
Add name to your inputs like this (they need to be different, else they would get overwritten):
<input type="text" class="form-control" id="your-unique-input-id" name"your_unique_input_name">
Then, when you submit your form, the all the inputs that have name attribute in them will get passed to your backend method. You can then retrieve their value, by using the name you gave them earlier. So in this example:
$request->your_unique_input_name // Which will return anything that user typed in that field
How to edit one column in a row of the database in laravel
I can't update one column of row has multiple columns by laravel
My edit :
public function edit($id)
{
$addremark=bookappoitment::findOrFail($id);
return view('admin.ManageTime.addremarks', compact('addremark'));
}
My update:
public function update(Request $request, $id)
{
$request->validate([
'Remarks'=>'required'
]);
$data=bookappoitment::find($id);
$data->Remarks = $request->get('Remarks');
$data->save();
return view('/home');
}
link to update:
Update
form:
<div class="panel-body">
<div class="row">
<div class="col-lg-6">
<div class="form-group">
<label>Remarks :</label>
<textarea class="form-control" rows="10" name="Remarks" placeholder="Remarks">{{ $addremark->Remarks }}</textarea>
</div>
<a class="btn btn-success btn-mini deleteRecord " href="{{route('BookAppoint.update',$addremark->id)}}">Update</a>
</div>
</div>
</div>
You can update 1 (or more, just add to the array) column through the DB class.
DB::table('yourTable')->where('id', $id)->update(['remarks' => $request->input('Remarks')]);
You can also do it for a model like so:
bookappoitment::where('id', $id)->update(['remarks' => $request->input('Remarks')]);
It's in the Laravel documentation here.
<form class="" action="index.html" method="{{ route('BookAppoint.update', $addremark->id) }}">
#method('PATCH')
#csrf
<div class="panel-body">
<div class="row">
<div class="col-lg-6">
<div class="form-group">
<label for="Remarks">Remarks :</label>
<textarea name="remarks" rows="10" placeholder="Remarks">{{$addremarks->Remarks}}</textarea>
</div>
</div>
<input type="submit" name="submit" value="Update">
</div>
</div>
</form>
Route:
Route::post('dategraph','Chatbot\TrackerController#dategraph');
Controller:
public function dategraph(Request $request)
{
$dategraph = DiraStatistics::all()->whereBetween('date_access', [$from, $to])->get();
$dates = $dategraph('date_access');
return view('AltHr.Chatbot.graph', compact('dates'));
}
View:
<form id="form-project" role="form" action="{{action('AltHr\Chatbot\TrackerController#dategraph')}}" autocomplete="off" method="POST">
{{csrf_field()}}
<!-- <canvas id="myChart" width="150" height="50"></canvas> -->
<div class="form-group-attached">
<div class="row">
<div class="col-lg-6">
<div class="form-group form-group-default required" >
<label>From</label>
<input type="date" class="form-control" name="from" required>
</div>
</div>
<div class="col-lg-6">
<div class="form-group form-group-default required" >
<label>To</label>
<input type="date" class="form-control" name="to">
</div>
</div>
</div>
</div>
<button class="btn alt-btn-black btn-xs alt-btn pull-right" type="submit">Next</button>
</form>
Hi guys, so im trying to view the data from the selected dates as the code ive written. But im getting an error. Did i write it correctly? or am i missing something?
You do not have a $from variable.
You need to pull out posted variables from the request.
The method get() will return a Collection of objects. You can, for example, turn it to a flat array by plucking the column and turning it toArray()
$dategraph = DiraStatistics::whereBetween(
'date_access',
[
$request->get('from'),
$request->get('to')
]
)->get();
$dates = $dategraph->pluck('date_access')->toArray();
How do I make custom method to get form data? I want this method same with Laravel update method with parameters request and id. I try this but get error.
In controller
public function updatePassword(Request $request, int $id) {
dd($request->all());
}
In route
Route::post('staffs/{id}/upassword', 'Admin\StaffController#updatePassword')->name('admin.staffs.upassword');
In blade file
<form method="post" accept-charset="utf-8" action="{{ action('Admin\StaffController#updatePassword', ['id' => $staff_id]) }}">
<div class="row">
<div class="col-md-3">
<div class="form-group">
<label class="control-label" for="password">New Password</label>
<input class="form-control" name="password" type="password">
</div>
</div>
</div>
<div class="row">
<div class="col-md-3">
<div class="form-group">
<label class="control-label" for="password_confirmation">Confirm New Password</label>
<input class="form-control" name="password_confirmation" type="password">
</div>
</div>
</div>
<input class="btn btn-primary" type="submit">
</form>
I am using Laravel 5.4.
here are some stuff to fix:
First in the tag you can set the action to :
action="route('admin.staffs.upassword', $staff_id)" since it's
easier to write and since you already gave the route a name, so why
not using it ;)
Second add {{csrf_field() }} right before your form closing tag
</form>
what error are you getting? the error is probably because you are not using {{csrf_field()}} after the form declaration, it is needed so that laravel can validate the request. if you want to get the data from the form you can use:
$request->get('inputname');