Difficulties with routes translation and api - laravel

I am creating an API for my project and I have the following problem:
I'm using the location files.
Route::group(array('prefix' => Config::get('app.locale_prefix')), function()
{
Route::get('{contact}', 'WelcomeController#index');
});
This part of code works correctly, and I can access with
http://localhost/project/public/en
However, I am interested in create an API to receive a list of products. So I add this code:
Route::group(array('prefix' => Config::get('app.locale_prefix')), function()
{
Route::get('{contact}', 'WelcomeController#index');
Route::group(['prefix' => 'api'], function() {
Route::get('test', function(){
return response()->json(['foo'=>'bar']);
});
});
});
I don't have any errors, but I can't receive a response. What is the problem?
http://localhost/project/public/en/api/test
UPDATED
I solve part os this problem , in the view I have this
<li><?= trans('locale.allauctions'); ?></li>
This works correctly but I need to put en/anotherurl , how can I put this ?

http://localhost/project/public/api/test is working fine.
This is because your Config::get('app.locale_prefix') is empty! So you defined there is NO prefix. Use dd(Config::get('app.locale_prefix')); to test it.
Why is http://localhost/project/public/en working then?
Because you defined a variable {contact} in
Route::get('{contact}', 'WelcomeController#index');
and en is {contact} then.

Related

How do I submit form using inertia and redirect to blade?

I am using a mix of inertia and blade pages in my Laravel application. I am submitting a form using inertia, saving the data, then want to redirect to a blade view. It redirects to the correct page, however, it opens in a modal.
My issue is actually more complex than below, but even when i hardcode the redirect, it opens in a modal. I thought i had to just add inertia middleware, but think im missing something else. thanks
userForm.vue:
store () {
this.form.post(this.route('user.store', {
user: this.userData...
route:
Route::post('/{user}/store/', [UserController::class, 'store'])->name('user.store')->middleware('inertia');
UserController:
return redirect('user/1')
One solution:
Inertia::location(/url)
If you use Inertia on client side, you shoud return an Inertia response, so in your controller:
return Inertia::location(route('user.show', ['id' => 1]));
Alternatively, you can post your data using axios and redirect on success:
axios.post(route('user.store', { user: this.userData })
.then(res => {
location.href = route('user.show', {'id' => 1})
})
.catch(err => {
console.log('Error: ', err);
});

pass array from vue.js to laravel controller, Error The GET method is not supported for this route. Supported methods: POST

I have a problem when trying to pass an array from vue.js to a laravel controller, the problem is that the route tells me that it is get but I have it as a post...
sorry for my bad english.
const create = new Vue({
el: '#createRemitos',
data:{
remitos:[]
},
methods:{
deleteElement:function(){
this.remitos.pop();
},
saveRemito: function(){
var url ='make/create';
axios.post(url,{
remitos: this.remitos
}).then(response => {
//toastr.success('Guardado');
console.log(response.data);
}).catch(e =>{
toastr.error('Error');
});
}
}
});
//this is my route.
Route::post('/make/create','CreateRemitoController#create')->name('create.store');
//this is my controller
public function create(Request $request){
return $request->all();
}
You are mixing absolute and relative URLs.
change from:
var url ='make/create';
to:
var url ='/make/create';
just pass route name instead passing url :
var url ='make/create';
become:
var url ='{{route("create.store")}}';
I already found the problem was an error of mine I did not know how to use axios the code I leave it for future reference is 100% functional thanks to the people who helped me.

How to set cookie after login laravel

how to set cookie after login laravel I want to do somethin like this
in web.php
Route::middleware(['auth'])->group(function () {
if(Auth::user()){
Cookie::queue('test', '1231231', 333);
}
});
but my code didnt work
UPDATE
Route::middleware(['auth'])->group(function () {
if(\Auth::user()){
Route::resource('admin/home', 'Admin\HomeController');
Route::resource('admin/promotion', 'Admin\PromotionController');
Cookie::queue('test', '1231231', 333);
}});
is because Auth::user() not available in the route file, unless you use a middle ware to do so,
or else you can try this way
Route::middleware(['auth'])->group(function () {
if(\Auth::user()){
Cookie::queue('test', '1231231', 333);
}});

How can I preload data for vue.js in Laravel Spark?

According to the docs and examples, I have perfectly working code that functions great:
Vue.component('admin-competitions-index', {
data: function() {
return {
competitions: []
}
},
mounted() {
this.$http.get('/api/admin/competitions')
.then(response => {
this.competitions = response.data;
});
},
methods: {
/**
* Toggle whether a competition is published or not.
*/
togglePublished(competition) {
Spark.patch(`/api/admin/competitions/togglePublished/${competition.id}`, this.togglePublishedForm)
.then(response => {
competition.is_published = response;
});
}
}
});
However, I'd like to change this code to save the extra request that is made on page load. I don't see a convention anywhere in Laravel or Spark where this is done. I'm guessing that all I need to do is set a JS variable but I'm not sure where it would be proper to do so.
I also understand that this kind of defeats the point of using vue for asynchronous loading, but nevertheless I would like to learn this. I think it will become more useful if I were to use vue for my #show restful requests where even if I wanted everything to load asynchronously I would at the very least have to supply vue with the competition ID that I want loaded.
This works out of the box:
#section('scripts')
<script>
var competition = {!! $competition !!};
</script>
#endsection

Application routes not working

I've just written some routes (app\routes.php) based on Laravel framework as following,
Route::model('cat', 'Cat');
Route::get('/', function()
{
return "All cats";
});
Route::get('/cats', function()
{
$cats = Cat::all();
return View::make('cats.index')->with('cats', $cats);
});
Route::get('/cats/breeds/{name}', function($name)
{
$breed = Breed::whereName($name)->with('cats')->first();
return View::make('cats.index')->with('breed', $breed)->with('cats', $breed->cats);
});
Route::get('/cats/{cat}', function(Cat $cat)
{
return View::make('cats.single')->with('cat', $cat);
});
Route::get('/cats/create', function()
{
return "Cat created.";
});
All routes are okay, except the one /cats/create.
I've tried to create other two dummies routes /dogs and /dogs/xxx, and the second one (/dogs/xxx) is not working.
It sounds weird but it actually happens. Has anyone face this problem before? Or you can provide me some hints to workout.
Maybe you need to put Route::get('/cats/create' before Route::get('/cats/{cat}. Right now system considers your create a {cat}.

Resources