A client wants to have CRUD functionality for events on their website.
In the migration up() method, I currently have the following for the schema:
$table->string('date');
$table->string('location');
On the website, I have an RSVP button.
How would I make an additional column for the user to add a url to their facebook event in the href inside the button?
TL;DR:
What should I do to get this to insert into the href? :
$table->string('url');
If you already passing an object to the view, do this:
<a href="{{ $object->url }}">
The migration is just setting up your database. You still need to populate it with data.
$table->string('url');
That is fine for your migration, but if you want the user to be able to enter the URL, you'll have to add a form to the front-end view for the user to provide the URL.
Then on the backend, you'll need a route in your routes file that points to a controller that processes the form submission and inserts or updates the database record with a model object.
You'll also need to add the 'url' field to the $fillable array on whatever model your migration was for:
protected $fillable = [..., 'url'];
You could check out this tutorial which has a CRUD example from scratch for Laravel 5.4
Related
I want to retrieve data from the database and create the dynamic route.
For example, in Laravel web route file we write :
Route::get('/{slug}', function($slug){
return $slug;
});
So, I want to create this code after retrieving data from the database. and based on the data I can create the dynamic route with params or passing model data to the view.
And also how do I create dynamic view files. Like for different layout or from the database data.
Thanks in Advance.
in Laravel i want to do a page with a search box and a form (the route could be /products)
I want to retrieve information using search box typing id from a db and populate the form.
I request datas with the route for example /products/{id}
But in the controller i use the same function products($Request request) with if the id exists do something if no do other things, or there are two different functions?
Thx
Please go through the Laravel Resource Controllers.
To show list of products i.e. /products, create a index() method. To show a specific product i.e. /product/{id}, create show() method.
Probably is better use the same function, if id exist, return the page with the values filling the form, if not returns the same page but with a message showing that product doesn't exist
I have a table in the database called users and it has columns id, name, email
what I wanted to do is when I'm clicking it should send an email to a specific user recognized by the id
I have a table structure of user data named checkin.blade.php
enter image description here
I have the mail template too how can I work with the routes and the controller
Route::get('/send-email/{user}', 'YourController#sendEmail');
Send</button>
//in controller
public function sendEmail(User $user){
Mail::to($user->email)->send(new YourMailTemplate());
return back();
}
Your route can take advantage of route/model binding in Laravel. The button just calls that route with the users id (assuming you're generating stuff in a foreach loop with each user having its own row or div). In your controller, you just send the mail template of your choice after the method takes in the User model with its included email address as a parameter. You also want to make sure you're "using" the "Mail" and the "YourMailTemplate" in the top of the controller file:
use App\Mail\YourMailTemplate;
use Mail;
I am looking to access the Auth'd users' data throughout my views. This information needs to come from a DB query so I can join in various other tables to get the data I need.
The view structure I am working with is as follows: (layout->dashboard). "Layout" being the generic html bits, parent. and "dashboard" being the page specific content.
My first attempt at passing data from the controller to the view outlined that I was only able to access the variable from the child view (dashboard) and not (layout) which I did presume beforehand. My question is, what is the best way to pass around user data, from a DB query, to any view I need it in.
In this one scenario, it is using a peice of data to retrieve the users avatar in the nav bar, found in "layout".
Many Thanks,
Laravel provides a quick way to scaffold all of the routes and views you need for authentication using one simple command:
php artisan make:auth
to make auth interface.
Check Laravel Doc:authentication
And then, try below...
Query DB like this...
//Controller
$users = DB::table('users')->get();
return view('layout', compact('users'));
After get the users collection, then send to view Blade(in the html file) like this...
//`Layout.blade.php` as View file like this..
<div class="container">
#foreach($users as $user)
{{$user->name}}
{{$user->"any you want"}}
#endforeach
</div>
#displaying-pagination-results
Enjoy coding~!! :)
I'm struggling to work out how to validate a field in Model A that should be unique when combined with a field from Model B.
Here's an example to clarify my question:
Page hasMany SitesPage
SitesPage belongsTo Page, belongsTo Site
Page has a slug field which should be unique across a site. Pages can be attached to any site.
Page.id
Page.slug
SitesPage.id
SitesPage.site_id
SitesPage.page_id
I have a checkUniqueSlug() custom validation method in my Page model but can't validate the slug is unique to the site as the site_id is stored in SitesPage which isn't available in the Page model validate method ($this->data only contains Page model data).
I can't do the validation in the SitesPage model as SitesPage doesn't have a slug field and I can't see the Page post is SitesPage.
How do I create a custom validation to check the slug is unique to the site?
One solution is to move the slug into the SitesPage model but we need all shared pages to have the same slug. i.e. A shared "About Us" page must have an "about_us" slug irrespective of which site the page is attached to.
Another solution is to perform the validation in the controller before I save which would work but that feels wrong as the validation should be done in the model.
As there have been no answers and someone else might be looking for help, here's how I ended up with a solution:
As the controller is the only place where both model data is present in the data array, the call to validate has to be done there.
I offloaded the validation code to the model and called it with the following:
if (!empty($this->request->data)) {
if ($this->Model->specialMultiModelValidate($this->request->data) && $this->Model->save($this->request->data)) {
// model has validated and saved
}
else {
// model has failed to validate and save
}
}
Hope someone finds this useful.