I have the error "InvalidArgumentException Trailing data " - php

Hi i tried everything i found on Google but nothing work, i have always the error trailing data when i'm trying to insert date in my db. i Hope someone can help me. Thanks a lot and sorry for my english.
//here's my model location
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Location extends Model
{
protected $table = 'location';
}
//Heres's My migration
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class Location extends Migration
{
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
Schema::create('location', function(Blueprint $table) {
$table->bigIncrements('id');
$table->date('start');
$table->date('end');
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::dropIfExists('location');
}
}
//And here's my method to store date in my controller
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Location;
use Carbon\Carbon ;
public function storeDate(Request $request){
$location = new Location();
$start = Carbon::createFromFormat('m/d/y', $request->input("start"));
$end = Carbon::createFromFormat('m/d/y', $request->input("end"));
$location->start = $start;
$location->end = $end;
$location->save();
return redirect('/myCar');
}
}
My form
<form class="form-inline" method="POST" action="/storedate">
#csrf
<div class="modal-body">
<label class="sr-only" for="inlineFormInputName2"></label>
<input type="text" class="form-control mb-2 mr-sm-2" id="inlineFormInputName2" placeholder="dd/mm/yyyy" value="20/04/2019" name="start">
<label class="sr-only" for="inlineFormInputGroupUsername2"></label>
<div class="input-group mb-2 mr-sm-2">
<input type="text" class="form-control" id="inlineFormInputGroupUsername2" placeholder="dd/mm/yyyy" value="20/04/2019" name="end">
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
<button type="submit" class="btn btn-primary">Send message</button>
</div>
</form>

It's ok i found it. In m/d/y my y need to be in uppercase :
m/d/Y
Thank you for your support !

Related

SQLSTATE[42S22]: Column not found: 1054 Unknown column 'male' in 'field list' laravel 9

to be honest idk what's wrong here in my code but i still receive this error i see all the solution here but nothing happen , is the problem in the type="radio"? i try to change it to type="checkbox" but it still appears an error """ datatype of checkbox-> boolean
my blade file : form ->
<form action="{{url('reserveinfo')}}" method="POST" enctype="multipart/form-data" >
#csrf
<!-- ******** input user data to reserve ******** -->
<div class="MaleFemale">
<div class="Male">
<input type="radio" name="male" id="ML">
<label for="ML">Male</label>
</div>
<div class="Female">
<input type="radio" name="female" id="FML">
<label for="FML">Female</label>
</div>
</div>
<div class="FullName">
<input type="text" name="fullname" placeholder="Full name">
</div>
<div class="PatientEmail">
<input type="email" name="email" placeholder="Email">
</div>
<div class="TestBirthday">
<div class="Test">
<p>Test PCR</p>
<div>
<label for="TSTFILE">
<img src="../Resources/File.png" for="TSTFILE">
Add file</label>
<input type="file" name="testpcr" id="TSTFILE">
</div>
</div>
<div class="Birthday">
<label for="Birthday">Birthday</label>
<input type="date" name="date">
</div>
</div>
<div class="DoctopPres">
<p>Doctor prescription</p>
<div>
<label for="DctrPres">
<img src="../Resources/File.png" for="DctrPres">
Add file</label>
<input type="file" name="doctorpres" id="DctrPres">
</div>
</div>
<!--########## Send request ########## -->
<div class="Reserve">
<a href="../HtmlFiles/Reserve-3-.html">
<button>Reserve</button>
</a>
</div>
Controllers code :
public function reserveinfo(Request $request){
$data = new reserveinfo;
$data->male=$request->Male;
$data->female=$request->female;
$data->fullname=$request->fullname;
$data->email=$request->email;
$data->birthday=$request->date;
$test_pcr=$request->file('testpcr');
$imagename=time().'.'.$test_pcr->getClientoriginalExtension();
$request->testpcr->move('Text-PCR-of-users',$imagename);
$data->$test_pcr=$imagename;
$Doctor_prescription=$request->file('doctorpres');
$imagename2=time().'.'.$Doctor_prescription->getClientoriginalExtension();
$request->doctorpres->move('Doctor-prescription-of-users',$imagename2);
$data->doctorpres=$imagename2;
if(Auth::id()){
$data->user_id=Auth::user()->id;
}
$data->save();
return redirect()->back();
}
routes :
Route::get('/reserve1_view',[UserController::class,'addview']);
Route::get('/reserve2_view',[UserController::class,'addview2']);
Route::post('/reserveinfo',[UserController::class,'reserveinfo']);
migrations code :
public function up()
{
Schema::create('reserve2s', function (Blueprint $table) {
$table->id();
$table->boolean('male')->nullable();
$table->boolean('female')->nullable();
$table->string('fullname')->nullable();
$table->string('email')->nullable();
$table->string('test_pcr')->nullable();
$table->string('birthday')->nullable();
$table->string('Doctor_prescription')->nullable();
$table->string('user_id')->nullable();
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::dropIfExists('reserve2s');
}
};
and get this error :
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'male' in 'field list
models code :
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class reserveinfo extends Model
{
use HasFactory;
}
""" nothing add to models """
thanks for reading this XD
Try this:
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class reserveinfo extends Model
{
use HasFactory;
protected $table = 'reserve2s';
}
I recommend you read the Laravel manual about Eloquent's naming convention.
Eloquent Model Conventions
In your second error you are using the value of the variable $test_pcr as the attribute name. Just remove the $.
// Change this
$data->$test_pcr=$imagename;
// to
$data->test_pcr=$imagename;
// Change this
$data->doctorpres=$imagename2;
// to
$data->Doctor_prescription=$imagename2;
Remembering that you should read about Eloquent to understand how this integration works with the model and the database table.

Error 500 while trying to upload an image using FilePond and Laravel Spatie

I'm trying to create items that have a picture/avatar using a form and insert them into my database, so I decided to use Laravel Spatie with the MediaCollection, but as soon as i implemented the library, I'm getting an error 500 when I submit my form.... any idea why? And how am I supposed to create my database fields so when I create a new item with a picture I submit, the picture is submitted in a "Picture" datatable and in my "item" datatable I have the foreign key "picture_id" referencing to the picture in the Picture datatable.
Item.php:
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Spatie\MediaLibrary\HasMedia;
use Spatie\MediaLibrary\InteractsWithMedia;
class Item extends Model implements HasMedia
{
use HasFactory;
use InteractsWithMedia;
public function inventory_list()
{
return $this->belongsTo(InventoryList::class);
}
public function work_ticket()
{
return $this->belongsToMany(WorkTicket::class);
}
public function picture()
{
return $this->belongsTo(Picture::class);
}
}
ItemController.php:
<?php
namespace App\Http\Controllers;
use App\Models\InventoryList;
use App\Models\Item;
use App\Models\Picture;
use App\Models\TemporaryFile;
use Illuminate\Http\Request;
class ItemController extends Controller
{
public function index()
{
$inventory_lists = InventoryList::all();
$pictures = Picture::all();
return view('add-item', ['inventory_lists' => $inventory_lists,
'pictures' => $pictures]);
}
public function store(Request $request)
{
$item = new Item;
$item->name = $request->name;
$item->state = $request->state;
$item->observations = $request->observations;
$item->list_id = $request->list_id;
$item->picture_id = $request->picture_id;
$temporaryFile = TemporaryFile::where('folder', $request->avatar)->first();
if($temporaryFile){
$item->addMedia(storage_path('app/public/avatars/tmp/' . $request->avatar . '/' . $temporaryFile->filename))
->toMediaCollection('avatars');
rmdir('app/public/avatars/tmp/' . $request->avatar);
$temporaryFile->delete();
}
$item->save();
return redirect('add-item')->with('status', 'Item Form Data Has Been inserted');
}
}
add-item.blade.php:
<!DOCTYPE html>
<html>
<head>
<title>Créer un nouvel item</title>
<meta name="csrf-token" content="{{ csrf_token() }}">
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">
<link href="https://unpkg.com/filepond/dist/filepond.css" rel="stylesheet">
</head>
<body>
<div class="container mt-4">
#if(session('status'))
<div class="alert alert-success">
{{ session('status') }}
</div>
#endif
<div class="card">
<div class="card-header text-center font-weight-bold">
Ajouter un nouvel item
</div>
<div class="card-body">
<form name="add-blog-post-form" id="add-blog-post-form" method="post" action="{{url('store-item')}}">
#csrf
<div class="form-group">
<label for="name">Nom</label>
<input type="text" id="name" name="name" class="form-control" required="">
</div>
<div class="form-group">
<label for="state">État</label>
<input type="text" id="state" name="state" class="form-control" required="">
</div>
<div class="form-group">
<label for="observations">Observations</label>
<textarea name="observations" class="form-control" required=""></textarea>
</div>
<div class="form-group">
Liste d'inventaire
<select name="list_id" class="form-control select2-multiple">
<option value=""></option>
#foreach($inventory_lists as $inventory_list)
<option value="{{$inventory_list->id}}">
{{$inventory_list->name}}
</option>
#endforeach
</select>
</div>
<div>
<label for="avatar">Photo</label>
<input type="file" name="avatar" id="avatar">
</div>
<button type="submit" class="btn btn-primary">Submit</button>
</form>
</div>
</div>
#section('scripts')
<script>
const inputElement = document.querySelector('input[id="avatar"]');
const pond = FilePond.create( inputElement );
FilePond.setOptions({
server: {
url: '/upload',
headers: {
'X-CSRF-TOKEN': '{{ csrf_token() }}'
}
}
});
</script>
#endsection
</div>
<script src="https://unpkg.com/filepond/dist/filepond.js"></script>
#yield('scripts')
</body>
</html>
UploadController.php
<?php
namespace App\Http\Controllers;
use App\Models\TemporaryFile;
use Illuminate\Http\Request;
class UploadController extends Controller
{
public function store(Request $request)
{
if ($request->hasFile('avatar')) {
$file = $request->file('avatar');
$filename = $file->getClientOriginalName();
$folder = uniqid() . '-' .now()->timestamp;
$file->storeAs('avatars/tmp/' . $folder, $filename);
TemporaryFile::create([
'folder' => $folder,
'filename' => $filename
]);
return $folder;
}
return '';
}
}
2022_05_06_063218_create_temporary_files_table.php:
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
Schema::create('temporary_files', function (Blueprint $table) {
$table->id();
$table->string('folder');
$table->string('filename');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::dropIfExists('temporary_files');
}
};
Thanks in advance
Edit:
I got this error right before getting my Error 500, then I simply refreshed my page and I never got the following error again, just an error 500.
local.ERROR: Declaration of
Spatie\MediaLibrary\MediaCollections\Models\Collections\MediaCollection::jsonSerialize()
must be compatible with
Illuminate\Support\Collection::jsonSerialize(): array
{"exception":"[object]
(Symfony\Component\ErrorHandler\Error\FatalError(code: 0):
Declaration of
Spatie\MediaLibrary\MediaCollections\Models\Collections\MediaCollection::jsonSerialize()
must be compatible with
Illuminate\Support\Collection::jsonSerialize(): array at
/home/vagrant/code/vendor/spatie/laravel-medialibrary/src/MediaCollections/Models/Collections/MediaCollection.php:51)
[stacktrace]
#0 {main}

Illuminate\Contracts\Container\BindingResolutionException Target class [App\Http\Controllers\FileController\FileController] does not exist

This is my web.php file and i have used the correct get and post methods and have specified the path correctly
<?php
use Illuminate\Support\Facades\Route;
namespace App\Http\Controllers\FileController;
use Illuminate\Support\Facades\Route;
Route::get('/', function () {
return view('welcome');
});
Route::post('store', [FileController::class,'store']);
Route::view('imgupload','imageUpload');
blade file--imageUplods;where i get like 2 images and store it as a multiple entry in the db
<form method="post" action="store" enctype="multipart/form-data">
#csrf
<div class="input-group realprocode control-group lst increment" >
<input type="file" name="filenames" class="myfrm form-control" id="filenames">
<div class="input-group-btn">
<button class="btn btn-success" type="button"> <i class="fldemo glyphicon glyphicon-plus">
</i>Add</button>
</div>
</div>
<div class="clone hide">
<div class="realprocode control-group lst input-group"
style="margin-top:10px">
<input type="file" name="filenames" class="myfrm form-control"
id="filenames">
<div class="input-group-btn">
<button class="btn btn-danger" type="button"><i class="fldemo
glyphicon glyphicon-remove">
</i> Remove</button>
</div>
</div>
</div>
<button type="submit" class="btn btn-success" style="margin-
top:10px">Submit</button>
</form>
</div>
model: filemodel - filesnames is the only attribute to enter
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class file extends Model
{
use HasFactory;
protected $fillable = [
'filenames'
];
public function setFilenamesAttribute($value)
{
$this->attributes['filenames'] = json_encode($value);
}
}
Controller-FileController- imageUploder is the blade file
<?php
namespace App\Http\Controllers;
use Illuminate\Support\Facades\DB;
use Illuminate\Http\Request;
use App\Models\File;
use Illuminate\Support\Facades\Auth;
class FileController extends Controller
{
/**
* Show the application dashboard.
*
* #return \Illuminate\Http\Response
*/
public function create()
{
return view('imageUpload');
}
/**
* Show the application dashboard.
*
* #return \Illuminate\Http\Response
*/
public function store(Request $request)
{
$this->validate($request, [
'filenames' => 'required',
'filenames.*' => 'image'
]);
$files = [];
if($request->hasfile('filenames'))
{
foreach($request->file('filenames') as $file)
{
$name = time().rand(1,100).'.'.$file->extension();
$file->move(public_path('files'), $name);
$files[] = $name;
}
}
$file= new File();
$file->filenames = $files;
$file->save();
return redirect('imageUpload')->with('success', 'Your images has
been successfully added');
}
}
You are mixing up 'use' and 'namespace'. 'namespace' is for defining the namespace of the current class
Change this line
namespace App\Http\Controllers\FileController;
to
use App\Http\Controllers\FileController;
Change what Gert said about the namespace in web.php
Change
class file extends Model
to
class File extends Model

Laravel 5.7 Auth::check

I am working on a login system for two users - admin and customer. For the admins, I've used the default auth files, but created new ones for the customer. My question is that the auth::check for admins works, but it doesn't for customers. So it doesn't query the database and logs in any email and any password. How can I fix this?
customerlogin.php
#extends('layouts.app')
#section('title','CustomerLogin')
#push('css')
#endpush
#section('content')
<div class="content">
<div class="container-fluid">
<div class="row">
<div class="col-md-8 col-md-offset-1">
#include('layouts.partial.msg')
<div class="card">
<div class="card-header" data-background-color="purple">
<h4 class="title">Customer Login</h4>
</div>
<div class="card-content">
<form method="POST" action="{{ route('customerLogin') }}">
#csrf
<div class="row">
<div class="col-md-12">
<div class="form-group label-floating">
<label class="control-label">Email</label>
<input type="email" class="form-control" name="email" value="{{ old('email') }}" required>
</div>
</div>
</div>
<div class="row">
<div class="col-md-12">
<div class="form-group label-floating">
<label class="control-label">Password</label>
<input type="password" class="form-control" name="password" required>
</div>
</div>
</div>
<button type="submit" class="btn btn-primary">Login</button>
Back
</form>
</div>
</div>
</div>
</div>
</div>
</div>
#endsection
#push('scripts')
#endpush
CustomerLogController
<?php
namespace App\Http\Controllers;
use App\Category;
use App\Item;
use App\Slider;
use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\AutenticatesUser;
use Illuminate\Support\Facades\Hash;
use Auth;
use Redirect;
use Session;
use Validator;
use Illuminate\Support\Facades\Input;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
class CustomerLogController extends Controller
{
use AutenticatesUser;
/**
* Where to redirect users after login.
*
* #var string
*/
protected function authenticated() {
if($customers = Auth::customers()){
return redirect()->route('homepage');
}
}
/**
* Create a new controller instance.
*
* #return void
*/
public function __construct()
{
$this->middleware('guest')->except('logout');
}
/**
* Create a new controller instance.
*
* #return void
*/
/**
* Show the application dashboard.
*
* #return \Illuminate\Http\Response
*/
public function index()
{
$sliders = Slider::all();
$categories = Category::all();
$items = Item::all();
return view('homepage',compact('sliders','categories','items'));
}
public function logout(Request $request) {
$this->guard()->logout();
$request->session()->invalidate();
return redirect('/');
}
}
web.php
<?php
/*
|--------------------------------------------------------------------------
| 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::redirect('lara-admin','login');
Route::get('/','HomeController#index')->name('welcome');
Route::get ('/homepage', 'HomepageController#index')->name('homepage');
Route::post('/reservation','ReservationController#reserve')->name('reservation.reserve');
Route::post('/contact','ContactController#sendMessage')->name('contact.send');
Auth::routes();
Route::resource ('customerLogin', 'CustomerLoginController');
Route::post('homepage', 'CustomerLogController#index')->name('customerLogin');
Route::get('/logout', 'CustomerLogController#logout');
Route::group(['prefix'=>'admin','middleware'=>'auth','namespace'=>'Admin'], function (){
Route::get('dashboard', 'DashboardController#index')->name('admin.dashboard');
Route::resource('slider','SliderController');
Route::resource('category','CategoryController');
Route::resource('item','ItemController');
Route::get('reservation','ReservationController#index')->name('reservation.index');
Route::post('reservation/{id}','ReservationController#status')->name('reservation.status');
Route::delete('reservation/{id}','ReservationController#destory')->name('reservation.destory');
Route::get('contact','ContactController#index')->name('contact.index');
Route::get('contact/{id}','ContactController#show')->name('contact.show');
Route::delete('contact/{id}','ContactController#destroy')->name('contact.destroy');
});
Any help would be much appreciated!
In an older version of Laravel, when we created new auth files, we had the same issue as you're having - that Auth::Check() did not work. At all. Could not work out why.
We ended up manually checking the login, ensuring that it was correct, by using Hash::check() and then processing the login using Auth::LoginUsingId().
if(Hash::check($password, $user->u_pass)){
Auth::LoginUsingId($user->id);
return redirect()->intended('/');
}
At this point, all the Auth:: functions worked as intended.
It was a hacky solution, but it did work.

OCTOBERCMS Form and Model

I'm a newbie in OctoberCms and i don't have much knowledge in Laravel also. While self studying I face a request like this it's a Select if record exist query I need to read the database and look for the match and I'm really confuse.
This is my form in form.htm where I design my Form.
use Drufal\DynamicContentManager\Models\MembersVerification;
==
<form data-request="onSend" accept-charset="UTF8" enctype="multipart/form-data">
<div class="form-group">
<label>First Name:</label>
<input type="text" class="form-control" name="first_name" required>
</div>
<div class="form-group">
<label>Middle Name:</label>
<input type="text" class="form-control" name="middle_name">
</div>
<div class="form-group">
<label>Last Name:</label>
<input type="text" class="form-control" name="last_name" required>
</div>
<div class="form-group">
<button type="submit" class="btn btn-primary" >Submit</button>
</div>
</form>
and this my model
<?php namespace Drufal\DynamicContentManager\Models;
use Model;
use Input;
/**
* Model
*/
class MembersVerification extends Model
{
use \October\Rain\Database\Traits\Validation;
/*
* Disable timestamps by default.
* Remove this line if timestamps are defined in the database table.
*/
public $timestamps = false;
/**
* #var array Validation rules
*/
public $rules = [
];
/**
* #var string The database table used by the model.
*/
public $table = 'drufal_dynamiccontentmanager_members';
public function onSend(){
$fn = Input::get('first_name');
$mn = Input::get('middle_name');
$ln = Input::get('last_name');
$membertbl=$table::where('first_name', '=', $fn)->first();
if ($membertbl === null) {
echo"
<script>
alert('Successfully');
</script>
";
}else{
echo"NO RESULT";
}
}
}
Help the newbie please.
I think you missed the DB:: in your database request:
$users = Db::table('users')->where('votes', 100)->first();
Maybe this documentation will help you:
https://octobercms.com/docs/database/query#where-clauses

Categories