Support the ongoing development of Laravel.io →
Requests Database Eloquent
Last updated 2 years ago.
0

Could you show the view, route, and controller code? When working on forms with "array" inputs, you may use dot notation to access the arrays:

$input = Request::input('products.0.name');
Last updated 9 years ago.
0

Hi @jimgwhit, there isn't a view, I'm buiding an REST API.

There's only one "input", it's named id. My controller code is:

$data = Request::all();

	$video = Video::find($data['id']);

	if (empty($video))
	{
		$output = ['success' => false];
	} else {
		$output = [
			'success' => true,
			'url'			=> $video->video_uri,
			'video'		=> [
				'mp4'		=> $video->video_uri .'.mp4',
				'webm'	=> $video->video_uri .'.webm',
				'ogg'		=> $video->video_uri .'.ogg'
			]
		];
	}

	return Response()->json($output);

If I pass 1jsjsja the Video::find($data['id']); returns the Video table entry with id = 1, instead returning an error. But If I pass 1231 it returns an error, since there's no 1231 id on the table.

0

OK, do an if to handle a no find, or a try catch. Search laravel for error handling. Also have you tryed something like $video[0], test what you have with a printr.

Last updated 9 years ago.
0

The reason for that is in PDO itself.

For example if you execute this statement you will get user with id = 1.

$pdo = new PDO ("mysql:host=$hostname;dbname=$dbname",$username,$pw);

$sql = "select * FROM users where id = :id";

$sth = $pdo->prepare($sql, array(PDO::ATTR_CURSOR => PDO::CURSOR_FWDONLY));

$sth->execute(array(':id' => '1asdasdasd'));

$user = $sth->fetchAll();

Anyway you should have some kind of validation like.

$data = Request::all();

$validator = Validator::make($data, array('id' => 'required|numeric'));

if (! $validator->fails())
{
    $video = Video::find($data['id']);

    if (empty($video))
    {
        $output = ['success' => false];
    } else {
        $output = [
            'success' => true,
            'url'           => $video->video_uri,
            'video'     => [
                'mp4'       => $video->video_uri .'.mp4',
                'webm'  => $video->video_uri .'.webm',
                'ogg'       => $video->video_uri .'.ogg'
            ]
        ];
    }


} else {

	$output = ['success' => false, 'messages' => implode(' ', $validator->messages())];
}

return Response::json($output);
Last updated 9 years ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.