How to add query string to Laravel view - php

I'm using Laravel 5.7 and would like to return a view, with query strings. E.g. below is what I'm doing right now.
return view('cart', compact('somevar'))
This takes the user to mydomain.com/cart
I'd like to add query strings, e.g. so the user goes to mydomain.com/cart?id=123
How would I do this?

You can achieve this :
return view('cart', ['id' => $id]);
Your data should be an array with a key-value pair.
You can also use with method:
return view('cart')->with('id', $id);
Also, you can use compact :
return view('cart', compact('id'));
For more, please refer Passing Data To Views

You can pass data to view like this:
return view('admin-panel.leave.index')->with($data);
And $data is an array that contain multiple values like this:
$data = array('count' => '7', 'id' => '4');

What your asking is not doable with this approach.Because, the main problem is /var/www/vendor/laravel/framework/src/Illuminate/View does not have what you are looking for. Only way to do that is return redirect()->route('cart', ['id' => 123]);
If this is really necessary, I think you need to redirect before call the /cart Something like this to your rootes/web:
Route::get('/addcart', 'CartsController#workaround');
Route::get('/cart', 'CartsController#showtheview')->name('cart');
Then in your CartsController:
public function workaround(){
// your code.....
return redirect()->route('cart', ['id' => 1]);
}
public function showtheview(){
//your code....
return view('cart', compact('somevar'))
}
Hope you get the logic.

I don't think it's possible. All the answers here forget that "somevar" has to be returned as well. Not only somevar compact data must be returned, but the URL must have variables too.
Put otherwise, you want internal blade data by compacting, but the URL must look different because it must have "&somevar2=xyz&somevar3=abc" appended.
Please correct me if I'm wrong, but not possible.

You can try this :
Route::get('cart/{id}', 'TicketsController#edit');
And in return:
return view('cart/'.$id, compact('somevar'))
Also you can try like this:
// app/Http/routes.php
Route::get('/cart/{id}', function ($id) {
return view('cart')->with('id', $id);
});
// resources/views/example.blade.php
The last part of the route URI is <b>{{ $id }}</b>

Related

Too few arguments for view error with laravel

I'm having a problem with laravel., I'm trying to send the variable $codes on my view :
$codes = Code::where('user', $id)->get();
return view('user.edit', ['user' => $user,'codes' => $codes]);
But I get that error Too few arguments to function e(), 0 passed in
The variable $user goes well but not the variable code, anyone have an idea for a solution?
Thank you all
use this:-
return view('user.edit')->with('codes',$codes);
you can get the user with :-
Auth::user()->name;
I'm pretty sure, if you are following Laravel naming conventions, in your eloquent query the column name is user_id and not user. Also if you are using such querys, and you have foreign, you can define relations in your models, like the following:
Code Model
public function user
{
$this->belongsTo(User::class);
}
User Model
public function codes
{
$this->hasMany(Code::class);
}
Instead of line
$codes = Code::where('user', $id)->get();
You can go with:
$codes = $user->codes();
If you don't have the foreigns in your database
$codes = Code::where('user_id', $id)->get();
If non of these helps, please dd() your variables before returning the view, and share result with me in comment. In laravel 7 how you return view is good.

ErrorException Missing required parameters laravel

I am having an issue by modifying the route for a view. I want instead of /company/id to show /company/id/name
the route:
Route::get('/company/{id}/{name}', 'PagesController#showCompany')->name('company.detail');
show method in controller:
public function showCompany($id){
$company = Company::find($id);
return view('company.show')->with('company', $company);
}
and in the view $companies is from a search controller - and it should get the results with a link to open the view
#foreach($companies as $company)
Show detail
#endforeach
if using only with id like /company/id works. What i am wrong?
A simple an elegant way (i think) is:
{{route('company.detail', ['id' => $company->id, 'name' => strtolower(preg_replace('/[^A-Za-z0-9-]+/', '-', $company->company_name))}}
You can have a friendly url name. I am sure that there are better solutions out there.
If you have more params in the route you can use an associative array and initialize each param name with a value.
the controller now is the same with the id.

Passing a parameter to a custom find method in cakephp 3.x

I want to build a custom find function that retrieves bands for a given genre, i have tried this but the function can't access to the parameter $genre:
public function findGenre(Query $query, array $options)
{
$genre = $options['genre'];
$bands = $this->find()->contain([
'Genres' => function($q){
return $q->where(['Genres.id' => $genre]);
}
]);
return $bands;
}
I can access the $genre outside the contain() method, but not inside it.
My question is, how can i pass the $genre var to the function($q) inside the contain method.
I found where the problem is, i had to use the keyword use after the function($q), so that part of the code will look like this
$bands = $this->Bands->find()->contain('Genres', function($q) use ($genre){
return $q->where(['Genres.name'=>$genre]);
});
Also,the contain() method returns all the data even if the bands don't belong to a genre, but when i replaced it with matching() it worked just fine.
I hope this will help anyone who is having a similar problem in the future.
I was facing same issue but now it's resolved. I will explain you step by step:
My tables are:
articles: id,name,status,created
tags:id,name,status,created
articles_tags: id,article_id, tag_id
my query is this:
I want to pass my $tag_data['slug'] in matching variable but
directly this variable is not working in query. So I put in simple
$uses variable and now it's working properly.
$uses = $tag_data['slug'];
$contain_article = ['Tags'];
$query = $this->Articles->find('All')
->where(['Articles.status' => '1'])
->contain($contain_article)
->matching('Tags', function ($q) use ($uses) {
return $q->where(['Tags.slug' => $uses]);
});
Please try this :-)

Argument 2 passed to Illuminate\Database\Eloquent\Model::update() must be of the type array, string given

While trying to change the status of user i am facing this problem.
in route:
Route::get('userstatus/{id}/{status}', 'UserController#changeStatus');
in controller:
public function changeStatus($id, $status, User $user)
{
$user->update(array('status' => $status), $id);
return redirect('users');
}
In database i am storing status like :
"active"
"banned"
"hold"
why am i getting this problem?is there any solution?
Use it like below:
$user->where('id', $id)->update(array('status' => $status));
See the docs

laravel compact() and ->with()

I have a piece of code and I'm trying to find out why one variation works and the other doesn't.
return View::make('gameworlds.mygame', compact('fixtures'), compact('teams'))->with('selections', $selections);
This allows me to generate a view of arrays for fixtures, teams and selections as expected.
However,
return View::make('gameworlds.mygame', compact('fixtures'), compact('teams'), compact('selections'));
does not allow the view to be generated properly. I can still echo out the arrays and I get the expected results but the view does not render once it arrives at the selections section.
It's oké, because I have it working with the ->with() syntax but just an odd one.
Thanks.
DS
The View::make function takes 3 arguments which according to the documentation are:
public View make(string $view, array $data = array(), array $mergeData = array())
In your case, the compact('selections') is a 4th argument. It doesn't pass to the view and laravel throws an exception.
On the other hand, you can use with() as many time as you like. Thus, this will work:
return View::make('gameworlds.mygame')
->with(compact('fixtures'))
->with(compact('teams'))
->with(compact('selections'));
I just wanted to hop in here and correct (suggest alternative) to the previous answer....
You can actually use compact in the same way, however a lot neater for example...
return View::make('gameworlds.mygame', compact(array('fixtures', 'teams', 'selections')));
Or if you are using PHP > 5.4
return View::make('gameworlds.mygame', compact(['fixtures', 'teams', 'selections']));
This is far neater, and still allows for readability when reviewing what the application does ;)
I was able to use
return View::make('myviewfolder.myview', compact('view1','view2','view3'));
I don't know if it's because I am using PHP 5.5 it works great :)
Laravel Framework 5.6.26
return more than one array then we use compact('array1', 'array2', 'array3', ...) to return view.
viewblade is the frontend (view) blade.
return view('viewblade', compact('view1','view2','view3','view4'));
Route::get('/', function () {
return view('greeting', ['name' => 'James']);
});
<html>
<body>
<h1>Hello, {{ $name }}</h1>
</body>
</html>
or
public function index($id)
{
$category = Category::find($id);
$topics = $category->getTopicPaginator();
$message = Message::find(1);
// here I would just use "->with([$category, $topics, $message])"
return View::make('category.index')->with(compact('category', 'topics', 'message'));
}
You can pass array of variables to the compact as an arguement
eg:
return view('yourView', compact(['var1','var2',....'varN']));
in view:
if var1 is an object
you can use it something like this
#foreach($var1 as $singleVar1)
{{$singleVar1->property}}
#endforeach
incase of scalar variable you can simply
{{$var2}}
i have done this several times without any issues
$data = [
'var1' => 'something',
'var2' => 'something',
'var3' => 'something',
];
return View::make('view', $data);

Categories