Losing the css properties - Laravel - laravel

I have a menu with vocals that redirect to the same page but showing different data thanks to the id passed through the anchor. I can print the ID in the blade but unfortunately I lose all the properties of the css. Why?
Menù in app.blade.php:
<li>Articoli
<ul class="dropdown">
<li>Persone</li>
<li>Business</li>
<li>Mindset</li>
</ul>
</li>
Route in web.php:
Route::get('/articoli/{articolo}', 'ArticoloController#show');
Controller:
public function show($id){
$articolo = Articolo::find($id);
return view('articoli',['articolo'=>$articolo]);
}
Erros in the console:
Failed to load resource: the server responded with a status of 404 (Not Found)

I am not sure if this will solve your issue, but from V 5.2 I believe, Laravel binds models to routes automaticly. That is if the route's parameter name matches the model name in lowercase. In your case '{articoli}'
https://laravel.com/docs/5.2/routing#route-model-binding
This could mean that your controller function is receiving a Eloquent Model instead of an ID. Typehinting like this avoids misunderstandings:
public function show(Articoli $articoli)
Also you won't have to find the model anymore. If you typehint a Model like so, and no model was bound to the route, a new Model will be inserted into the function. you can also try typehinting (int $id), but honestly I don't know why you wouldn't use this awesome feature

It's important to include files js and css with the correct syntax.
Like this:
<script src="{{ asset('js/jquery.js') }}"></script>
And not:
<script src="js/jquery.js"></script>

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 can I pass a variable from slot to its layout in Laravel?

I am using Laravel 8 and I can't find a way to solve this problem.
I have a app-layout like this:
<!-- ./app-layout.blade.php -->
<html>
<head>...</head>
<body>
<livewire:create-suggestion />
{{ $slot }}
</body>
</html>
And this view uses the app-layout:
<!-- ./index.blade.php -->
<x-app-layout>
...
</x-app-layout>
In controller, I pass a variable to index view:
public function index() {
return view('index', ['variable', '$variable']);
}
How can I pass this variable to the app-layout? Because I also want to
use this in the create-suggestion livewire component.
Try this. edit your index.blade.php
#extends('app-layout', ['variable' => $variable])
...
You can pass any variable attribute to the layout like so:
<x-layout :myvariable=$myvariable>
or static attribute values like so
<x-layout myvariable="This is a string">
and retrieve it in the layout.blade.php like so:
{{ $attributes['myvariable'] }}
First off I beleive the App Layout will have access to whatever is passed by the view function. But if I am wrong or you wish to pass any custom variable whatsoever using the Layout syntaxt as opposed to template inheritance the way would be: (I came cross your question having a problem passing a static string for my HTML page title and no solution worked until I found this)
<x-app-layout>
<x-slot:variable>{{$variable}}</x-slot>
</x-app-layout>
However your issue is you are using Livewire and livewire is not simply just a view! Laravel Intentionally makes Livewire independent from the rest of the page. You would have to either pass it in PHP as param during mount which will only be accessible if set during the mount function() in \App\HTTP\LiveWire\createSuggestion.php
#livewire("create-suggestion",[ $variable])
Or ideally you have your javascript use the $emit feature to pass the element to the livewire listener all handled via Livewire. It seems to me you are using Livewire for something that you should simply only use Blade/Views:
#include("create-suggestion");

Route keep asking for parameter even if not set to receive any

I'm new to laravel and I'm having trouble with one of my web routes...
I grouped some routes with the auth middleware
Route::middleware(['auth:web'])->group(function ($router) {
Route::get('/news', [NewsController::class, 'news'])->name('news');
Route::get('/profile/{user_id}', [ProfileController::class, 'profile'])->name('profile');
Route::get('/marketplace', [MarketplaceController::class, 'marketplace'])->name('marketplace');
});
Before this issue, I have set a user_id parameter for the news route and then removed it the next day, now the news route keeps returning error saying it needs user_id.
here's my controller:
<?php
namespace App\Http\Controllers;
class NewsController extends Controller
{
public function news() {
return view('modules.news.news');
}
}
and since im using laravel-vue mix, my modules.news.news view looks like this:
#section('content')
<newsfeed inline-template>
<div>
...
...
</div>
</newsfeed>
#endsection
I've checked the web route, the controller, the blade, and I really can't see anything that requiring the news route to receive a parameter.
can someone point out where I went wrong?
Your route file might be cached, try running php artisan route:clear and see if this fixes the issue.

Image not displaying in show blade but displaying in index blade

Please I need your help. So am trying to display an image in my view, it quite working on the index blade but not working on the show with the same code.
Here is my code
<img src="{{asset('/blog_images/'.$post->photo )}}" style="width:300px; height:150px" alt="NO IMAGE">
I've even gone ahead to try other helpers like public_path, url.
Please note that i inspected on the browser and it returned the full part to the image yet didn't display.
I'm thinking it might have to do with a certain settings somewhere.
Cheers!
What is in your Controller show method?)
It must be somthing like this:
public function show($id)
{
$post = Post::find($id);
return view('post.show', compact('post'));
}

Laravel Sub-menu Within View

Hi I am very new to Laravel and MVC frameworks in general and am looking to create a list of links (in a view within a template) that links to some content. I am using this to display a list of nine people and to display their profile description when the link is clicked on. I have created a model of what the page looks like at http://i.imgur.com/8XhI2Ba.png. The portion that I am concerned with is in blue. Is there a way to route these links to something like /about/link2 or /about?link2 while maintaining the same exact page structure but modifying the ‘link content’ section (on the right of the link menu) to show the specific link's content? I would greatly appreciate it if someone could point me in the right direction, as I have literally no clue where to go with this!
There are a couple ways you can go about doing this.
Templates
Create your route.
Im assuming a lot about your app here but hopefully you get the picture. If you need help with anything in particular, be sure to update your question with the code youve tried so it will be easier to help you.
Route::get('about/{page}', function($page)
{
$profile = Profile::where('name', $page)->first();
return View::make('about')->with('profile', $profile);
});
Modify Template.blade.php
Put this line where you wish for About.blade.php to appear.
#yield('content')
Create your view which will extend your template
#extends('Template')
#section('content')
<h2>User Profile</h2>
<ul>
<li>Name: {{ $profile->name }}</li>
<li>Last Updated: {{ $profile->updated_at }}</li>
</ul>
#stop
AJAX
This solution will utilize AJAX to grab the data from the server and output it on the page.
Route for initial page view
Route::get('about', function($page)
{
$profiles = Profile::all();
return View::make('about')->with('profiles', $profiles);
});
Feel free to follow the same templating structure as before but this time we need to add some javascript into the template to handle the AJAX. Will also need to id everything which needs to be dynamically set so we can easily set it with jquery.
#extends('Template')
#section('content')
<h2>Links</h2>
#foreach($profiles as $profile)
{{ $profile->name }}
#endforeach
<h2>User Profile</h2>
<ul>
<li>Name: <span id="profile_name">{{ $profile->name }}</span></li>
<li>Last Updated: <span id="profile_updated_at">{{ $profile->updated_at }}</span></li>
</ul>
<script>
function setProfile(a)
{
$.ajax({
method: 'get',
url: 'getProfile',
dataType: 'json',
data: {
profile: $(a).data('id')
},
success: function(profile) {
$('#profile_name').html(profile.name);
$('#profile_updated_at').html(profile.updated_at);
},
error: function() {
alert('Error loading data.');
}
});
}
</script>
#stop
Route to handle the AJAX request
Route::get('getProfile', function()
{
$profile_id = Input::get('profile');
$profile = Profile::find($profile_id);
return $profile->toJson();
});
Now, the page should not have to reload and only the profile information should be updated.
Making some assumptions here as no code posted and assuming you're using the latest version of Laravel, Laravel 5.
Lets say you have a table in your database named users and you have a Model named Users (Laravel 5 comes with the Users model as default, see app/Users.php). The users will be the base of our data for the links.
Firstly, you want to register a route so you can access the page to view some information. You can do this in the routes file. The routes file can be found here: app/Http/routes.php.
To register a route add the following code:
Route::get('users', ['uses' => 'UserController#index']);
Now what this route does is whenever we hit the URL http://your-app-name/public/users (URL might be different depending on how you have your app set up, i.e. you may not have to include public) in our web browser it will respond by running the index method on the UserController.
To respond to that route you can set up your UserController as so:
<?php namespace App\Http\Controllers;
class UserController extends Controller {
public function index()
{
}
}
Controllers should be stored in app/Http/Controllers/.
Now lets flesh out the index method:
public function index()
{
// grab our users
$users = App\Users::all();
// return a view with the users data
return view('users.index')->with('users');
}
This grabs the users from the database and loads up a view passing the users data.
Here's what your view could look like:
<!DOCTYPE html>
<html>
<head>
<title>Users Page</title>
</head>
<body>
#foreach($users as $user)
<a href="{{ URL::route('user', ['id' => $user->id]) }}">
{{ $user->username }}
</a>
#endforeach
</body>
</html>
The view code will loop through each user from the $users data we passed to the view and create a link to their user page which is different for each user based on their id (their unique identifier in the DB)
Due to the way I've named it, this would be found in app/views/users/index.blade.php - if you save files ending in blade.php you can use Laravel's templating language, blade.
Now you need to finally set up another route to respond to a user page, for example http://your-app-name/public/user/22.
Route::get('user/{id}', ['uses' => 'UserController#show']);
Then add the show method to UserController
public function show($id)
{
// this will dump out the user information
return \App\User::find($id);
}
Hope that helps a little! Wrote most of it off the top of my head so let me know if you get any errors via comment.
This question is very bare, and it is difficult to actually help your situation without you showing any code. Just to point you in the right direction though, here is what you would need.
A Model called People, this is how you will access your data.
A controller. In this controller you will do the following
Get the ID of the profile you want from the functions parameters
Find that persons information e.g. People::find($person_id);
return the profile view with that persons data e.g. return view('profile')->with('person', $person);
In your view you can then use that data on that page e.g. {{ $person->name }}
For the page that needs to display the links to the people you would have a method in your controller which..
Get all the people data e.g. People::all();
Return a view with that data return view('all-people')->with('people', $people);
You will then need a route to access an individual person. The route will need to pass the persons ID into a controller method e.g.
Route::get('get-person/{id}',
[ 'as' => 'get-person',
'uses' => 'PeopleController#getPerson' ]);
You can then use this route in your view to get the links to each person
#foreach($people as $person)
{{$person->name}}
#endforeach
This would produce the list of links you want.

Resources