Creating a link using laravel routes and vue.js - laravel

On my laravel home page I have a vue component like this
<new-tutor area-id="{{ $area->slug }}" tutor-id="{{ $tutor->slug }}" route="{{ route('tutor.show', [$area, $tutor]) }}"></new-tutor>
it returns all new tutors in a given area in individual bootstrap cards, and uses this controller
public function index(Request $request, Area $area, Profile $profile, Tutor $tutor)
{
$newTutors = Tutor::with(['user', 'area'])->inArea($area)->latestfirst()->get();
return response()->json($newTutor, 200);
}
I would like to be able to click on the tutors name and be sent to that tutors page, but I can't seem to get the tutor slug to pass though properly, and I am not sure why.
In my vue component I set the link like
<a :href="route">{{newTutor.name}}</a>
and I have props set up like this
props: {
areaId: null,
tutorId: null,
route: { type: String, required: true }
},
the slug does come through in a dd on newTutors, and Tutor.
Also note, I am using the same web route
tutor.show
in other blade templates, so I am mostly confident that that is not where the issue is.

if you using laravel + vuejs,You need add #{{}}
<a :href="route">#{{newTutor.name}}</a>

Related

Loading failed for the module with source “http://127.0.0.1:5173/resources/js/Pages/Ads/Index.vue?t=1670962685226”. Using Inertia, VueJS and Laravel

I am currently developing a CRUD application using Laravel 9, InertiaJS and VueJS.
When I try to go to the route /ads I have the following error message in the console :
My index page is in /Pages/Ads/Index.vue
The code of my route (web.php) is :
Route::resource('ads', AdController::class);
The code of my controller is :
class AdController extends Controller
{
public function index(Request $request)
{
$ads = Ad::with(['model','user'])->get();
return inertia('Ads/Index', compact('ads'));
}
}
The code of my Pages/Ads/Index.vue is :
<template>
<h1>Index ad</h1>
</template>
<script>
export default {
name: 'Index ad',
props: {
ads: Object,
},
}
</script>
I don't understand why I have this error, because I have other routes working and rendering properly Vue pages.
I check the Laravel and Inertia documentation but I didn't found the issue.
Thanks in advance for your help :)
I tried to look the at the differences with my code that is working with other routes, I checked the Laravel and Inertia documentation, and look online same issues.

Laravel 8x create user table with JetStream Teams

I am building a web app and running Laravel teams, I have made a user table but am unsure how to nest the data so that the users are viewing their main team. From my research I have found:
return App\Models\Calendar::where(
'team_id', $request->user()->currentTeam->id
)->get();
or
// Access a user's "personal" team...
$user->personalTeam() : Laravel\Jetstream\Team
but I need a little help applying it properly as I've never done it.
My current view, Controller, and route are:
View is just a standard table which I built to show with a foreach method:
<div class="">
{{$user->name}}
</div>
Controller:
public function render()
{
# Load all users and sort by name
return view('users.table',[
'users' => User::orderBy('name')->paginate($this->perPage)
]);
}
Route:
Route::middleware(['auth:sanctum', 'verified'])->group( function () {
Route::resource('users', \App\Http\Livewire\Users::class);
});
I tried to cut down the code so it's easier to give me suggestions, let me know if I missed anything.
This is my current screenshot of the users table:
File tree
Change
User::orderBy('name')->paginate($this->perPage)
with
Team::find(Auth::user()->current_team_id)->users()->paginate($this->perPage)

How to set url without refreshing the page (Laravel + vueJS)

I have a Laravel + VueJS app. I would like to change the url when the user selects a new year, for example.
The user in on the url idea/[IDEA_ID]/[YEAR] and changes year inside the page, so i want to set the url, but the rest of the work is done through axios call.
Here is how I work for now:
Route::get('idea/{n}/{m}', 'IdeaController#idea')->name('idea');
class IdeaController extends Controller
{
public function idea($id, $year)
{
$sql = "";
$array = DB::connection('ideas')->select( DB::connection('ideas')->raw($sql));
return view('ideas/idea', ['idea' => json_encode($array)] );
}
}
blade view:
#extends('template')
#section('content')
<div>
<div id="app">
<ideapage ideas="{{ $idea }}"></ideapage>
</div>
</div>
#endsection
And in my Vue view (ideapage), I have all the logic and only make axios requests.
The problem is that I want to change my url inside my Vue view, when the user changes the year for example.
Therefore I am wondering if I did the things well. Would it be a better idea to separate the components inside the laravel blade view? And how can I change only a section when the url changes?
I am not using VueRouter: the routes are in web.php only.
Thanks a lot in advance.
After a lot of thinking, I didn't change my architecture for now, and I only use history.pushstate when the year or idea changes
idea.vue
watch: {
idea: function() {
history.pushState({}, null, '/idea/'+this.idea +'/'+this.year)
},
year: function() {
history.pushState({}, null, '/idea/'+this.idea +'/'+this.year)
}
},

Laravel + Vue Page Redirection

How can I redirect to a page when I try to create something that it has to be done in another page?
Example:
I created page that will ask the details for books and saved it to the database. The first page will ask the user to put the cover of the book, title and description. I used Vue Component for the page and axios to save it in the database. I used this code window.location.href = "/create-book/" to redirect to another page which will be the user can start writing and when he saved it, it will save on the current book he was making.
right now Im stuck in this
Vue Component
axios.post('/admin/saveBook', formData).then(function(result){
// console.log(result.data)
window.location.href = "/admin/write-book";
});
and In my controller I used this
Controller
return response()->json($book);
My goal is just to make a one whole page for writing the content of the book. Thanks!
You might look into using Vue Router, specifically Dynamic Route Matching and Programmatic Navigation.
In your router, you'd have a dynamic path for your books.
routes: [
{
// dynamic segments start with a colon
path: '/books/:id',
name: 'book',
component: Book
}
]
Then in your component, you'd navigate to the book on creation.
axios.post('/admin/saveBook', formData).then(function(result) {
this.$router.push({ name: 'book', params: { id: result.data.book.id }});
});
If you need info for the book when you navigate to it, you can either fetch it using the id when mounted or pass the book as a prop on navigation. See Passing Props to Route Components.

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