Laravel 4 search function - laravel

From my previous question, I've gotten this code from a user.
// app/routes.php
Route::get('characters', 'CharactersController#all');
Route::get('characters/{name}', 'CharactersController#detail');
// app/controllers/CharactersController.php
class CharactersController extends BaseController
{
public function all()
{
// show all characters
}
public function detail($name)
{
// find character by name & show detail for example
return View::make('acc.test');
}
}
// app/views/acc/test.blade.php
// HTML::style('css/style.css') loads CSS file located at public/css/style.css
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
{{ HTML::style('css/style.css') }}
</head>
<body>
</body>
</html>
also, the search function:
<form action="{{ URL::action('CharactersController#search') }}" method="get">
<input type="text" name="search-term">
<input type="submit" value="Search">
public function search()
{
$name = Input::get('search-term');
$searchResult = Character::where('name', '=', $name)->get();
....
}
Route::get('characters/search', 'CharactersController#search');
how could I in the :
public function detail($name) { // find character by name & show detail for example return View::make('acc.test'); }
how could I find the character by name? I've tried doing something like
$name = $player->name
(I have a model called players, I've also changed Character::where to Player::where), what do I have to insert there? also, how could I display it in the view?
So I mean when I search a player by name it displays the players name ($player->name) for every specific player.
Also do I have to change the relations in the model toHasMany or something like that?

You can do it this way
public function detail($name) {
$player = Player::where('name', '=', $name)->first();
}
If more players can have same name I would rather pass ID instead of name, so in this case use this solution
public function detail($id) {
$player = Player::findOrFail($id);
}

Related

Passing the same variable to multiple laravel controller functions

I'm trying to create a variable accessible by two different controller functins in laravel. How can I do that. The first function gets a value from a blade, it stores it in a variable and then I want to pass that variable with value to another controller function. For example, the following blade passes obj_id to controller:
1) My blade:
<!DOCTYPE html>
<html>
<meta charset="UTF-8">
<head>
<title>test</title>
</head>
<body>
<form method='post' action="/hard">
{{csrf_field()}}
<br>
<legend><i> Fill Data </i></legend>
<br>
<label>
OBJECT ID:
<input name='obj_id' type='text' minlength="8" required="" oninvalid="this.setCustomValidity('8 char at least')">
</label>
<br>
<input type='submit' value="Submit!">
</form>
<br>
<br>
</body>
</html>
2) My controller function Roger correctly gets obj_id (I have tested ot with dd)
public function Roger(Request $p)
{
$t = $p-> get('obj_id'); //I want $t to be global variable
//dd($t);
}
3) and then I want to pass $t to function Roger1 in the same controller
public function Roger1()
{
dd($t);
}
I have tried to declare $t as global with no success. I'm a little bit confused with $this and tried several combinations with no success.
Could you assist please?
You can use the session to store a variable
public function Roger(Request $p)
{
$t = $p-> get('obj_id'); //I want $t to be global variable
$p->session()->put('myvalue', $t);
}
public function Roger1(Request $p)
{
$p->session()->get('myvalue);
}
https://laravel.com/docs/5.8/session#storing-data
Scenatio #01
IF both methods are in the same controller AND your second method is called inside the first method (in the same call), you can just do:
class CoolController extends Controller {
public $var;
public function first_method(Request $value)
{
// Example 1: passing the value as a parameter:
$this->second_method($value);
// Example 2: passing the value through a class variable:
$this->var = $value; // $value: 'some-text'
$this->third_method();
}
public function second_method($value)
{
dd($value); // 'some-text'
}
public function third_method()
{
dd($this->var); // 'some-text';
}
}
Scenatio #02
Now, in the case you want to make a request from your view to set a value in your first method, and then another request calling your second method and getting that value that was "stored" in the first call.. well you can use any of this approaches. Why? because both call are in different lifecycles.
When the first call ended the value assigned in the first method (stored in memory) will be erased when the request is finished. That's why your second call will get a null value if you try to use it.
To store a temporary variable you have several paths:
Store it in the database.
Store it in the cache.
Send the value as a request parameter when doing the second call.

Codeigniter's controller data always shows on top of the html

I am trying to get the data of another function inside the controller, but there is a problem when I load it with the view. Anyway I placed the output of the controller inside the view and it still goes to the top of the HTML
this is the controller code
public function index()
{
$data['record'] = $this->samples();
$this->load->view('home',$data);
}//end index()
public function samples(){
echo "string";
}
this is the view
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<?php echo $record;?>
<h1>This is a sample</h1>
</body>
</html>
Now when I go to the browser it looks ok, but when you go to the source code you can see the data is on top of HTML. Why is that?
source code from view
You should return it
public function index()
{
$data['record'] = $this->samples();
$this->load->view('home',$data);
}
public function samples(){
return "string";
}

Why can I not use a body class selector with Laravel Dusk?

I am using Laravel 5.5 and Dusk 2.0. I have the following html.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
</head>
<body class="my-body-class" id="my-body-div">
<div class="my-content-class" id="my-content-div">
Content goes here.
</div>
</body>
</html>
Here's my Dusk test.
public function testBasicExample()
{
$this->browse(function (Browser $browser) {
$browser->visit('/test/admin-fixed-layout');
$this->assertNotNull($browser->element('.my-content-class'));
$this->assertNotNull($browser->element('#my-content-div'));
// $this->assertNotNull($browser->element('.my-body-class'));
$this->assertNotNull($browser->element('#my-body-div'));
});
}
If I un-comment the assertion that uses the body class selector, the test fails. Why?
This is because by default prefix is set to body:
public function __construct($driver, $prefix = 'body')
{
$this->driver = $driver;
$this->prefix = trim($prefix);
}
in Laravel\Dusk\ElementResolver class.
If you really need to change this (but probably there is no point), you can add the following method into Tests/DuskTestCase class:
protected function newBrowser($driver)
{
return new \Laravel\Dusk\Browser($driver, new \Laravel\Dusk\ElementResolver($driver, ''));
}
This will override default browser and pass empty prefix instead of default body prefix

What is the proper way of setting the page meta title in laravel

In many examples and documents I generally see that the page title is set via someController->main.blade.php->somePage.blade.php. Something like:
SomeController.php
public function someAction()
{
$title = 'Some Title';
return view('somePage', ['title'=>$title]);
}
main.blade.php
<head>
<title> #section('title') | Page #show </title>
...
somePage.blade.php
#section ('title')
{{$title}} #parent
#endsection
Wouldn't it be mode convenient to set it directly/only over the controller and blade layout file? I mean something like:
SomeController.php
public function someAction()
{
$title = 'Some Title';
return view('somePage', ['title'=>$title]);
}
main.blade.php
<head>
<title>{{ $title }}</title>
...
Wouldn't it be better to use it in that way?
I prefer not to assign the title from the controller - it's content and should be in the template from my point of view. I like to have a section in the template like
//layout file
<title> FancyApp - #yield('title')</title>
// Template
#section('title', 'Page Title')

Laravel 4 pages

I have a question. How could I create subpages (something like this: character.php?name=Xar) but I want it in Laravel. Do I have to create routes? Also to mention, when I create a route like this:
Route::get('account/test', 'HomeController#test');
and the view is in folder under views/aac/test, and the function is like:
public function test()
{
return View::make('aac.test');
}
it won't load the CSS. it's just an HTML page.
back to the problem again, how could I create sites like that? I'm also using Blade templating engine.
// app/routes.php
Route::get('characters', 'CharactersController#all');
Route::get('characters/{name}', 'CharactersController#detail');
// app/controllers/CharactersController.php
class CharactersController extends BaseController
{
public function all()
{
// show all characters
}
public function detail($name)
{
// find character by name & show detail for example
return View::make('acc.test');
}
}
// app/views/acc/test.blade.php
// HTML::style('css/style.css') loads CSS file located at public/css/style.css
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
{{ HTML::style('css/style.css') }}
</head>
<body>
</body>
</html>
Search function
Place search form somewhere in your view file
<form action="{{ URL::action('CharactersController#search') }}" method="get">
<input type="text" name="search-term">
<input type="submit" value="Search">
</form>
As specified, search form is submited to CharactersController and its search method.
Controller's method
public function search()
{
$name = Inpute::get('search-term');
$searchResult = Character::where('name', '=', $name)->get();
....
}
Register new route
Route::get('characters/search', 'CharactersController#search');

Resources