I'm trying to store a video file alongside some image and texts but i keep getting the error and i can't seem to get it.
This is the code:
public function store(HeaderFormRequest $request)
{
$validatedData = $request->validated();
if($request->hasFile('image')){
$file = $request->file('image');
$ext = $file->getClientOriginalExtension();
$filename = time() .'.'. $ext;
$file->move('uploads/header/', $filename);
$validatedData['image'] = "uploads/header/$filename";
}
if ($request->hasFile('video')){
$file = $request->file('video');
$ext = $file->getClientOriginalExtension();
$filename = time() .'.'. $ext;
$file->move('uploads/header/', $filename);
$validatedData['video'] = "uploads/header/$filename";
}
$validatedData['status'] = $request->status == true ? '1':'0';
Header::create([
'title' => $validatedData['title'],
'description' => $validatedData['description'],
'small_description' => $validatedData['small_description'],
'meta_description' => $validatedData['meta_description'],
'image' => $validatedData['image'],
'video' => $validatedData['video'],
'status' => $validatedData['status'],
]);
return redirect('admin/header')->with('message', 'Header added successsfully');
}
This is the code for the form request (HeaderFormRequest)
return [
'title' => [
'required',
'string',
'max:255',
],
'description' => [
'required',
'string',
'max:255',
],
'small_description' => [
'required',
'string',
'max:255',
],
'meta_description' => [
'required',
'string',
'max:255',
],
'image' => [
'nullable',
'image',
'mimes:jpg,jpeg,png',
],
'video' => [
'nullable',
'video',
'mimetypes:video/m4v,video/avi,video/flv,video/mp4,video/mov',
],
'status' => [
'nullable',
],
];
Related
Page loading correctly and every things work fine while bringing datas. Now I want to filter them but FilterColumn() method doesnt even work. When I tried filter() method it's working but then I won't get $keywords variable. I am missing something while querying leads ?
if (request()->ajax()) {
$leads = Lead::with('country','agent','detail')->orderBy('id','DESC');
$leads->where(function($q) use ($user){
if ($user->hasRole('agent') && !$user->hasRole('admin') && !$user->hasRole('lead')){ //agent isem
$q->where('agent_id',$user->id)
->orWhere('agent_id',null);
}
});
return DataTables::of($leads)->
addColumn('edit_button', function ($lead) {
$link = route('leads.edit',$lead->id);
return ' Edit ';
})->
filterColumn('edit_button', function ($query, $keyword) {
dd($keyword);
return $query->whereHas('patient', function ($query) use ($keyword) {
$query->whereRaw("CONCAT( name, ' ', surname ) like ?", ["%$keyword%"]);
});
})->rawColumns(['edit_button','phone','detail.conversion_notes'])->
toJson();
}
$tableColumn = [
['data' => 'edit_button', 'name' => 'edit_button', 'title' => 'Edit', 'searchable' => false],
['data' => 'full_name', 'name' => 'full_name', 'title' => 'Full Name'],
['data' => 'phone', 'name' => 'phone', 'title' => 'Phone'],
['data' => 'email', 'name' => 'email', 'title' => 'Email'],
['data' => 'country.name', 'name' => 'country.name', 'title' => 'Country'],
['data' => 'agent.name', 'name' => 'agent.name', 'title' => 'Agent'],
['data' => 'treatment', 'name' => 'treatment', 'title' => 'Treatment'],
['data' => 'find_us', 'name' => 'find_us', 'title' => 'Find Us'],
['data' => 'form_type', 'name' => 'form_type', 'title' => 'Form','visible' => false],
['data' => 'social_account', 'name' => 'social_account', 'title' => 'Sosyal Medya'],
['data' => 'created_at', 'name' => 'created_at', 'title' => 'Created At'],
['data' => 'detail.conversion_notes', 'name' => 'detail.conversion_notes', 'title' => 'Primary Notes'],
];
$html = $builder->columns($tableColumn)->parameters([
"pageLength" => 25,
"lengthMenu" => [[10, 25, 50, 100, -1], [10, 25, 50, 100, "All"]],
'dom' => 'Bfrtip',
'columnDefs' => [
['width' => '2%', 'targets' => 0],
['width' => '7%', 'targets' => 1],
'buttons' => [
'pageLength',
[
'extend' => 'colvis',
'collectionLayout' => 'fixed two-column',
'columns' => ':not(.noVis)'
]
]
]);
before updating i need to check if a particular value is empty. if its empty avoid updating that column
DrmCustomer::updateOrCreate([
'email'=> isset($row['email']) ? $row['email'] : "",
'user_id'=> $user_id,
],[
(isset($row['fieldMobilePhone']['value'])) ?: 'phone'=> $row['fieldMobilePhone']['value'],
(isset($row['fieldWebsite']['value'])) ?: 'website'=> $row['fieldWebsite']['value'],
(isset($row['fieldStreet1']['value'])) ?: 'address'=> $row['fieldStreet1']['value'],
(isset($row['fieldCity']['value'])) ?: 'city'=> $row['fieldCity']['value'],
(isset($row['fieldState']['value'])) ?: 'state'=> $row['fieldState']['value'],
(isset($row['fieldZip']['value'])) ?: 'zip_code'=> $row['fieldZip']['value'],
(isset($row['fieldCountry']['value'])) ?: 'country'=> $row['fieldCountry']['value'],
'default_language'=> 'DE',
'currency'=> 'EUR',
'insert_type'=> 'API'
]);
You can use Laravel Collection to filter your data by using following technique
<?php
$mapFields = [
'fieldMobilePhone' => 'phone',
'fieldWebsite' => 'website',
'fieldStreet1' => 'address',
'fieldCity' => 'city',
'fieldState' => 'state',
'fieldZip' => 'zip_code',
'fieldCountry' => 'country',
];
$validKeys = array_keys($mapFields);
$collection = collect($row);
$data = $collection->filter(function($value, $key) use($validKeys){
return Arr::get($value, 'value' false) && in_array($key, $validKeys);
})->flatMap(function($value, $key) use($mapFields){
return [
$mapFields[$key] => $value['value']
];
})->merge([
'default_language'=> 'DE',
'currency'=> 'EUR',
'insert_type'=> 'API'
])->all()->toArray();
DrmCustomer::updateOrCreate([
'email'=> Arr::get($row, 'email', ''),
'user_id'=> $user_id,
], $data);
I was able to do that like this:
DrmCustomer::updateOrCreate(['email' => isset($row['email']) ? $row['email'] : '', 'user_id' => $user_id], [
'default_language'=> 'DE',
'currency'=> 'EUR',
'insert_type'=> 'API'
] + (isset($row['fieldMobilePhone']['value']) ? [
'phone' => $row['fieldMobilePhone']['value']
] : []) + (isset($row['fieldWebsite']['value'])) ? [
'website' => $row['fieldWebsite']['value']
] : []) + (isset($row['fieldStreet1']['value']) ? [
'address' => $row['fieldStreet1']['value']
] : []) + (isset($row['fieldCity']['value']) ? [
'city' => $row['fieldCity']['value']
] : []) + (isset($row['fieldState']['value']) ? [
'state' => $row['fieldState']['value']
] : []) + (isset($row['fieldZip']['value']) ? [
'zip_code' => $row['fieldZip']['value']
] : []) + (isset($row['fieldCountry']['value']) ? [
'zip_code' => $row['fieldCountry']['value']
] : []));
I have two different Guzzle post requests that I am trying to merge (solely because they basically do a united job and should be performed together).
Initially I have my donation data:
'donation' => [
'web_id' => $donation->web_id,
'amount' => $donation->amount,
'type' => $donation->type,
'date' => $donation->date->format('Y-m-d'),
'collection_id' => NULL,
'status_id' => $donation->status_id,
],
And then I have my files that go with it, which are basically two different PDFs that are enabled or disabled for donors, sometimes they have both. I know the multipart would look something like below, but I'm not sure.
foreach ($uploadDocs as $doc) {
'multipart' => [
[
'name' => 'donation_id',
'contents' => $donation->web_id,
],
[
'name' => 'type_id',
'contents' => $doc->type_id',
],
[
'name' => 'file',
'contents' => fopen($doc->path, 'r'),
'headers' => ['Content-Type' => 'application/pdf'],
],
],
}
Since I've usually only handled one file at a time and I'm not sure how to merge the first block of code with the second for an appropriate Guzzle post request.
You can try this:
$donationData = [
'web_id' => $donation->web_id,
'amount' => $donation->amount,
'type' => $donation->type,
'date' => $donation->date->format('Y-m-d'),
'collection_id' => NULL,
'status_id' => $donation->status_id,
];
$multipart = [];
foreach ($uploadDocs as $doc) {
$multipart[] = [
[
'name' => 'donation_id',
'contents' => $donation->web_id,
],
[
'name' => 'type_id',
'contents' => $doc->type_id,
],
[
'name' => 'file',
'contents' => fopen($doc->path, 'r'),
'headers' => ['Content-Type' => 'application/pdf'],
],
];
}
Than perform your request:
$r = $client->request('POST', 'http://example.com', [
'body' => $donationData,
'multipart' => $multipart,
]);
In my form I have a couple checkboxes and i want at least one of them (or both) to be checked before submitting my form. My input checkboxes are named car and bike. In my controller i am validating my request
$this->validate($request, [
'content'=> 'required',
'title' => 'required',
'car | bike' => 'required', //this is not correct.
]);
What i couldn't figure is the statement that allows me to do the validation above
return Validator::make($request, [
'firstName' => 'required|max:255',
'lastName' => 'required|max:255',
'email' => 'required|email|max:255|unique:users',
'password' => 'required|confirmed|min:6',
'checkbox' =>'required_without_all',
]);
OR
return Validator::make($request, [
'firstName' => 'required|max:255',
'lastName' => 'required|max:255',
'email' => 'required|email|max:255|unique:users',
'password' => 'required|confirmed|min:6',
'option' =>'accepted'
]);
OR
Validator::make(
[ 'cats' => Input::get('cats') ],
[ 'cats' => 'min:1' ]
);
Examples:
$validator = Validator::make([
'cats' => ['Boots', 'Mittens', 'Snowball']
], ['cats' => 'min: 1']);
$result = $validator->fails(); // returns false
$validator = Validator::make([
'cats' => ['Boots', 'Mittens', 'Snowball']
], ['cats' => 'min: 2']);
$result = $validator->fails(); // returns false
$validator = Validator::make([
'cats' => ['Boots', 'Mittens', 'Snowball']
], ['cats' => 'min: 4']);
$result = $validator->fails(); // returns true
OR
Refer : https://laravel.com/docs/5.1/validation#available-validation-rules
I have an app with laravel in which, I have different users and different roles. I insert some users, roles, role_user, permission and permission_role in my DB using seeder but, when I try to login using email and password already recorded in DB, I got : These credentials do not match our records.
this is UserTableSeeder :
public function run()
{
//
$user = [
[
'name' => 'admin',
'email' => 'admin#mail.co',
'password' => 'passwordadmin'
],
[
'name' => 'president',
'email' => 'President#mail.co',
'password' => 'passwordpresident'
],
[
'name' => 'utilisateur1',
'email' => 'utilisateur1#mail.co',
'password' => 'passworduser'
],
[
'name' => 'utilisateur2',
'email' => 'utilisateur2#mail.co',
'password' => 'passworduser'
]
];
foreach ($user as $key => $value) {
User::create($value);
}
}
RoleTableSeeder :
public function run()
{
//
$role = [
[
'name' => 'admin',
'display_name' => 'Administrateur',
'description' => 'Administrateur du système'
],
[
'name' => 'president',
'display_name' => 'Président',
'description' => 'President de la commune'
],
[
'name' => 'utilisateur_normal',
'display_name' => 'membre du conseil',
'description' => 'membre du conseil'
]
];
foreach ($role as $key => $value) {
Role::create($value);
}
}
RoleUserTableSeeder :
public function run()
{
//
DB::table( 'role_user' )->insert([
[ 'user_id' => 6, 'role_id' => 4 ],
[ 'user_id' => 7, 'role_id' => 5 ],
[ 'user_id' => 8, 'role_id' => 6 ],
[ 'user_id' => 9 , 'role_id' => 6 ],
]);
}
Models I have : User, Role, Permission.
Any idea please ?
It looks you are not hashing your passwords into your database, you need to use bycript:
public function run()
{
//
$user = [
[
'name' => 'admin',
'email' => 'admin#mail.co',
'password' => bcrypt('passwordadmin')
],
[
'name' => 'president',
'email' => 'President#mail.co',
'password' => bcrypt('passwordpresident')
],
[
'name' => 'utilisateur1',
'email' => 'utilisateur1#mail.co',
'password' => bcrypt('passworduser')
],
[
'name' => 'utilisateur2',
'email' => 'utilisateur2#mail.co',
'password' => bcrypt('passworduser')
]
];
foreach ($user as $key => $value) {
User::create($value);
}
}
You can see more seeder examples on the oficial seeder Laravel documentation.
As Troyer said, you need to hash them with bcrypt. Laravel provides a handy wrapper with \Hash::make($str).
It is extremely bad practice to have your passwords in plaintext.
You need to run your seeder like this:
public function run()
{
$users = [
[
'name' => 'admin',
'email' => 'admin#mail.co',
'password' => \Hash::make('passwordadmin') // Hash them in order to make use of \Auth library (and more importantly, in order not to be a dick to your users)
],
[
'name' => 'president',
'email' => 'President#mail.co',
'password' => \Hash::make('passwordpresident')
],
[
'name' => 'utilisateur1',
'email' => 'utilisateur1#mail.co',
'password' => \Hash::make('passworduser')
],
[
'name' => 'utilisateur2',
'email' => 'utilisateur2#mail.co',
'password' => \Hash::make('passworduser')
]
];
User::create($users); // Import them with 1 query for performance
}