is there a way that I automatically could return data to every view in laravel, so I don't have to do it in every single controller.
Like in the BaseController?
Yes, read the docs carefully
View::share('name', $value);
Related
This is a little bit hard to understand even the title I put. Sorry about that I just do not know how to clearly explain this, but I will try...
So for example I have a controller and inside that controller I have a function which return the data in the table of my database. then in the last column of every row, I make view,add,edit,delete options as links. When a user clicks on the add for example, they will redirect to an add page. After they submit the form of the add page. they should be redirected to the first page that return the data from the table. but the problem is, the variables of foreach loop in the first page do not got recognized by laravel anymore. because they do not got processed since the route does not goes to the controller and to the function that return the data instead it goes to add function.
So I want to know is there anyway to solve this? If you could provide sample code, I would appreciate a lot thanks
From your explanation, I believe the code to go back to the original page after adding, editing etc is simply return redirect()->back(). This will take the user back to the previous page. As for data continuity, one approach would be considering using sessions. They save data globally and can be accessed from any controller once saved. To do this, simply save the data with session(['session_name' => $data]) and to retrieve the data use session('session_name'). Let me know if this helps!
If you want ti redirect after something like a login or an activation process you can do it with something like this:
return redirect()->to('login')
You can specify the path from your web.php file as you can see in my example in 'myPath'
As #DerickMasai correctly pointed out it is better to use named routes instead of hard coding the path itself.
Naming a route can work like so:
Route::get('/login', [LoginController::class, 'index'])->name('login');
I am trying to figure out how to do something seemingly simple with Laravel Nova, but I can't figure this out.
What I want to do is reference relationship data in a text field. I see in Nova, and understand how to reference a relationship via the HasOne, HasMany ... facades. But what I want to do is get relationship data like this:
Text::make('State', $this->state->name)
This doesn't work, and something I noticed when trying to debug is that each function in a Nova resource seems to run multiple times. Here is the logging I added:
public function fields(Request $request) {
logger($this->state->name)
}
When I do this, there are 3 logging instances, the first 2 containing the state name, and the third not. I think that may have something to do with it not working, but don't know what might be causing this.
Thank you in advance for help!
There's a simple way to get relationship data into a Nova Text field, just use a closure:
Text::make('State', function() {
return $this->state->name;
})
As for the multiple calls to the fields function, the answer has to do with the question: Do you have another Resource in your Nova folder that is related to the Resource we are discussing? If so, that is why -- it needs to call fields to display it properly. You can examine the following query string parameters to get more insight: viaResource, viaResourceId, and viaRelationship.
I am working on a Laravel app that has used Blade files for a while. We are switching part of the app to use Vue now. One of the endpoints we had used pagination with a page parameter at the end of it.
For example: /store/api/users?page=1 and each page would show 20 users -- sort of like a lazyLoad.
Does this make sense to keep it like this for Vue? With Vue, shouldn't the endpoint just get me ALL the users and then I can do what I want with that data?
No, you should not query all the data and return to vuejs. If your data is huge then you will be in big trouble with slower performance. So, it's always a good idea to use Larave's paginations even while you are responding json instead of view.
For example when you were using blade, you were doing something like:
$users = User::where('column', $value)->paginate();
return view('user.index', compact('user'));
Right? Now you are using Vuejs and still have you covered with it's nice length aware paginator instance. So, now you can do something like:
$users = User::where('column', $value)->paginate();
return $users;
This Will return all paginations meta data like, total page, current page etc.
So that, you can manipulate those data perfectly in vuejs.
I am new to Laravel coming from CakePHP where the form and save method for a form is one and the same function name. I saw in many Laravel tutorials that the from method (that displays the form) is different than the method to save form (that actually saves data). Why using 2 different method names?
For example what's wrong with:
pub function xyz(Request $request)
{
if($results->isMethod('post')){
... then save and return redirect
}
... the code for showing the form in case there is no POST.
then having 2 routes one for GET and one for POST on the same url?
It is because people like to filter out things at route level not in controller, Also it helps developer to apply middleware grouping for each route separately. so that they can apply roles and permission etc. easily at route level.
It will looks horrible if mix all things in controller.
Think about middleware and groups in your code.
It is because you don't wanna mix a lot of logic in the same method . The case you have simple is the simple scenario . But there will be case where you wanna pass initial data in the create form . You have to write logic for that also in the same method and while you store the data you need to do the validation and calculate other business logic . If you combine all those things in one method it will mix all the things in one method and code difficult to read
i have a web application in Razor-MVC and the thing is this:
I have an Action that goes like every actions in MVC {controller}/{action}/{id}
Now... i have another action, that needs to return this view: return View("Xview") with the Model View too. The model view its not a problem, but the route i see when i do the return View("Xview") its like {controller}/{action} without the id... and i need that id in my route.
I know i can make a return RedirecToAction("Xview", new { id = idX }) but i need to use return View(...)
Edit:
The main issue because i need to return the view is because the ModelView in this action fills with temporal data... and in the Xview action, this ModelView its called from database and if i return RedirectToAction then the temporal data is lost, thats why i return View instead preserving my ModelView.
I hope you can help me with that,
Thanks!
Based on the edit to the question, I would say that the real issue is the state of the application. So instead of passing data between from request to request, use the Session object to store temporary results.
The route parameter is used for requests, not responses. Unless you redirect the user as part of your response, which turns out to be a new request.
If you want to return a View then pass any info you need as part of the model or in the ViewBag.
If you would like to reuses existing code (e.g. another action), then you will need to redirect with RedirectToAction.