Route [feeds.main] not defined with spatie/laravel-feed package - laravel

I use Laravel 8 with spatie/laravel-feed 4.0 package with this codes:
routes/web.php
Route::feeds();
config/feed.php
<?php
return [
'feeds' => [
'main' => [
/*
* Here you can specify which class and method will return
* the items that should appear in the feed. For example:
* [App\Model::class, 'getAllFeedItems']
*
* You can also pass an argument to that method. Note that their key must be the name of the parameter: *
* [App\Model::class, 'getAllFeedItems', 'parameterName' => 'argument']
*/
'items' => ['App\Models\Blog\Post', 'getFeedItems'],
/*
* The feed will be available on this url.
*/
'url' => '/feed',
'title' => 'News',
'description' => 'The description of the feed.',
'language' => 'en-US',
/*
* The image to display for the feed. For Atom feeds, this is displayed as
* a banner/logo; for RSS and JSON feeds, it's displayed as an icon.
* An empty value omits the image attribute from the feed.
*/
'image' => '',
/*
* The format of the feed. Acceptable values are 'rss', 'atom', or 'json'.
*/
'format' => 'atom',
/*
* The view that will render the feed.
*/
'view' => 'feed::atom',
/*
* The mime type to be used in the <link> tag. Set to an empty string to automatically
* determine the correct value.
*/
'type' => '',
/*
* The content type for the feed response. Set to an empty string to auatomatically
* determine the correct value.
*/
'contentType' => '',
],
],
];
app/Models/Blog/Post.php
class Post extends Model implements Feedable
{
// ...
public function toFeedItem(): FeedItem
{
return FeedItem::create([
'id' => $this->id,
'title' => $this->title,
'summary' => $this->summary,
'updated' => $this->updated_at,
'link' => $this->link,
'author' => $this->user->name,
]);
}
public static function getFeedItems()
{
return Post::orderBy('publish_date', 'desc')
->limit(10)
->get();
}
}
resources/views/layout.blade.php
<!DOCTYPE html>
<html lang="#yield('lang')">
<head>
<!-- ... -->
#include('feed::links')
</head>
<body>
<!-- ... -->
I tried to run these commands:
php artisan route:clear
php artisan optimize
...but noting changed.
I get this error message:
Route [feeds.main] not defined. (View:
/var/www/html/vendor/spatie/laravel-feed/resources/views/links.blade.php)
Any idea what I missed?

If you looking for immediate solution then you can remove
Route::feeds();
and add route like below
Route::get('feed',FeedController::class)->name("feeds.main");
and make sure name of url should match in config file
<?php
return [
'feeds' => [
'main' => [
/*
* Here you can specify which class and method will return
* the items that should appear in the feed. For example:
* [App\Model::class, 'getAllFeedItems']
*
* You can also pass an argument to that method. Note that their key must be the name of the parameter: *
* [App\Model::class, 'getAllFeedItems', 'parameterName' => 'argument']
*/
'items' => ['App\Models\Order', 'getFeedItems'],
/*
* The feed will be available on this url.
*/
'url' => '/feed',
'title' => 'My feed',
'description' => 'The description of the feed.',
'language' => 'en-US',
/*
* The image to display for the feed. For Atom feeds, this is displayed as
* a banner/logo; for RSS and JSON feeds, it's displayed as an icon.
* An empty value omits the image attribute from the feed.
*/
'image' => '',
/*
* The format of the feed. Acceptable values are 'rss', 'atom', or 'json'.
*/
'format' => 'atom',
/*
* The view that will render the feed.
*/
'view' => 'feed::atom',
/*
* The mime type to be used in the <link> tag. Set to an empty string to automatically
* determine the correct value.
*/
'type' => '',
/*
* The content type for the feed response. Set to an empty string to automatically
* determine the correct value.
*/
'contentType' => '',
],
],
];
and run following command
php artisan route:clear
php artisan optimize
Updates
Easy solution is if you specify url in config file then it wont throw error.For example in config/feed.php
'url' => '/feed',
so no need to change anythink

Related

How to set up spatie/laravel-feed content type to xml?

I use Laravel v8 with spatie/laravel-feed v4 with this codes:
routes/web.php
Route::get('feed', '\App\Models\Blog\Post#getFeedItems')->name("feeds.main");
config/feed.php
<?php
return [
'feeds' => [
'main' => [
/*
* Here you can specify which class and method will return
* the items that should appear in the feed. For example:
* [App\Model::class, 'getAllFeedItems']
*
* You can also pass an argument to that method. Note that their key must be the name of the parameter: *
* [App\Model::class, 'getAllFeedItems', 'parameterName' => 'argument']
*/
'items' => ['App\Models\Blog\Post', 'getFeedItems'],
/*
* The feed will be available on this url.
*/
'url' => '/feed',
'title' => 'News',
'description' => 'The description of the feed.',
'language' => 'hu-HU',
/*
* The image to display for the feed. For Atom feeds, this is displayed as
* a banner/logo; for RSS and JSON feeds, it's displayed as an icon.
* An empty value omits the image attribute from the feed.
*/
'image' => '',
/*
* The format of the feed. Acceptable values are 'rss', 'atom', or 'json'.
*/
'format' => 'atom',
/*
* The view that will render the feed.
*/
'view' => 'feed::atom',
/*
* The mime type to be used in the <link> tag. Set to an empty string to automatically
* determine the correct value.
*/
'type' => 'application/atom+xml',
/*
* The content type for the feed response. Set to an empty string to automatically
* determine the correct value.
*/
'contentType' => '',
],
],
];
The result in webpage's HTML code:
<link rel="alternate" type="application/atom+xml" href="http://mydomain.test/feed" title="News">
But if I open the link in browser the result contains JSON code.
How can I set up correctly / force the application/atom+xml content type?
The error is in route
Route::get('feed', '\App\Models\Blog\Post#getFeedItems')->name("feeds.main");
So it should be
Route::get('/feed',FeedController::class)->name("feeds.main");
or
Route::feeds();

Laravel Requests Validation rules only if value is not null?

I've created a request for my update method called CandidateProfileUpdateRequest.php:
public function authorize()
{
return true;
}
/**
* Get the validation rules that apply to the request.
*
* #return array
*/
public function rules()
{
return [
'photo' => ['mimes:jpeg,png,jpg,gif,bmp', 'max:4096'],
'video_one' => ['mimes:mp4,mov,ogg,qt', 'max:30720'],
'video_two' => ['mimes:mp4,mov,ogg,qt', 'max:30720'],
'video_three' => ['mimes:mp4,mov,ogg,qt', 'max:30720'],
'resume' => ['mimes:doc,docx,pdf', 'max:4096'],
'job_title' => ['required'],
];
}
public function messages()
{
return [
'photo.max' => 'The photo may not be greater than 4MB.',
'video_one.max' => 'The video may not be greater than 30MB.',
'video_two.max' => 'The video may not be greater than 30MB.',
'video_three.max' => 'The video may not be greater than 30MB.',
'resume.max' => 'The resume may not be greater than 4MB.',
];
}
For these 4 fields that aren't required photo, video_one, video_two, video_three,
I only want to apply these rules, if a file is being uploaded in either of these form fields.
So for example if video_two is empty i.e. the User isn't uploading anything here, and clicks Update, it shouldn't return any rules for video_two. Is this possible?
Check out the sometimes rule.
In some situations, you may wish to run validation checks against a field only if that field is present in the data being validated. To quickly accomplish this, add the sometimes rule to your rule list:
/**
* Get the validation rules that apply to the request.
*
* #return array
*/
public function rules()
{
return [
'photo' => ['mimes:jpeg,png,jpg,gif,bmp', 'max:4096'],
'video_one' => ['mimes:mp4,mov,ogg,qt', 'max:30720'],
'video_two' => ['sometimes', 'mimes:mp4,mov,ogg,qt', 'max:30720'],
// ^^^^^^^^^^^
'video_three' => ['mimes:mp4,mov,ogg,qt', 'max:30720'],
'resume' => ['mimes:doc,docx,pdf', 'max:4096'],
'job_title' => ['required'],
];
}
The sometimes rule didn't work. Thank you to lagbox, the nullable rule worked!

Call to a member function first() on null in laravel resource

i try to build a customized response in my resource like this:
class ApplicationResource extends JsonResource
{
/**
* Transform the resource into an array.
*
* #param \Illuminate\Http\Request $request
* #return array
*/
public function toArray($request)
{
return [
'id' => $this->id,
'sort'=> $this->sort,
'is_seen' => $this->is_seen,
'name' => $this->name,
'position' => $this->position,
'company' => $this->company,
'education' => $this->education,
'degree' => $this->degree,
'phone' => $this->phone,
'university' => $this->university,
'cv_folder_id' => $this->cv_folder_id,
'cv' => route('applications.cvShow', ['candidateCv' => $this->candidate_cv]),
'comments'=> ApplicationCommentsResource::collection($this->applicationComments),
'ratingFields'=> ApplicationRatingsResource::collection($this->applicationRatings()->get()),
'jobPostRatingFields' => JobPostRatingFieldsResource::collection($this->jobPost->jobPostRatingFields),
];
}
}
but i just get errors. the error i get is:
Call to a member function first() on null
i dont know how to build my response that if the collection is empty i dont get any error?
That simply means that you want to retrieve value that does not exist.
You can make simple condition like that:
if(is_null($this->sort)){
return "-";
}
Good luck!
I'm pretty sure the relationship is the problem.
But since there is not enough information, first of all find out in which line the error is, then check the relationships.
For example:
'comments'=> ApplicationCommentsResource::collection($this->applicationComments)
Model Application must have relationship applicationComments

Validating not all fields in form - Laravel

In laravel, I have created a form. At the moment, I am working on the validation of the input fields of this form. I ran into a problem when I tried to validate some input fields and others not. For example, mail should be validated but catering_name not (it isn't necessary to fill in this field, its an option)
I have tried all validation methods I could find. I keep getting the same error.
Method Illuminate\Validation\Validator::validatePhone does not exist.
I guess I am missing something.
I have tried:
Validator::make($request->...
$this->validate(request(), [ ...
$request->validate([ ...
Bellow, you will find all the data that should be inputted in the database.
If I remove the validation part, the data got inserted into the database. I think the problem lays with how I try to validate. Thanks for any help.
$this->validate(request(), [
'add_name' => 'required|min:3',
'add_mail' => 'required|email',
'name' => 'required|min:3',
'email' => 'required|email',
'telefone' => 'numeric|phone',
'gsm' => 'numeric|phone',
'event' => 'required|min:3',
'date_start' => 'required|date|after:tomorrow',
'date_end' => 'required|date|after_or_equal:event_date_start',
'location' => 'required|min:3',
'number' => 'required',
]);
$event = new Event;
$event->add_name = request('add_name');
$event->add_mail = request('add_mail');
$event->name = request('name');
$event->email = request('email');
$event->telefone = request('telefone');
$event->gsm = request('gsm');
$event->name = request('name');
$event->date_start = request('date_start');
$event->date_end = request('date_end');
$event->location = request('location');
$event->number = request('number');
$event->catering = request('catering');
$event->catering_name = request('catering_name');
$event->remarks = request('remarks');
$event->status = Event::STATUS_0;
$event->save();
Unfortunately phone is not one of the default validation. You can try something like:
[
'telefone' => 'required|regex:/(01)[0-9]{9}/',
]
You can see the available list of validations given by Laravel here.
There are a wide variety of more complex options depending on how important it is to you.
There are packages for easy plug and play like Laravel-Phone.
You can create your own custom validation using php artisan make:rule phone_number and then editing the new rule made:
namespace App\Rules;
use Illuminate\Contracts\Validation\Rule;
class PhoneNumber implements Rule
{
/**
* Determine if the validation rule passes.
*
* #param string $attribute
* #param mixed $value
* #return bool
*/
public function passes($attribute, $value)
{
// logic here, most likely some sort of regex.
}
/**
* Get the validation error message.
*
* #return string
*/
public function message()
{
return 'The :attribute must be a valid phone number.';
}
}

Validate when passing individual data using JSON.stringify

Before passing the data to Controller, the data is being added :
formData.push({"name":"channels","value":JSON.stringify(channels)});
Cause of this even when no data is present, its passed like
'channels' => '[]'
Now the issue is when I try to validate this in validator, I cannot use
'channels' =>'required',
'channels.*' =>'required|exists:channels,id',
How do validate the above data? Don't want to convert the format as its a working system. Any suggestions are appreciated. Thanks.
Updated for Request All Params:
'_token' => 'DjqgmNab0o3ifrVrSvHh6dM5vxLP7tZDc47pq05r',
'startdate' => '05 Sep 2018',
'years' => NULL,
'months' => NULL,
'enddate' => NULL,
'addChannel' => NULL,
'offerRuns' => 'UL',
'numberOfRuns' => NULL,
'limitPeriod' => 'FP',
'licenseAudioTrack' => '1',
'amount' => NULL,
'include_materials_costs' => '1',
'include_withholding_taxes' => '1',
'paymentTermsType' => 'US',
'termsAndConditionDescription' => NULL,
'document_s3_url' => NULL,
'file' => NULL,
'fileSize' => NULL,
'materialSpecificationDescription' => NULL,
'note' => NULL,
'countries' => '[]',
'platforms' => '["1","2","3","4","5","6","7","8","9"]',
'platforms-exclusive' => '[]',
'platforms-non-exclusive' => '[]',
'platforms-holdback' => '[]',
'channels' => '[]',
'languages' => '[["56","AL",1,"seller"]]',
'currencySelectedTerm' => 'EP',
'currencyId' => '1',
'paymentTerms' => '[]'
Check the present validation rule. It states:
present
The field under validation must be present in the input data but can
be empty.
Also look into sometimes rule:
In some situations, you may wish to run validation checks against a
field only if that field is present in the input array. To quickly
accomplish this, add the sometimes rule to your rule list
https://laravel.com/docs/5.7/validation#conditionally-adding-rules
As I understood channels is passed as JSON string & required validator is not working because it is not an empty string.
You can create a custom validator to validate empty JSON string & use it.
<?php
namespace App\Rules;
use Illuminate\Contracts\Validation\Rule;
class JsonRequired implements Rule
{
/**
* Determine if the validation rule passes.
*
* #param string $attribute
* #param mixed $value
* #return bool
*/
public function passes($attribute, $value)
{
return ! empty(json_decode($value, true));
}
/**
* Get the validation error message.
*
* #return string
*/
public function message()
{
return 'The :attribute is required.';
}
}
And use it as 'channels' =>'new JsonRequired'.
If you only need once throughout your application, you may use a Closure instead of a rule object.
Laravel custom validation
You can use json_decode for data first and then apply validations
public function store(Request $request)
{
$request_data = $request->all();
foreach($request_data as $key=>$value)
{
$request_data[$key] = json_decode($value);
}
// And then pass data in validator rules
$rules = [
// All rules here
];
$validator = Validator::make($request_data, $rules);
// other code
}

Resources