Laravel : sending data from one page to second to third to submit in DB - laravel

Need to send data from 1 -> 2 (Add more info) -> 3 (Preview & Submit).
First page user submit a url in an input
Second page user submit the url related information like Title, Description, Selects a category, add tags etc. All in inputs and textarea.
Third page for final view of submitted information so far and submit it into the DB.
But the sedon pae won't go to third page it gives POST error.
Controller
public function submitUrl(Request $request)
{
$validatedData = $request->validate([
'link' => 'required|unique:links|min:5|active_url',
]);
$categories = DB::table('categories')->get();
return view('submit2')->with('validatedData', $validatedData)->with('categories', $categories);
}
public function submitData(Request $request)
{
$validatedData = $request->validate([
'title' => 'required|between:15,150',
'category' => 'required',
'description' => 'required|between:250,500',
'tags' => ['required'],
]);
$categories = DB::table('categories')->get();
return $validatedData;
return view('submit3')->with('validatedData', $validatedData)->with('categories', $categories);
}
public function saveData(Request $request)
{
}
Route
Route::get('/submit', 'SubmitLinks#index')->middleware('auth');
Route::post('/submit2', 'SubmitLinks#submitUrl')->middleware('auth');
Route::post('/submit3', 'SubmitLinks#submitData')->middleware('auth');

Your first route is defined as:
Route::get('/submit', 'SubmitLinks#index')->middleware('auth');
and I believe you are using it to save first step data via POST. So change its method to post then like so:
Route::post('/submit', 'SubmitLinks#index')->middleware('auth');

Related

How to upload file in relationship hasOn<->belongsTo Laravel Backpack

Can be possible to store a file uploaded to a related table?
Scenario: I have a usres table in database and another one pictures. Users Model have the following function
public function picture()
{
return $this->hasOne(Picture::class);
}
And the Picture Model have the following function.
public function user_picture()
{
return $this->belongsTo(User::class, 'user_id', 'id');
}
Is possible to store the picture in pictures database table (id, user_id, img_path) from the UserCrudController store() function?
try something like this
public function store(Request $request)
{
Picture::create([
'user_id' => // get the user id from $request or auth()->user(),
'img_path' => $request->file('image')->store('images', 'public'),
]);
return // your view or something else
}
Let's say it is a registration form that need to insert an image. Instead of using the Picture model directly you can just do this :
public function store(Request $request)
{
$request->validate(...);
$user = User::create(...);
//It will ensure that the image belongs to the user.
$user->picture()->create([
'image_path' => $request->file('image')->store('images');
])
}
I resolved the issue with the following steps.
As per Laravel Backpack I added the input field in the Blade:
#include('crud::fields.upload', ['crud' => $crud, 'field' => ['name' => 'img1', 'label' => 'Image 1', 'type' => 'upload', 'upload'=> true, 'disk'=>'uploads', 'attributes' => ['id' => 'img1', 'capture' => 'user']]])
After this I added the function in the User Controller as follow:
$request->validate(['img1' => 'mimes:jpg,png,jpeg|max:5120']);
$fileModel = new Picture;
if($request->file()) {
$fileName1 = time().'_'.$request->img1->getClientOriginalName();
$filePath1 = $request->file('img1')->storeAs('uploads', $fileName1, 'public');
$fileModel->name = time().'_'.$request->img1->getClientOriginalName();
$fileModel->img1 = '/storage/' . $filePath1;
$fileModel->save();
}
With these lines of code I was able to store the related Picture with the User.
Thank you all for the guidelines.

Validating form comaring two fields values

I'm trying to find Laravel 8 documentation on how to validate comparing two fields to each other. I'm creating an app that allows creating matches from teams in a database table, using the create() method in the controller. I looked into Laravel #Validation, even #Custom Validation Rules, but I can't find anything when comparing the two fields.
public function store(Request $request)
{
$validatedData = $request->validate([
'local_team' => 'required',
'local_score' => 'required|numeric',
'visitor_team' => 'required',
'visitor_score' => 'required|numeric',
]);
$score = new Score();
$score->local_team = $request->local_team;
$score->local_score = $request->local_score;
$score->visitor_team = $request->visitor_team;
$score->visitor_score = $request->visitor_score;
$score->save();
$new = true;
return redirect()->route('scores.show',
['id' => $score->id, 'new' => true]);
}
In my case, the 'local_team' and 'visitor_team' fields should be different. Any clue on how to do it?

how to validate phone number and email address

i am new to laravel.. my problem is
i have a html form in which i am taking more than one phone number in a single input text tag using saprator ',' which is working fine.. but when i take that form data to controller i want to validate that each data should be a number and it should be of 10 digit only. same thing i want to do for the email also.
i have use laravel validation for the same but its not working... can anyone please help me on this.. thanx in advance: This is my controller store method code : Controller Code :
public function store(Request $request, Customer $newCustomer)
{
$mobileNo[] = explode(',',$request->contactNo);
$request->contactNo = $mobileNo;
$emails[] = explode(',',$request->email);
$request->email[] = $emails;
Validate::newCustomer($request);
}
Validation code :
public static function newCustomer(Request $request)
{
$request->validate([
'companyName' => 'required|string',
'city' => 'required|string',
'contactNo' => 'required|array',
'contactNo.*' => 'digits_between:10,10',
'email' => 'required|email',
'email.*' => 'email',
]);
}

Laravel: Automatically Create Slug from Title Using Create & Validate Methods

I want to automatically create a slug and save it to the database based on the title entered into a form. Currently, this is how my controller is set up:
public function store(News $id) {
News::create($this->validateArticle());
return redirect('/news');
}
public function validateArticle() {
return request()->validate([
'title' => 'required',
'excerpt' => 'nullable',
'body' => 'nullable'
]);
}
How can I modify this code so that I automatically generate a slug based off of the title?
Thanks.
This is another option of how to do it. Or you could use Observer to observe the crating method like so news->slug= Str::slug($request->title);
public function store(Request $request)
{
$news= new News();
$news->title= $request->title;
$news->slug= Str::slug($request->title);
$news->excerpt= $request->excerpt;
$news->body= $request->body;
$news->save();
return redirect('/news');
}
Make use you import Str use Illuminate\Support\Str;

Laravel send mail with multiple check box value

i'm trying to make inquiry form where costumer fill up form then check the value on the checkbox then once they submit form will send email to me listing all the information the customer selected, now problem is i want to change this[event_id,requirement_id] instead of id replace it with name those two id parameter is from my two model listed below.
Model:
Event:[id,name]
Requirement:[id,name]
Controller:
public function store(Request $request)
{
$summary=[
'name' => $request->fullname,
'email' => $request->email,
'company' => $request->company,
'event' => $request->event_id,
'requirement' => $request->requirement_id
];
return $summary;
Mail::send('emails.contact-message',[
],function($mail) use($summary){
$mail->from('myemail#gmail.com', 'tester');
$mail->to('myemail#gmail.com')->subject('Contact Message');
});
return redirect()->back();
}
This is the result of my return request:
{"name":"myname","email":"myemail#gmail.com","company":"mycompany","event":["1","2"],"requirement":["1","2"]}
As you can see the array Event has value of 1 and 2 i wanted to replace it with its name output should be [Wedding,Birthday] i'm sorry for my bad english hope you understand me..
Well, you'd need to pull the name from your models.
The following should do the trick:
$events = App\Event::whereIn('id', $request->event_id)
->get()
->pluck('name')
->toArray();
$requirements = App\Requirement::whereIn('id', $request->requirement_id)
->get()
->pluck('name')
->toArray();
Obviously, replace name in the above example with the actual name field in your models. This is just an example.
$events and $requirements will both be an array containing the names matching the ids you are supplying in your request.
You also need to change your $summary array as follows:
$summary = [
'name' => $request->fullname,
'email' => $request->email,
'company' => $request->company,
'event' => $events
'requirement' => $requirements
];

Resources