Most efficient way to get a specific model from a collection in BLADE in Laravel - laravel

Hi im developing a site using Laravel and have some site data id like to keep track of such as facebook url, instagram url, address, phone number, etc. This information for this model is stored as a name and value in the DB, so example name:facebook and value:[facebook url here]. I know that i can retrieve all the models and get a collection, pass it to my view and loop through them, but id like a little more control to get specific models to use it where i need them on specific parts of the page. This is what I currently have it:
I have a model SiteMeta which i pass an instance to my view:
$site_meta = new \App\SiteMeta();
return view($page_string)->with('site_meta',$site_meta);
And in my SiteMeta Model:
public static function get($name)
{
return SiteMeta::where('name', '=', $name)->firstOrFail();
}
And then in my view im getting my specific model by its name:
<a target="_blank" href="{{$site_meta::get('Facebook')->value}}">
<i class="fa fa-facebook-official data-icon" aria-hidden="true"></i>
</a>
So my question is, is this the most efficient way to do it? Im doing this in various places on the contact page to display information like phone number and address and other site information which i feel like is a bit overload as it make a request to the database each time im doing sitemeta::get('name');
Is there a better way to get a specific one from the collection in BLADE? Thanks

I would pass it as an array from your controller method:
$site_meta = \App\SiteMeta::lists('value', 'name');
return view($page_string, compact('site_meta');
On your blade template you could do this:
$site_meta['Facebook'];

Related

how to display data based on specific id in laravel?

I am new to laravel. I want to send data as id of existing data. Id comes from products.blade I send via href tag as shown below to gallery page. I have tried to find a way through other sites but it still doesn't work
<a class="btn btn-success" href="/dashboard/galleries/{{ $product->id }}"><i class="ri-image-add-line text-white"></i></a>
then i create a route like this
Route::resource('/dashboard/galleries', DashboardGalleryController::class)->middleware('admin')->shallow();
in the controller gallery, I made like this
public function index($id)
{
$gallery = Gallery::where('products_id', $id)->get();
return view('dashboard.galleries.index', compact('gallery'));
}
then inside the gallery page, I want to display a table to add images based on that id.
dashboard/galleries/index.blade.php
<h1>{{ $gallery->id }}</h1>
should i add data inside foreach or outside?
A restful resource controller sets up some default routes for you and even names them.
and ur case the logic should be inside show() function not index()
check this issue it will help u same issue solved here

how to hide id from URL laravel 7?

i have this url :
http://127.0.0.1:8000/deliverer/4
i want it like this
http://127.0.0.1:8000/deliverer/
this is my function :
public function show($id)
{
// $userhash->hashids->encode($id);
$user=User::find($id);
return view('deliverer.profile')->with('user',$user);
}
and this is my route
Route::get('deliverer/{id}', 'deliverer\DelivererController#show')->name('profile');
and this in view
<a href="http://127.0.0.1:8000/deliverer/{{ Auth::user()->id }}" >
<i class="nc-icon nc-single-02"></i>
<p>Profile</p>
</a>
Assuming, that what you're trying to accomplish is to view the current authenticated user's profile, then you should follow the steps below:
First you have to modify the route and remove the {id} from the URL, like this:
Route::get('deliverer', 'deliverer\DelivererController#show')->name('profile');
Then, inside the controller you have to remove the $id param from the show() method and change the method to get the id from the authenticated user.
public function show($id)
{
// $userhash->hashids->encode($id);
$user = \Auth::user();
return view('deliverer.profile')->with('user',$user);
}
And of course, you have to remove the Auth::user()->id() from the view route, and perhaps use the named route instead of hardcoding it, like so:
<a href="{{ route('profile') }}">
<i class="nc-icon nc-single-02"></i>
<p>Profile</p>
</a>
I'm assuming you're just trying to hide id's so users can guess the next number or try to pull up records they shouldn't.
Have you looked into UUID's? Example here: (https://dev.to/wilburpowery/easily-use-uuids-in-laravel-45be) That might be a solution.
Also, if you are worried that someone might tamper with URL to pull records, you should look into securing up your models. Do a check to see if the user should have access to that particular record. Many ways to accomplish that.
You can use token or configure a middleware, or as you did you can hash or crypt the id and make the verification after the call
The url will be like that :
http://127.0.0.1:8000/deliverer/?id=aze45a8sd54q

Laravel - updateOrCreate or a sync method?

I'm trying to see what my best route should be but for the sake of ease I will make this sound much simpler than it is.
I have two models, one called donor and one called donation. For the relationships, the donor has many donations.
In my edit form of the donor model, I have the ability to create, update or delete the donations from the same form.
Updating and creating are easy at the moment because I can use updateOrCreate. But what happens if I want to delete a donation?
Would I perform an actual query filtering out the ids of the donations that were still on the edit form (and therefore not deleted by the user) and then delete the models from there? Or is there a better way of handling this action?
Thanks in advance.
In your DonationController you could select the donations you want to delete with a query like this:
public function destroy($id)
{
$donation = Donation::where('donor_id','=', $id)->get();
$donation->delete();
}
In your view you could make a delete form and them send him to this function in the controller. I think the best way to do it is make a foreach and loop the donations from the donor inside.
#foreach($donor->donations as $donation)
{
<form method="POST" action="{{route('donor.destroy', $donation->id)}}">
<button type="submit">Delete {{$donation->id}}</button>
</form>
}
#endforeach

How to pass value from one controller to two or more view in laravel 5.4

I m working on a e commerce site with laravel 5.4
Let's assume I have a ProductConttoller and inside a showSingle($id) function.
I want pass value to the view of single product view with product details and wanna also pass the product title to a partial view _meta.blade.php
For single product view I am passing product value like
return view('product.show')->with product($product);
Now how to pass title/any meta to a different view like meta.blade.php
Thank you
I'm guessing that you use the include function in your single product view.
So just do like that:
#include('_meta', ['title' => $product->title])
Or probably more easier, pass the entier product:
#include('_meta', ['product' => $product])
Then you can access any product properties from your _meta template.

Model validation on different request

I have several models all of which have a create page. When a model is created, I do not perform any validation. This is because I allow the user at any time to go back and add to things.
However, at some point, I provide a button to the user
<a href="{{ route('projects.push', $project->id) }}" class="btn btn-info pull-right" data-token="{{ csrf_token() }}">
Push
</a>
All of the models in question are related to the Project model. When they click the push button, I am going to send the Models to an external system. However, at this point I need to validate that the
models being sent have all the required data. I know about validation on a model, but this is when they are created. Is it possible to validate them on a completely different action?
Thanks
Of course it is possible. It would be smart to store your rules and/or messages inside you model as a static function. An example would be:
// Project model
public static function rules()
{
return [
'field1' => 'rules1..',
'field2' => 'rules2..'
];
}
Then you can retrieve your rules anywhere in your application:
Validator::make($fields, Project::rules());
One last thing. You said you validate your model when it already has been created. I don't know if putting the entire retrieved model variable instead of $fields will work. Example:
$project = Project::find($id);
// Try this
Validator::make($project, Model::rules());
// Otherwise try this
Validator::make($project->attributes, Model::rules());
Hope this helps :)

Resources