get passed ID from an action - php

In my blade I tried to pass the ID in an action link.
test
I get the passed id like this in my controller:
public function edit()
{
$input = Input::get();
dd($input);
}
but the output of dd() is:
array:1 [▼
5 => ""
]
But why is it an array? I only want the number but I don't see why laravel gives me the input as an array.

Input::get() gives you the whole input array. Use the specific key to get a specific value, for instance Input::get('id'). But your action() call appears to just append the id without a key. Try e.g. action('DomainController#edit', ['id' => $domain->id]).
That said, you may want to look at named routes and route model binding - makes the whole thing easier.

I think that You should pass $id as a parameter in edit:
public function edit($id)
{
dd($id);
}

Related

Undefined foreach variable laravel

hello guys im having a problem with passing variable from my controller to views, as it does not identify its variable, here is my code:
RegisterController.php
use App\angkatan;
public function index()
{
$select = Angkatan::all();
return view('/auth/register')->with('name', $select);
}
My Route
web.php
Route::get('register', 'RegisterController#index');
and my view
register
#foreach($name as $ps)
#endforeach
the error say
Undefined variable: name (0)
im very thankful if anyone can help
You are just passing the wrong way $select variable to your view.
when you use Illuminate\View\View::with method you should pass an associative array which as key => value pairs
return view('/auth/register')->with(['name' => $select]);
You can also use compact which allows to pass variable which are accessible inside of the scope of your controller to the view and the string passed as argument to that function will be the name of variable accessible inside of the view file
$select = Angkatan::all();
return view('/auth/register', compact('select'));
You can not pass the variable in this way to the view. You have to pass an array in the second parameter of the with() method - it should be something like this:
return view('greeting', ['name' => 'James']);
return view('/auth/register', ['name' => $select]);
you can pass a second massive parameter to the view,
and also in with method, you need to pass massive as I remember.

LARAVEL Argument 1 passed to ... Must be of the type array, int given

I am trying to store an entry into a database but I get this error.
" Argument 1 passed to Illuminate\Database\Grammar::parameterize() must be of the type array, int given, called in C:\xampp\htdocs\im-stuff\test\vendor\laravel\framework\src\Illuminate\Database\Query\Grammars\Grammar.php on line 869 "
request()->validate(['description' => 'required']);
$project->addTask(compact('description'));
Ok, so this does work, but next and better version doesn't.
$attributes = request()->validate(['description' => 'required']);
$project->addTask($attributes);
So this is the model which is being used.
public function tasks()
{
return $this->hasMany(Task::class);
}
public function addTask($description)
{
$this->tasks()->create(compact('description'));
}
At this point I am lost and really don't understand.
why are you using compact when you are saving the record to database instead as i mentioned in my comment it should be like this simple and easy:
public function addTask($description)
{
$this->tasks()->create($description);
}
here you can find the working of compact
Thanks
You should try to pass the attributes array directly to the create() function, e.g.:
public function addTask($description)
{
$this->tasks()->create($description);
}
Compact creates an array containing the variables passed, in your case:
print_r(compact('description'));
Array
(
[description] => your_array_of_attributes
)
So with compact() you are encapsulating your attributes array in another array giving them the key 'description'.
The compact() function is very useful when you want to pass data to the view, instead of writing this:
view('materials.index', ['materials'=>$materials, 'users'=>$users]);
You can write:
view('materials.index', compact('materials', 'users'));

Laravel Model wrong return

I have this function in one of my controllers:
public function getApplicationFiles(Contract $contract) {
dd($contract->get());
}
The parameter is defind in the url. So my route is
Route::get('contracts/files/{contract}', 'ContractController#getApplicationFiles');
My problem is that the function getApplicationFiles displays all entries from the Contract type and not only the Object with the given id?
What am I doing wrong?
I would go with the following, make your route a named route like this:
Route::get('contracts/files/{contract}', 'ContractController#getApplicationFiles')->name('contract.files');
Then in your view use it like this:
Files
This should give you the expected result.
u don't need to pass all row u need to pass only one row try like this(if you are paassing only contract id in request)
public function getApplicationFiles(Contract $contract) {
//dd($contract);
return response()->json($contract); // if you pass json
return view('show',compact($contract); /// if you view
}

get parameter without define in function get from route in laravel

public function login($companyId="",$invoiceId="")
{
}
$companyId I want to fetch from URL get its working fine for me but here I want to get $companyid as input::get
public function login()
{
$companyid = Input::get();
}
I am getting null becourse i cannot pass name in get() becouse i m fetching it from url so how can i achive it ........
value i have passed from route
First you need to look at your route definition. If your route is as
Route::get('/login/{companyId?}/{invoiceId?}', array('as'=> 'login', function($companyId="",$invoiceId="") {}));
then remove those parameters and change it to
Route::get('/login', array('as'=>'login',function(){....}));
After that you can change your route call to
route('login', array('companyId'=> 1231323, 'invoiceId'=> 123123));
this arguments you passed in array will be query string. It is important to remove those dynamic segments from your Route definition.

laravel controller function parameters

I'm trying to call a function inside one of my controller from the action() helper function. I need to pass a paramter to my function.
Here is the funciton I'm trying to call :
public function my_function($clef = 'title')
{
$songs = Song::orderBy($clef)->get();
return View::make('my_view', compact('songs'));
}
Here is the way I call it :
Author
The function is always running with the default value, even if I put anything else in my call. From what I can see in my address bar, the paramter seems to be sent along with the call :
http://www.example.com/my_view?clef=author
From the little I know, it seems correct to me, but since it doesn't work, I must come to the evidence that it isn't. What would be the cleanest way to call my function with the right parameter?
The reason why it's not working is because query strings aren't passed as arguments to your controller method. Instead, you need to grab them from the request like this:
public function my_function(Request $request)
{
$songs = Song::orderBy($request->query('clef'))->get();
return View::make('my_view', compact('songs'));
}
Extra tidbit: Because Laravel uses magic methods, you can actually grab the query parameter by just doing $request->clef.
Laravel URL Parameters
I think assigning parameters need not be in key value pair. I got it to work without names.
If your route is like /post/{param} you can pass parameter as shown below. Your URL will replace as /post/100
URL::action('PostsController#show', ['100'])
For multiple parameters say /post/{param1}/attachment/{param2} parameter can be passed as shown below. Similarly your url will be replaced as /post/100/attachment/10
URL::action('PostsController#show', ['100', '10'])
Here show is a method in PostsController
In PostsController
public function show($param1 = false, $param2 = false)
{
$returnData = Post::where(['column1' => $param1, 'column2' => $param2 ])->get();
return View::make('posts.show', compact('returnData'));
}
In View
Read More
In Routes
Route::get('/post/{param1}/attachment/{param2}', [ 'as' => 'show', 'uses' => 'PostsController#show' ] );
URL Should be: http://www.example.com/post/100/attachment/10
Hope this is helpful.

Categories